自制软件安装Ruby Keg-only找不到gem

时间:2019-01-27 23:39:04

标签: ruby rubygems homebrew

使用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的类似问题。

2 个答案:

答案 0 :(得分:1)

假设您正在使用Homebrew Ruby ...

const obj = { a: 4, b: 0.5 , c: 0.35, d: 5 }; const max = Math.max.apply(null, Object.values(obj)); console.log(max) // 5 可执行文件位于:

irb

您可以直接使用该行,将其符号链接到您的/usr/local/opt/ruby/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb $PATH等中。


或者,您也可以在第22行附近打上alias

/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中进行同样的操作以修补这些命令。

为什么?

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

Homebrew Ruby公式假定所有宝石都将安装在“全局宝石目录” /usr/local/opt/ruby/bin/rdoc中。因此,当您卸载并重新安装Homebrew Ruby时,这些宝石就缠住了-您也不必重新安装它们(有点烦人,因为我已经为Ruby版本安装了宝石,我什至都没有安装过,但这是另一个问题) )。

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

所以Homebrew Ruby无法找到它们。

Homebrew patches Rubygems,因此此摘要再次修补了Rubygems,但更深入。您还可以像这样修补补丁:

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

但是module Gem def self.default_dir private_dir end end is used in other places我不想破坏任何东西。

答案 1 :(得分:1)

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