I recently wanted to try out Ruby on Rails 5
, but didn’t want to loose my ability to easily switch to Rails 4
if I wanted to. Here is my setup.
I use macOS Mojave
(10.14.2 at the time of this writing). If you use something different, you may need to alter some of the instructions to cater to your system.
However, one of the great things about RVM/Ruby/Rails
is that once setup, they should function agnostically across environments.
I use Ruby Version Manager (RVM) to manage my Ruby installs. It’s a handy tool, and a fairly simple install.
First open your Terminal
application and type in the RVM
command to see if it’s already on your machine:
Terminal
rvm --version
If it prints the version installed, you’re good go to! Skip ahead. Otherwise, let’s install it.
You can also follow the install instructions on RVM here.
First, let’s add the GPG key to verify the downloaded package:
Terminal
gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
If you run into an issue with the gpg
command missing, simply install it via the Homebrew package manager via brew install gnupg
.
After that, simply install the latest stable version of RVM
by running:
Terminal
curl -sSL https://get.rvm.io | bash -s stable
Then re-run the RVM
command from earlier to test if it’s working:
Terminal
rvm --version
Try opening a new terminal window/tab and try again. That will ensure all the newly added system paths are loaded.
That’s it for RVM
!
If using macOS, you probably already have Ruby
installed. Check via the ruby --version
command in terminal.
To list all the currently installed versions of Ruby
using RVM
on your system, run:
Terminal
rvm list
Then let’s list all the Ruby
packages available to install.
Terminal
rvm list known
This will display all the known Ruby
versions. The list might be a little long for your terminal, so simply scroll to the top part where the #MRI Rubies
are listed.
MRI stands for Matz's Ruby Interpreter, with Matz's being the nickname of the creator of Ruby, Yukihiro Matsumoto. More on Wikipedia.
If you’d like to install the latest version of Ruby
:
Terminal
rvm install ruby
Or a specific version:
Terminal
rvm install ruby-2.5.3 # Get this info from 'rvm list known'
Once done, verify by checking the current Ruby version:
Terminal
ruby --version
If it prints the version you installed you’re all set with Ruby
!
If it prints a different version than you installed, simply change using RVM
:
Terminal
# List all installed versions
rvm list
# Set version of Ruby
rvm use ruby-2.3.1
# Set default version
rvm use --default ruby-2.3.1
Ruby uses packages called gems
. gems
are specific to the version of Ruby they're installed on. Rails
is a gem
.
What this means is that the version of Rails
you install will be specific to the current active version of Ruby
. If you change Ruby
versions, your version of Rails
will have to be re-installed.
This is a good thing. In a little bit, you'll see how easy it is to switch versions of Rails
just by changing Ruby
versions.
To install Rails, just run:
Terminal
gem install rails # Latest version
# or
gem install rails --version=4.2.11.1 # For a specific version
You’re done!
Now, finally, after getting everything setup, this is how you can easily change between versions of Rails
. Simply change to a new version of Ruby
, install the version of Rails
you want, and that’s it!
Terminal
# List all installed versions
rvm list
# Set version of Ruby
rvm use ruby-2.3.1
# Set default version
rvm use --default ruby-2.3.1
I use Ruby 2.3.1
for Rails 4.2.11.1
, and Ruby 2.5.3
for Rails 5.2
. Using RVM
, it’s easy for me to change quickly to whichever version I want.
You don't have to switch Ruby
versions if you just want to make a new project with a specific version of Rails
(assuming it's installed). Simply run the command rails _4.2.11.1_ new new_project
You’re all done! Happy programming ✨