Robby
Robby

Reputation: 335

How to use the Homebrew's Ruby package instead of the Ruby package that comes with MacOS?

How can I switch from using the version of Ruby that comes with MacOS to the most recent version of Ruby that I downloaded using Homebrew?

My version of MacOS appears to have ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18] already installed with the system at usr/bin/ruby. I tried running brew install ruby to get the most recent version of Ruby however when I run ruby -v the same old version shows up. I figured I probably had to add it to my path so I went to my ~/.bash_profile and added

export PATH="/usr/local/Cellar/ruby/2.6.1/bin/ruby:$PATH"

but still ruby -v shows the old version. I closed the terminal, reopened the terminal, ran source ~/.bash_profile with no luck.

Upvotes: 7

Views: 5374

Answers (5)

Alexander Terp
Alexander Terp

Reputation: 425

For M1 / apple silicon users:

As per the official ruby formula caveat as of version 3.2.2.1:

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

^ can adapt to whatever shell you're using. This fixed the issue for me and my ruby went from /usr/bin/ruby to /opt/homebrew/opt/ruby/bin/ruby, which was the correct version.

Upvotes: 1

Stone Zhang
Stone Zhang

Reputation: 11

Instead of

export PATH="/usr/local/Cellar/ruby/2.6.1/bin:$PATH"

It is better to use this

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

Which is a symbolic link of ../Cellar/ruby/2.6.1. You can use readlink to print it.

Then you don't need to worry about upgrading ruby.

Upvotes: 1

Nick
Nick

Reputation: 532

@prettycoder's answer almost did it for me. I needed to do an rbenv init as well to get the proper version of ruby when running ruby -v:

brew install rbenv
brew upgrade ruby-build
rbenv install 2.6.5
rbenv global 2.6.5
 or
rbenv local 2.6.5
rbenv init

Upvotes: 2

Robby
Robby

Reputation: 335

Figured out my mistake.

export PATH="/usr/local/Cellar/ruby/2.6.1/bin/ruby:$PATH"

Should have been

export PATH="/usr/local/Cellar/ruby/2.6.1/bin:$PATH"

then just run

source ~/.bash_profile

and confirm with ruby -v or type -a ruby

Upvotes: 6

prettycoder
prettycoder

Reputation: 228

I would recommend using a version manager, e.g. rbenv

 brew install rbenv
 brew upgrade ruby-build
 rbenv install 2.6.1
 rbenv global 2.6.1
 or
 rbenv local 2.6.1

Details about rbenv here: https://github.com/rbenv/rbenv

Upvotes: 1

Related Questions