As a Ruby on Rails developer, it’s important (and necessary) to have a fresh version of Ruby working on your machine. With most developers using MacOS, the rest of this article will be written with this in mind.
Use a Ruby Version Manager
One thing you’ll quickly learn is that Rails projects use different versions of Ruby, and things can quickly spiral out of control if you don’t have the ability to switch Ruby versions easily.
Your options are as follows:
Installing RVM
The first thing you’ll need to install is gpg which is required as part of the verification of the RVM package installer.
Run
brew install gnupg
Once the GPG library is installed, you’ll want to verify the installation package from RVM:
gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
After the keys are verified, you’ll then install RVM. There are other development versions, but we recommend using the most stable version unless you know what you are doing.
\curl -sSL https://get.rvm.io | bash -s stable
Note: If you want to include the most recent version of ruby, add the ruby
flag:
\curl -sSL https://get.rvm.io | bash -s stable --ruby
To ensure that RVM is working you can run the command:
rvm --version
rvm 1.29.12-next (master) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
Install a Specific Version of Ruby
To install a specific version of ruby, either a newer release or older version, you’ll use the RVM command line tool to do this.
Let’s say a rails project requires ruby 2.7.2p137
, but you only have ‌ruby 3.0.0p0
installed. We can use RVM to add these additional versions:
rvm install 2.7.2
This will take a few minutes for RVM to download the relevant version of ruby and setup the new paths.
To verify that the correct version of ruby is installed, you’ll need to use RVM:
rvm list
jruby-1.7.16.1 [ x86_64 ]
jruby-9.1.7.0 [ x86_64 ]
ruby-1.9.3-p551 [ x86_64 ]
ruby-2.0.0-p648 [ x86_64 ]
ruby-2.2.3 [ x86_64 ]
ruby-2.2.4 [ x86_64 ]
ruby-2.3.3 [ x86_64 ]
ruby-2.3.7 [ x86_64 ]
ruby-2.4.0 [ x86_64 ]
ruby-2.4.1 [ x86_64 ]
ruby-2.4.2 [ x86_64 ]
ruby-2.5.0 [ x86_64 ]
ruby-2.5.7 [ x86_64 ]
ruby-2.7.0 [ x86_64 ]
ruby-2.7.1 [ x86_64 ]
ruby-2.7.4 [ x86_64 ]
=* ruby-3.0.0 [ x86_64 ]
ruby-3.1.2 [ x86_64 ]
# => - current
# =* - current && default
# * - default
On my machine, I have several versions installed, including jruby. You’ll see the current and default marker showing which is the current version and which version is loaded by default from the command line environment.
To switch versions to ruby-2.7.2
, you’ll run:
rvm use 2.7.2
Then to be sure that this is correct, you can check the actual ruby version:
ruby --version
ruby 2.7.2p137
Running Rails with Different Versions of Ruby
If you are running a different version of Ruby in a Rails project, there’s a few other things to keep in mind.
Rails should automatically switch Ruby versions when in the context of the project
When you cd
into your rails project, you should see that the Ruby version automatically switches (which is why it’s important that it’s easy to install more than one version).
If you look at your Gemfile
you’ll see at the top of the file the Ruby version:
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.4.1'
...
As an example:
ruby -v
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin20]
cd ~/Projects/grocerylist
ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin17]
Ruby Gems need to be re-installed when updating or changing ruby versions
Within the ruby eco-system, Gems are used to manage different ruby packages as 3rd party plugins. Each ruby gem is tied to a specific version, and so for each version of Ruby installed, the relevant gems need to be re-installed.
If you find that your rails project doesn’t load when updating the Ruby version, it’s most likely that the gems need to re-installed with the bundler:
bundle install
Where are the packages on my computer!?
If you really want to dig into the Gems ecosystem, they should by default be installed in the ~/.rvm
folder:
ls ~/.rvm/gems/ruby-2.7.2/gems
Which will list all of the ruby gems installed for ruby version 2.7.2
. If you are trying to trouble shoot a particular gem and want to dig into the source code, this is where you’d look.
All of the gems will be located under the respective ruby version in the ~/.rvm/gems/
folder.
Keeping Up With Ruby
When it comes to being an efficient Ruby on Rails developer, it’s important to be able to quickly switch between Ruby versions. On top of that, Ruby regularly has new versions released, so you should be able to upgrade quickly without disrupting other projects and have the latest