6

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.

Robby
  • 165
  • 1
  • 11

4 Answers4

6

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

Robby
  • 165
  • 1
  • 11
2

@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
Nick
  • 473
  • 4
  • 8
1

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

prettycoder
  • 228
  • 1
  • 11
  • Thanks, surely I don't need `rbenv` to do this though? – Robby Feb 01 '19 at 01:56
  • 1
    @Robby using a ruby version manager like rbenv should help setup your paths correctly, both for ruby AND (importantly) for installing gems via `gem install` and `bundle install`. Plus, it's easier to switch/upgrade versions later – Jay Dorsey Feb 01 '19 at 02:31
  • 1
    After doing this - `ruby -v` still points to the version preinstalled with Mac OS. – kilogic Oct 21 '19 at 16:17
0

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.

Stone Zhang
  • 1
  • 1
  • 1