Read more

rbenv: How to switch to another Ruby version (temporarily, per project, or globally)

Avatar
Arne Hartherz
January 23, 2014Software engineer at makandra GmbH

Unlike RVM, rbenv does not offer a command like rvm use. By default, it respects your project's .ruby-version file.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more

If you need to change manually, you have several options:

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

You probably want rbenv shell.

Temporarily: rbenv shell

Changes your Ruby version on your current shell:

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

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

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

Note that your terminal session will no longer respect any .ruby-version files. You need to run rbenv shell --unset to enable the auto switch again.

Per project: rbenv local

Looks like rbenv shell...

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

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

...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).

Globally: rbenv global

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.

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

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

$ echo "1.9.3p484" > .ruby-version
$ rbenv global 2.0.0-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.

Posted by Arne Hartherz to makandra dev (2014-01-23 13:09)