45

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.

Timo Tijhof
  • 10,032
  • 6
  • 34
  • 48
john2x
  • 22,546
  • 16
  • 57
  • 95
  • Maybe the path is not being passed correctly to sudo. I've seen this happen on computers not my own, where I haven't cared enough to find out how to fix it. Try doing `sudo /usr/local/Cellar/ruby/bin/gem list` (or something, not sure about the path... I no longer have a Mac) to see if that works. If it does, you can use that workaround until someone comes along who knows how to configure your ZSH/sudo to fix this. – Marten Veldthuis Jun 26 '11 at 08:35

7 Answers7

161

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.

Timo Tijhof
  • 10,032
  • 6
  • 34
  • 48
  • As Laruent mentioned below `$(brew --prefix ruby)/bin` would probably be a better alternative. – Mat Schaffer Apr 02 '13 at 05:41
  • I didn't use that before because `brew --prefix` used to be quite slow, but I see that has improved. I'll adopt it, thanks! – Timo Tijhof Apr 02 '13 at 18:03
  • 1
    I think this should be `export PATH="/usr/local/lib/ruby/gems/2.6.0/bin:$PATH"` – adriendenat May 17 '19 at 14:54
  • This is awesome! I've used the info you've provided here, and iterated on it to produce a version that doesn't hardcode the version number: https://stackoverflow.com/a/62674795/399007 – Dan Caseley Jul 01 '20 at 10:29
  • 1
    in my case, I had to change GEM_HOME, so instead of having `export PATH=/usr/local/lib/ruby/gems/2.7.0/bin:$PATH`, I have `export GEM_HOME="$HOME/.gem"` and `export PATH="$GEM_HOME/bin":$PATH` – Adem Oct 16 '20 at 10:37
  • Important note for anyone using any version of ruby other than 2.7.0 (ie. ruby 3.0.0 in my case). Make sure you change the `export PATH=/usr/local/lib/ruby/gems/2.7.0/bin:$PATH` to indicate whatever version of ruby you are using. For example, for ruby 3.0.0 you need to following `export PATH=/usr/local/lib/ruby/gems/3.0.0/bin:$PATH`. – joeldesante Jan 08 '21 at 23:11
22

brew unlink ruby; brew link ruby might add symlinks to /usr/local/bin/:

$ which sass
$ brew unlink ruby; brew link ruby
Unlinking /usr/local/Cellar/ruby/2.0.0-p0... 20 links removed
Linking /usr/local/Cellar/ruby/2.0.0-p0... 31 symlinks created
$ which sass
/usr/local/bin/sass

brew --prefix ruby is still pretty slow, but you could also just add /usr/local/opt/ruby/bin to the path.

$ time brew --prefix ruby
/usr/local/opt/ruby
0.216
$ time brew --prefix ruby
/usr/local/opt/ruby
0.076
$ stat -f%Y /usr/local/opt/ruby
../Cellar/ruby/2.0.0-p0
Lri
  • 26,768
  • 8
  • 84
  • 82
  • 5
    This should be the accepted answer. The OP is using homebrew, so an answer solving for the particular use case (when available) is more appropriate than suggesting a different workflow. – lhagemann May 02 '14 at 02:35
10

I like home brew. There's probably a better way to do this, but if you run:

gem environment

That will print out a nice list of all the relevant paths. Look for the one labeled EXECUTABLE DIRECTORY. That's the one you want to add to your path. In my case that's /usr/local/Cellar/ruby/1.9.3-p362/bin/ruby but I would imagine it would change with newer version of Ruby.

I'm using /bin/bash as my shell, but the process of adding it to your path should be pretty much the name.

I use TextWrangler (via the command line tools) to edit my .profile file. To do that, it's just:

edit ~/.profile

When your done, either close your terminal and open a new one, or run:

source ~/.profile
Jachin
  • 2,025
  • 2
  • 19
  • 26
  • 1
    I had no problem using Homebrew with `gem install ruby` then (after using rvm for a few years then abandoning it, but being used to non-system-wide gem installs) just `gem install [gemname]`. To use the gems, I added `/usr/local/opt/ruby/bin` to the end of my `PATH` variable in .bash_profile. Simple, and works for me, as I don't need gemsets or multiple Rubies. – Dave Everitt Oct 15 '13 at 21:03
8

Using the info in Timo's answer, I've got this:

PATH=/usr/local/opt/ruby/bin:$PATH
GEMSDIR=$(gem environment gemdir)/bin
PATH=$GEMSDIR:$PATH
export PATH

Works for Homebrew, works for the separate gems directory, and doesn't hardcode a Ruby version.

Dan Caseley
  • 521
  • 6
  • 8
5

You can be fine with ruby installed by homebrew too.. You just lack the functionality of custom gemsets with homebrew.

first do:

sudo nano /etc/paths

this will bring up nano editor,

then add the following to the paths:

/usr/local/Cellar/ruby/1.9.3-p194/bin

your version of ruby will probably vary.

Thats it. It should now detect your gems.

Oh, btw, you need to Ctrl+X > y > ENTER to save a file in nano.

sambehera
  • 959
  • 3
  • 13
  • 33
4

Instead of using => $(cd $(which gem)/..; pwd)

You could use this instead => $(brew --prefix ruby)/bin

Timo Tijhof
  • 10,032
  • 6
  • 34
  • 48
3

I think this evolve a bit.

Just add

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

To your .bashrc (or .bash_profile, .zshrc/.bashrc, .. – whatever you use).

If you have a problem with ruby itself

brew unlink ruby
brew link ruby
gagarine
  • 4,190
  • 2
  • 30
  • 39