Ruby package managers enable you to install multiple versions of Ruby. The two most popular products are Ruby Version Manager (RVM) and Rbenv. This article is for developers who want to use rbenv.

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 is to 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.

List all available versions of Ruby for rbenv.

rbenv install --list-all

List all available versions of Ruby within a specific version.

 rbenv install --list-all | grep 2.2

List only stable releases for rbenv.

rbenv install --list

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-rdocto 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 the directory to 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

Troubleshooting

You can upgrade rbenv and ruby-build using Homebrew by running the following command:

brew upgrade rbenv ruby-build

Resources