47

I installed Ruby with Homebrew:

brew install ruby

Under "Caveats" it said:

NOTE: By default, gem installed binaries will be placed into:
/usr/local/Cellar/ruby/1.9.3-p194/bin

You may want to add this to your PATH.

What does that mean and how can I add it to my "path"? Assuming it has to do with a bash_profile but new to this.

TechRemarker
  • 2,788
  • 11
  • 37
  • 60
  • 17
    @Kyle's solution below is correct. However his "Note" is unfortunate. There is a ridiculous amount of group-think out there that everyone needs to install Ruby using rvm or rbenv so they can manage unlimited versions of Ruby. I used to do this because everyone else said so but I only ever used the latest version of Ruby. Before you go out and decide to add yet another form of package management to your system, you should decide if you actually need to maintain old versions of Ruby for any reason. If not, go with the Homebrew version described here. It works great. –  Jun 19 '13 at 20:22
  • @TomD The real advantage to rvm is their use of gemsets. It comes in handy when you have multiple ruby projects using different gem versions. It's unfortunate you didn't mention this. – Kyle Jun 19 '13 at 21:41
  • 2
    @Kyle That's true, gemsets are an advantage. However, I don't think it's wise to assume people's needs. In my opinion, these tools (as wonderful as I think they are) add complexity to a simple Mac-based Ruby setup and shouldn't be treated as necessary or advantageous for all or even most situations. –  Jun 20 '13 at 14:36

5 Answers5

63

in ~/.bash_profile add the following line

export PATH=/usr/local/Cellar/ruby/1.9.3-p194/bin:$PATH

When you're done, close your terminal and re-open it. You should be fine.

Alternatively, you can execute the follwing in each open shell instead of closing/re-opening:

source ~/.bash_profile

Note: I highly recommend installing ruby via rvm or rbenv so you can manage multiple ruby versions and use gemsets.

Kyle
  • 21,978
  • 2
  • 60
  • 61
  • 6
    +1 for rvm or rbenv I'd give more votes if possible :) heavymark: Don't do it any other way. – cbrulak Sep 05 '12 at 18:55
  • Thanks, so first I should create a blank file called ".bash_profile" in my home directory correct? I'm on a brand new mac and be default that file does not exist. Another item I installed said, "Homebrew installed npm. We recommend prepending the following path to your PATH environment variable to have npm-installed binaries picked up: /usr/local/share/npm/bin". So do I need two "path" lines or do I have to combine those somehow? As for "rvm" I like Homebrew and don't need multiple versions for my limited needs. But thanks! – TechRemarker Sep 05 '12 at 18:59
  • create that file and just add another path section on the same line `export PATH=/foo/bar:/baz/bar/:some/other/path:$PATH` ... each entry is separated by a colon. `$PATH` is saying that you'd like to also keep whatever the path was already set to. – Kyle Sep 05 '12 at 19:01
  • @heavymark I would *strongly* recommend rvm or rbenv anyway; the possibility of screwing things up is just to great. – Dave Newton Sep 05 '12 at 19:14
  • Since I've already done "brew install ruby", I don't know how to uninstall it from homebrew before installing it with rvm. I don't think "brew uninstall ruby" exists. – TechRemarker Sep 05 '12 at 20:39
  • @heavymark Just because everyone says to use rvm or rbenv does not mean it's necessary. If you are only using the latest version of Ruby there is **absolutely no reason** to use anything other than Homebrew. –  Jun 19 '13 at 20:26
  • 5
    @Kyle Yes, I was. To improve the answer you can fix the emphasis of your note. For example, change your note to read "If you need to manage multiple Ruby versions and environments, I highly recommend installing Ruby via rvm or rbenv." I think the wording you use is unnecessarily subjective. I think rvm and rbenv add unnecessary complexity for some users who may not have such needs. It's not that I'm necessarily opposed to rvm and rbenv, both are great packages, but in my experience the vast majority of Ruby users use only one version of Ruby along with Bundler and these just add confusion. –  Jun 20 '13 at 14:33
  • @TomD I would suggest that you provide your own answer and let the community decide. My answer solved the OP's issue and my note did not invalidate the answer. I do not think a downvote was appropriate. A vote count should give visitors the ability to determine how the community feels about the solution provided. If SO users are downvoting because of opinions given that are not relevant to the question at hand, SO will quickly go downhill and not be a reliable resource. Just my $0.02 - downvote all you like. – Kyle Jun 20 '13 at 15:03
  • 4
    @Kyle You know very well how opinionated the Ruby community is and how many people would down-vote any words opposing rvm/rbenv. I'd be attacked for the same mindless reasons rvm was attacked when rbenv was released. An answer would not serve my cause; I'm not interested in standing in front of an avalanche. I just want to save new Ruby users the time I wasted learning these systems when they were totally unnecessary. As for your statement criticizing my down-vote, I think SO goes downhill when you add your tangential opinions to answers. Further, I find your Note misleading. Just my $0.02. –  Jun 20 '13 at 15:22
  • Is `export PATH=...` necessary ? Isn't `PATH=` sufficient ? – BaltoStar Mar 29 '14 at 03:46
  • @BaltoStar see this SO question:http://stackoverflow.com/questions/1158091/defining-a-variable-with-or-without-export – Kyle Mar 29 '14 at 20:18
