DEV Community

Cover image for Upgrade to Ruby 3 - upgrade ⚙️
Michael Currin
Michael Currin

Posted on

Upgrade to Ruby 3 - upgrade ⚙️

Install Ruby 3 ⚙️ 💎

Linux

$ sudo apt-get update
$ sudo apt-get install ruby ruby-dev
Enter fullscreen mode Exit fullscreen mode

macOS

$ brew install ruby@3.0
Enter fullscreen mode Exit fullscreen mode

Set up Ruby path ➡️

In the case of macOS, you'll have to add this to your PATH to run the custom Ruby.

If you had Ruby 2 running before you can leave this as is when using Ruby 3.

export PATH="/usr/local/opt/ruby/bin:PATH"
Enter fullscreen mode Exit fullscreen mode

Note that both ruby and bundler exist there.

$ which ruby bundler
/usr/local/opt/ruby/bin/ruby
/usr/local/opt/ruby/bin/bundler
Enter fullscreen mode Exit fullscreen mode

Test with:

$ ruby -v
Enter fullscreen mode Exit fullscreen mode
$ bundle -v
Enter fullscreen mode Exit fullscreen mode

Set up gems path 📦

This is to allow user level gems to run from anywhere.

If you like to install gems at the shared system level, like this, then you can skip this step.

$ gem install GEM_NAME
$ # OR 
$ sudo gem install GEM_NAME
Enter fullscreen mode Exit fullscreen mode

But if you like to install gems at the user level, like this, then you do need this step.

$ gem install GEM --user-install
Enter fullscreen mode Exit fullscreen mode

Add the following to .bashrc or .zshrc file:

if which ruby >/dev/null && which gem >/dev/null; then
  GEM_PATH="$(ruby -r rubygems -e 'puts Gem.user_dir')/bin"
  export PATH="$GEM_PATH:$PATH"
fi
Enter fullscreen mode Exit fullscreen mode

Then start a new terminal tab to load the changes.

That shell command will figure out that path to your user gems, like this:

  • ~/.local/share/gem/ruby/3.0.0/bin

Then it will add the path to your PATH variable.

The best part is that it is dynamic - it does not have a hard-coded path. So Ruby can change where it decides to install gems (like it did between Ruby 2 and 3 from ~/.gem to ~/.local). And you can upgrade from Ruby 3.0 to 3.1 or 4.0. Yet the shell command will figure out the correct path, without needing a manual update or causing head scratching when Ruby projects break one day.

Top comments (0)