Howto switch to another ruby version (temporarily, per project, or globally) using rbenv

undefined

As systems engineer, who setups and maintains different servers running Ruby On Rails applications ” testing, staging, and production servers”, I’ve many ruby versions installed on my machine and I’ve to switch between them when deploying new code of ROR apps to any of my servers. Why I’ve to switch between Ruby versions running on my local machine? this because, each app uses different Gems version, which give me errors about wrong Gems version when deploying the new code to different kinds of servers. This happens on my machine which I use to deploy ROR codes to different servers using Capistrano. Also, I needed to switch between ruby versions installed on my continuous integration server “Jenkins”, as it’s one server have all my ROR apps and each one has it’s own ruby version. When I build my projects on Jenkins I always face issues related to ruby versions.

Unlike RVM, rbenv does not offer a command like rvm use but will always respect your project’s .ruby-version file.

We’ve three options to use to switch between different ruby versions:

  1. rbenv shell
  2. rbenv local
  3. rbenv global

But choose wisely, for while the true command will bring you life, the false command will take it from you.

Option 1: Temporarily Using rbenv shell Command

To change your Ruby version on your current shell, run the following command to show your current ruby version and switch to the desired one:

$ ruby -v
ruby 1.9.3p484 (...)

$ rbenv shell 2.3.1
$ ruby -v
ruby 2.3.1p353 (...)

Background: This actually sets the RBENV_VERSION environment variable in your terminal session.

Options 2: Per Project Using rbenv local Command

Looks like rbenv shell…, run the following command to show your current ruby version and switch to the desired one:

$ cd <Your_App_path>
$ ruby -v
ruby 1.9.3p484 (...)

$ rbenv local 2.3.1
$ ruby -v
ruby 2.3.1p353 (...)

…but actually writes that version to a .ruby-version in your current directory. Use this only when you want to change the Ruby version on a project, not to change it temporarily (as you’d change your project’s file or clutter whatever directory you are currently in with that file).

Now, I’ll check the created .ruby-version files to see what wrote in it, run the following command:

$ cat .ruby-version
2.3.1

Option 3: Globally Using rbenv global Command

This will also change your Ruby version, but only the one you are using whenever no other version is specified, e.g. via a .ruby-version file or RBENV_VERSION variable, run the following commands to show your current ruby version and switch globally  to the desired one:

$ ruby -v
ruby 1.9.3p484 (...)

$ rbenv global 2.3.1-p353
$ ruby -v
ruby 2.3.1p353 (...)

$ echo "1.9.3p484" > .ruby-version
$ rbenv global 2.3.1-p353
$ ruby -v
ruby 1.9.3p484 (...)

Note that this is really your global Ruby version, so calling that on one terminal session will affect other terminal sessions as well – unless they are inside a directory tree using .ruby-version, of course.

If You Appreciate What We Do Here On Mimastech, You Should Consider:

  1. Stay Connected to: Facebook | Twitter | Google+
  2. Support us via PayPal Donation
  3. Subscribe to our email newsletters.
  4. Tell other sysadmins / friends about Us - Share and Like our posts and services

We are thankful for your never ending support.

Leave a Reply

Your email address will not be published. Required fields are marked *