Whenever I need to install different versions of Ruby and Rails, I use rbenv to manage my Ruby packages and this post will show you how to use it too.

Getting Started

To install multiple versions of Ruby on your computer, you'll need a Ruby package manager. The two most popular products are Ruby Version Manager (RVM) and Rbenv. This article assumes you have rbenv installed. If you have not installed rbenv yet, read this tutorial.

Step 1 - Install Ruby

You first need to install the right version of Ruby for your app. If you are working with an existing rails project, the easiest thing to do is look at the Gemfile.

For example, here is the Gemfile of a sample Rails 4 app for Heroku. It requires Rails 4.2.3 and Ruby 2.2.1.

Install Ruby 2.2.1

rbenv install 2.2.1

After rbenv installs ruby, run this.

rbenv rehash

You can double-check your work by asking rbenv to list which versions of Ruby you have installed.

rbenv versions

Step 2 - Install Rails

Now let's tell rbenv which version of Ruby to use when installing rails.

RBENV_VERSION=2.2.1 rbenv exec gem install rails --version 4.2.1 --no-ri --no-rdoc

We are skipping the documentation --no-ri and --no-rdoc so that we can speed up the installation process.

Step 3 - Create a Rails app

Create a new version of rails using a specific version of Ruby.

RBENV_VERSION=2.2.1 rbenv exec rails _4.2.1_ new [my_rails_4_app]

Step 4 - Set your local Ruby version

Change directory into your project.

cd [my_rails_4_app]

Set the local Ruby version.

rbenv local 2.2.1

Local enables you to keep your global version of Ruby yet use 4.2.1 on this local app.

Step 5 - Start Rails web sever

rails s