The RVM Recipe
Ruby Version Manager, aka RVM, is a command line tool designed to manage multiple installations of Ruby on the same device. In addition to allowing you to install and use multiple Ruby versions, it has other interesting features that you can use to maintain organization between projects with different gems. In this article I will explain my basic workflow and also some tips on using RVM. I do suppose that RVM is already installed.
RVM Basic Commands
Basic syntax: rvm, a category, and a command for that category.
To list known rubies, or the ruby versions that are available to install:
$rvm list known
To install a Ruby version:
$rvm install 2.3.3 # Latest known patch level
$rvm install 1.9.3-p551 # Patchlevel 551
To list all rubies and gemsets installed in your computer
$rvm list # List rubies only
$rvm list gemsets # List rubies and gemsets
$rvm gemset list # List gemsets for current ruby
Selecting Ruby to work
$rvm use 2.3.3 # For current session only
Defining the default version to use (you can refer to it as default)
$rvm 2.3.3 --default
Using the default ruby version
$rvm use default
Gemsets
RVM by default allows creating multiple environments for one ruby - called gemsets. Gemsets is a feature that allows you manage gem versions, so it is possible to define a package (or set)of gems with a specific version. When assigning a gemset to a project you ensure that you have an isolated environment acessing the correct version of each gem.
During installation of Ruby, RVM creates two gemsets:
- default - automatically selected when no @gemset specified: rvm use 1.9.3
- global - super gemset, inherited by all other gemsets for the given ruby
To create a gemset:
$rvm gemset create gemset_name
Using a gemset:
$rvm gemset_name
or Gemsets can be specified together with ruby name using gemsets separator(@):
$rvm gemset version@gemset_name
or
$ruby-2.3.3@my-project
To create a gemset and already use it:
$rvm --create use version@gemset_name
All gems that you install when using gemset will be confined to it. In the list of gems will appear only the gems of the gemset and those of the global gemset.
If you want to install a new gem to the global gemset, use:
$rvm gemset use global
$gem install name_of_the_gem
To see which gemset is being used:
$rvm gemset name
Listing all available gemsets for the version:
$rvm gemset list
Deleting a gemset and the gems contained in it (-force is not to ask for confirmation):
$rvm --force gemset delete gemset_name
Where are the gems installed? Type:
$gem env
That will result in something like:
RubyGems Environment:
…
- GEM PATHS:
- /Users/foss/.rvm/gems/ruby-2.3.1@jekill
- /Users/foss/.rvm/gems/ruby-2.3.1@global
In my case (using OS X El Capitan) I have all gemsets installed at /users/foss/gems
Separate Gemset to each project
Whenever you change to a directory (cd directory), RVM checks if there is a .rvmrc inside it. This file is a shell script that can contain any code needed to initialize the project environment. For example, if you want to load a gemset with a specific ruby version every time you enter the project directory, you just need to create a .rvmrc with the content:
$rvm ruby_version@gemset_name
With that in mind, I standardized the setup of my projects to:
$mkdir project_name
$cd project_name
$rvm --create --rvmrc use ruby_version@project_name
The last command will create a gemset for the project, and also the .rvmrc to load the correct gemset. As I am also using bundler, I create a Gemfile, listing the dependencies of the project, (even in older projects, with Rails 1.2.6). Then just install them with:
$bundle install
So, I have an isolated gems environment for each project, and the best, every time I switch to a project directory, RVM will already load the correct gemset based on .rvmrc!
Other rvm commands
To have a list of the available commands:
$rvm help
To get help on a specific command:
$rvm help command
If you want to uninstall a version of Ruby:
$rvm uninstall ruby_version
To delete a gemset:
$rvm gemset use gemset_name
$rvm gemset delete gemset_name
Check the RVM website for a detailed (and good) documentation at https://rvm.io/