100

I have installed ruby 1.9.3 using hombrew

brew install ruby

But default 1.8.7 is still used. How can I switch osx to use 1.9.3 as default ruby?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Alexey Zakharov
  • 24,694
  • 42
  • 126
  • 197

10 Answers10

104

SHORT ANSWER:

after installing ruby via homebrew just do this:

brew link --overwrite ruby

and restart or reopen your Terminal  


LONG ANSWER

So I did a normal install of ruby using homebrew

brew install ruby

that installed fine BUT it was still using the system's default ruby. which I verified by doing:

which ruby 
#/usr/bin/ruby

So as per Matthew Rudy's suggestion, I checked the order of my /etc/paths, and all was good.

Then I decided to do:

which -a ruby
#/usr/bin/ruby
#usr/local/bin/ruby

so nothing was broken as such. tried to reinstall ruby again using the homebrew method, and then i found it.

Homebrew mentioned:

Warning: ruby-2.3.1 already installed, it's just not linked

so had to do:

brew link --overwrite ruby
adamkonrad
  • 6,794
  • 1
  • 34
  • 41
Craig Wayne
  • 4,499
  • 4
  • 35
  • 50
  • 17
    Don't forget to restart your iTerm instance, otherwise `ruby --version` will show you an outdated version. – dialex Aug 22 '16 at 09:11
  • 1
    Do you actually need to restart? or just `hash -r`? – dwanderson Sep 26 '18 at 22:13
  • 1
    FYI, Homebrew now refuses do do it. For me with Fish, it suggested : echo 'set -g fish_user_paths "/usr/local/opt/ruby/bin" $fish_user_paths' >> ~/.config/fish/config.fish – Atem18 Aug 23 '19 at 22:50
  • 1
    In my case, I had to add `--force`. `brew link --overwrite ruby --force` – winner.ktw Jun 21 '23 at 09:31
95

I suggest you take a look at rvm. You can then set it as default with rvm use 1.9.3 --default

But if you are happy with your homebrew install.

Then just change the precedence of directories in the PATH

Here is my /etc/paths

# homebrews should always take precedence
/usr/local/bin

# the default stack
/usr/bin
/bin
/usr/sbin
/sbin

This is important generally for homebrew, else the system version of git, ruby, pg_admin,... will all be used instead of the brew version.

if you say which -a ruby you'll see all the installed rubies, and the precedence in the PATH

eg.

$ which -a ruby
/Users/matthew/.rvm/rubies/ruby-1.9.3-p0/bin/ruby
/Users/matthew/.rvm/bin/ruby
/usr/bin/ruby


UPDATE: I now don't think you should change /etc/paths

Instead you need to check which of .profile, .bashrc, or .bash_login is being loaded in your shell, and just add /usr/local/bin to your path.

For me, I only have a .profile. You can create that file if none of those files already exist in your home directory.

# homebrews should always take precedence
export PATH=/usr/local/bin:$PATH
Matt
  • 74,352
  • 26
  • 153
  • 180
Matthew Rudy
  • 16,724
  • 3
  • 46
  • 44
  • 1
    Finally i choose rbenv, previously used rvm too. – Alexey Zakharov Jan 05 '12 at 09:25
  • cool. I suggest you make the changes to your /etc/paths anyway though. I used to get weird errors (eg. I'd `brew upgrade git`, but the version didn't change. turns out I was never using the brew version) – Matthew Rudy Jan 05 '12 at 13:39
  • 31
    Thank you for providing an actual answer rather than just saying "use rvm". Not everybody can use RVM and I hate when the top answer just tells OP to do something that they didn't ask how to do. – bricker Jul 14 '12 at 06:51
  • This did the trick: http://www.interworks.com/blogs/ckaukis/2013/03/05/installing-ruby-200-rvm-and-homebrew-mac-os-x-108-mountain-lion – user2387149 Feb 02 '15 at 21:20
  • I had problems compiling ruby using rvm so I ended up going with rbenv instead since it worked without issues. – Pellet Dec 29 '16 at 07:35
  • RVM can turn your life into a nightmare. Stick with RBENV. Follow the installation instructions in their GitHub page. https://github.com/rbenv/rbenv – myhouse Jun 04 '18 at 16:54
  • Just pointing out for others - after installing with Homebrew I found the path to export needed to be added to the bottom of .bash_profile, placing it at the top had no effect. – Chris Wickens Sep 25 '22 at 22:20
