【问题标题】:Homebrew install Ruby keg-only can't find gemHomebrew install Ruby keg-only 找不到 gem
【发布时间】:2019-06-20 23:31:37
【问题描述】:

如何在使用 Homebrew 安装 Ruby 后让 irb 工作?

当我尝试运行 irb 时,出现错误:

$ irb
Traceback (most recent call last):
    2: from /usr/local/opt/ruby/bin/irb:23:in `<main>'
    1: from /usr/local/lib/ruby/site_ruby/2.6.0/rubygems.rb:302:in `activate_bin_path'
/usr/local/lib/ruby/site_ruby/2.6.0/rubygems.rb:283:in `find_spec_for_exe': can't find gem irb (>= 0.a) with executable irb (Gem::GemNotFoundException)

我试过了:

$ brew link ruby
Warning: Refusing to link macOS-provided software: ruby
If you need to have ruby first in your PATH run:
  echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile

For compilers to find ruby you may need to set:
  export LDFLAGS="-L/usr/local/opt/ruby/lib"
  export CPPFLAGS="-I/usr/local/opt/ruby/include"

我的/etc/paths 文件顶部有以下几行:

/usr/local/bin
/usr/local/opt/ruby/bin
/usr/local/lib/ruby/gems/2.6.0/bin

irb 没有出现在gem list 的输出中,但是:

$ find /usr/local -name irb
/usr/local/lib/ruby/2.6.0/irb
/usr/local/Cellar/ruby/2.6.0_1/bin/irb
/usr/local/Cellar/ruby/2.6.0_1/lib/ruby/2.6.0/irb
/usr/local/Cellar/ruby/2.6.0_1/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb
/usr/local/Cellar/ruby/2.6.0_1/share/ri/2.6.0/system/lib/irb

rirdoc 也有类似的问题。

【问题讨论】:

标签: ruby rubygems homebrew


【解决方案1】:

假设您使用的是 Homebrew Ruby...

irb 可执行文件位于:

/usr/local/opt/ruby/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb

您可以直接使用该行,将其符号链接到您的 $PATHalias 或其他任何内容中。


或者,您可以在第 22 行附近修补 /usr/local/opt/ruby/bin/irb

# patch
class Gem::BasicSpecification
  def self.default_specifications_dir
    File.join(Gem.private_dir, "specifications", "default")
  end
end
# /patch

# Next line looks like this. Don't change this.
# if Gem.respond_to?(:activate_bin_path)

您也可以在 /usr/local/opt/ruby/bin/ri/usr/local/opt/ruby/bin/rdoc 中执行相同的操作来修补这些命令。

为什么?

https://github.com/Homebrew/homebrew-core/blob/955497722b9bf65069957b0e7c903b96939cdd99/Formula/ruby.rb#L112

Homebrew Ruby 公式假定所有 gem 都将安装在“全局 gem 目录”/usr/local/lib/ruby/gems/2.6.0/ 中。因此,当您卸载-重新安装 Homebrew Ruby 时,gem 会一直存在 - 您也不必重新安装它们(有点烦人,因为我已经为 Ruby 版本安装了 gem,我什至不再安装了,但这是另一个问题)。

但是 Ruby 的默认 gem 不在全局 gem 目录中。它们位于 Ruby 安装目录中(Homebrew 公式称为private_dir):/usr/local/opt/ruby/lib/ruby/gems/2.6.0/

所以 Homebrew Ruby 找不到它们。

Homebrew patches Rubygems,所以这个 sn-p 再次修补 Rubygems,但更深。你也可以像这样打补丁:

module Gem
  def self.default_dir
    private_dir  
  end
end

但是default_dir is used in other places 我不想破坏任何东西。

【讨论】:

  • 这是一个应该报告给自制软件的错误吗?
【解决方案2】:

运行:gem install irb,你现在可以开始了。

【讨论】:

    猜你喜欢
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 2018-12-18
    • 2012-11-05
    相关资源
    最近更新 更多