Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
799 views
in Technique[技术] by (71.8m points)

macos - Installing Ruby gems not working with Home Brew

The gems I install via sudo gem install ... can't be executed (I get a command not found). They seem to install into /usr/local/Cellar/ which is Brew's install directory (also, the gems in /Library/Ruby/ don't work either). Is there anything else I need to do to make the gems executable? I'm using ZSH on Mac OS X 10.6 with Ruby v1.8 for the one in Brew.

EDIT: It seems to be working now. I just went out for a few hours and came back to try it again.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Homebrew is nice. However unlike brew and npm, gem does not make aliases in /usr/local/bin automatically.

Solution

I went for a very simple approach (as of March 2020):

# Based on "`brew --prefix ruby`/bin"
export PATH=/usr/local/opt/ruby/bin:$PATH
# Based on "`gem environment gemdir`/bin"
export PATH=/usr/local/lib/ruby/gems/3.0.0/bin:$PATH

Add this to your .bashrc (or .bash_profile, .zshrc, etc.).

That's it! Now all Ruby bins and installed gems will be available from your shell!

In older versions of Homebrew (before 2017), there was a separate package for Ruby 2 called ruby20, for which you'd use the following snippet instead:

export PATH=/usr/local/opt/ruby20/bin:$PATH

This line was the only line needed at the time. But, in Ruby 2.1 the gems got moved to a separate directory. No longer under /usr/local/opt/ruby/bin, but instead at /usr/local/lib/ruby/gems/2.0.0/bin (where "2.0.0" is the last major Ruby version for Gem's purposes).

How it works

Homebrew keeps track of where it installed a package, and maintains a symbolic link for you that points there.

$ brew --prefix ruby
/usr/local/opt/ruby

$ l /usr/local/opt/ruby
/usr/local/opt/ruby@ -> ../Cellar/ruby/2.5.3_1

Effectively, adding /usr/local/opt/ruby to PATH is the same as the following:

export PATH=/usr/local/Cellar/ruby/2.5.3_1/bin:$PATH

Except, this long version hardcodes the currently installed version of Ruby and would stop working next time you upgrade Ruby.

As for Gem, the following command will tell you the exact directory Gem adds new packages to:

$ gem environment gemdir
/usr/local/lib/ruby/gems/2.7.0

Tools

These tools were meant to automatically bridge between Homebrew and Gem:

I haven't used these but they might work for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...