48

If you'd like to use homebrew to install 1.9.3, you can follow these steps:

$ brew update
$ brew install rbenv
$ brew install ruby-build

Once you have rbenv and ruby-build installed, you can run the following command to get Ruby 1.9.3 installed.

$ rbenv install 1.9.3-p125

Now if you’d like to use 1.9.3 by default, you can run the following command:

$ rbenv global 1.9.3-p125
Francis Kim
  • 4,235
  • 4
  • 36
  • 51
11

Ruby was installed by Homebrew at /usr/local/opt/ruby. So, we need to add this path to bash or Zsh.

# Type this to find out which shell you're using (e.g., bash, Zsh)
echo $SHELL

# If you're using Bash (e.g., echo $SHELL returns /bin/bash)
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile

# If you're using Zsh
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc

Then, source the file

# E.g., if you're using bash
source ~/.bash_profile

Finally, verify ruby's version

ruby -v
Chau Pham
  • 4,705
  • 1
  • 35
  • 30
  • 1
    proper way for me. before you `echo` maybe run `echo $PATH` and see if any ruby was referred at all. I was setting up a new machine therefore no reference about ruby (via Brew) - the OS ruby does not need a separate reference. Till you add Brew/ruby to $PATH you will see `/usr/local/opt/ruby/bin` is included in. – seedme Jun 10 '22 at 02:34
10

I had similar situation. I installed ruby using Homebrew. which -a ruby gave me the following output:

#usr/local/bin/ruby
#/usr/bin/ruby

Which means that newly installed version should have been used, but ruby --version still returned the old system version.

I quit terminal (Cmd+Q), and after restart ruby --version returned the correct version. So make sure you restart terminal after installing before trying any other (potentially unnecessary) fixes.

Andrii Chernenko
  • 9,873
  • 7
  • 71
  • 89
4

SHORT: Do note what you want to change it for.

If you're on OS X and trying to use Ruby for something like Jekyll, then don't use homebrew because that's what Apple is using for Ruby for and it might not be good to use if you're not sure what you're doing. Instead, use rbenv or RVM.

LESS SHORT: I was trying to switch from the default version to an updated version (from 2.0) to use Jekyll because it required Ruby version 2.2.5 and above. I updated it and version 2.5 was installed, but when I checked "ruby -v", it was still 2.0. Once I finally got around to changing the default version, I wasn't able to install the package I needed because I didn't have write permission. For example, if you come across something like this, then you probably are having the same problem

$ gem install jekyll bundler
ERROR:  While executing gem ... (Gem::FilePermissionError)    
    You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
1
brew install ruby

This will install the latest version of ruby, now you want to set it as default use for example in my case:

echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc

then run

source ~/.zshrc

this will set it as default in the profile

now you can check the version of ruby

ruby -v
Waqas Mustafa
  • 192
  • 1
  • 7
0

In OSX you can change the path using:

sudo nano /etc/paths

And then add a path or change the order.

Robert
  • 261
  • 2
  • 11
0

After seeing Craig Wayne's answer I thought maybe I had missed a warning when installing ruby with Homebrew. So I reinstalled it with brew reinstall ruby@2.7 and there it was:

==> Caveats
By default, binaries installed by gem will be placed into:
  /usr/local/lib/ruby/gems/2.7.0/bin

You may want to add this to your PATH.

ruby@2.7 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

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

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

For pkg-config to find ruby@2.7 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/ruby@2.7/lib/pkgconfig"

So the solution for me was adding /usr/local/opt/ruby@2.7/bin: to the beginning of PATH in ~/.zshrc.

-2

Just as an alternative approach for anyone else looking for an answer to this - you can set an alias in your .bash_profile e.g

ruby="/usr/local/bin/ruby"

this is how i got around the issue

Bn Mk
  • 164
  • 1
  • 13
  • yeah the problem with that (and most likely the reason of the downvotes) is that this only works if you want to manually invoke ruby and not have the system use a specific version – David Seek Jul 28 '20 at 21:31