14

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

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

This is an up-to-date version of Kyle's answer. As of May 2014, brew info ruby prints:

By default, gem installed executables will be placed into:

  /usr/local/opt/ruby/bin

You may want to add this to your PATH. After upgrades, you can run

  gem pristine --all --only-executables

...to restore binstubs for installed gems.

Community
  • 1
  • 1
pje
  • 21,801
  • 10
  • 54
  • 70
9

Install ruby:

brew install ruby

I'd recommend setting $PATH, $GEM_PATH and $GEM_HOME. For latest Ruby it's:

export PATH=/usr/local/opt/ruby/bin:$PATH
export GEM_HOME=/usr/local/opt/ruby/lib/ruby/gems/2.6.0
export GEM_PATH=/usr/local/opt/ruby/lib/ruby/gems/2.6.0

Put them in something like ~/.bash_profile.

And then to verify:

type -a ruby
> ruby is /usr/local/opt/ruby/bin/ruby
> ...

ruby -v
> ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-darwin18]

gem list
> *** LOCAL GEMS ***
> 
> did_you_mean (1.3.0)
> minitest (5.11.3)
> ...
Paweł Gościcki
  • 9,066
  • 5
  • 70
  • 81
  • 2
    I came here to instal ruby, to install brew, and I found answer how to instal ruby via brew!! :D – Gediminas Šukys Jan 09 '19 at 12:20
  • can you please check https://stackoverflow.com/questions/68317941/could-not-find-cocoapods-0-a-among-48-total-gems-gemmissingspecerror – vikramvi Jul 09 '21 at 16:56
  • I had to add "" around my location in .bash_profile to make it work. export PATH="/usr/local/opt/ruby/bin:$PATH" – Mochi Apr 30 '22 at 12:17
3

In ruby 2.6.x, brew info ruby says:

By default, binaries installed by gem will be placed into:
  /usr/local/lib/ruby/gems/2.6.0/bin

You may want to add this to your PATH.

ruby is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ruby first in your PATH run:
  echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc

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"

I don't want to update XXshrc whenever ruby is updated. My zshrc is:

if [ -d "/usr/local/opt/ruby/bin" ]; then
        export PATH=/usr/local/opt/ruby/bin:$PATH
        export PATH=`gem environment gemdir`/bin:$PATH
fi
mtgto
  • 511
  • 1
  • 5
  • 5
2

Quick fix:

Open /etc/paths.

Change the order of lines(highest priority on top).
/usr/local/bin
/usr/local/sbin
/usr/bin
/bin
/usr/sbin
/sbin
Don Johnson
  • 67
  • 1
  • 2
  • Just did `brew install ruby` on Mountain lion, was about to add path to ~/.bash_whatever, noted the switch to /etc/paths, added `/usr/local/opt/ruby/bin` to paths, ruby -v still showed old version... forgot about priority ordering, so this helped. – Dave Everitt Dec 01 '13 at 15:41