Table of contents
Introduction
RVM (Ruby enVironment Manager) is a command-line tool which allows you to easily install, manage, and work with multiple ruby environments from interpreters to sets of gems.
Every installed Ruby using RVM behaves similarly to how our built-in Ruby works. It is isolated in its own directory with its libraries and gems. This is really convenient if you need to maintain multiple projects, dependents on different Ruby versions.
RVM also has a very flexible gem management system called Named Gem Sets. These gemsets allows defining independent groups of gems, which is really convenient to maintain multiple projects with different set of gems. This is partially addressed by Bundler but while Bundler just takes care of installing and selecting the appropriate Gem version, RVM completely isolate the gem sets into its own GEM_PATH. Also, not all your applications will be compatible with bundler. It is also possible to use a combination of both, which will make sure you won't get in any trouble when working with different gem requirements.
Because of the above mentioned features, RVM is a perfect asset for your development process but that does not mean it is not suitable for production environments thanks to its ability to make setting up rubies / gems very efficient. It will also allow you to test your application in multiple Ruby versions so you will have a consistent environments from development, through testing, and into production.
Prerequisites
To make use of the RVM features you must have a compilation environment setup in your machine. You will need this compilation environment mostly to compile new Rubies but it will be also needed to compile some Gems such as nokogiri.
Depending on the operating system your are running, the steps to get the necessary tools will change. You can find a very detailed list of the packages to install by executing:
rvm requirements
It will include however more packages than what you will really need as our RubyStack already includes most of the required libraries.
The following sections provide a more specific list of packages to install for the mist common OS.
Ubuntu (And other Debian based Linux distributions)
On Ubuntu, you will need to execute the below command to get the minimum list of required packages:
apt-get install build-essential autoconf automake libtool bison
You may need other packages in the future but the above should be enough to start compiling your Rubies.
OpenSUSE / RedHat
On OpenSUSE, the list of packages is very similar to the ones listed in the Ubuntu section:
sudo zypper install -y patterns-openSUSE-devel_basis gcc-c++ libtool bison
OS X
There are multiple ways of setup the required environment in OS X:
* Install Xcode 4.1: This is the preferred way. There is no problems with using this version with the compilation of Rubies and the native gems.
* Using osx-gcc-installer, from Kenneth Reitz: This is a stripped down version of Xcode 4.1, which only includes GCC and the required libraries. The limitation of this solution is that it does not include certain headers, such as the Carbon/Cocoa headers, which are found in the main Xcode install (because of license restrictions from Apple). This means that some native gems will fail to compile. You could still get the header packs from Apple but you will have to be a registered Apple Developer.
* Using Xcode 4.2+: These versions of Xcode use a non-compatible version of GCC. If you want to use them, you will need to start installing Xcode 4.1, and then upgrade, answering not to them when asked to remove prior versions. Then you will have to use '/usr/bin/gcc-4.2' to compile.
Loading RVM
To use RVM interactively, you have to load it in an interactive shell. To do that you just need to execute the 'rubyconsole':
$ cd installdir $ ./rubyconsole
Managing Rubies
How to list the installed Rubies
To get the list of the currently installed Rubies you just have to execute:
$ rvm list rvm rubies # No rvm rubies installed yet. Try 'rvm help install'.
As the message details, if you have not yet manually installed any Ruby, the list will be empty. After installing a couple of Rubies, the output will change to the below:
$ rvm list rvm rubies ruby-1.9.1-p431 [ i386 ] =* ruby-1.9.2-p320 [ i386 ] # => - current # =* - current && default # * - default
How to select a Ruby from the list of installed versions
If your list of available rubies is the below detailed (with 'ruby-1.9.1-p431' currently selected):
rvm rubies ruby-1.9.1-p431 [ i386 ] =* ruby-1.9.2-p320 [ i386 ] # => - current # =* - current && default # * - default
Changing the Ruby interpreter being used is as easy as executing:
$ rvm use ruby-1.9.2-p320
$ rvm list rvm rubies * ruby-1.9.1-p431 [ i386 ] => ruby-1.9.2-p320 [ i386 ] # => - current # =* - current && default # * - default
$ ruby --version ruby 1.9.2p320 (2012-04-20 revision 35421) [i386-darwin11.2.0]
It is also possible to use the system Ruby (the main Ruby installed with the Stack, not through RVM) by executing:
$ rvm use system Now using system ruby.
How to install a new Ruby
You can get the list of all the available Rubies by invoking:
$ rvm list known
Which will print a list such as:
... # MRI Rubies ... [ruby-]1.8.7-head [ruby-]1.9.1[-p431] [ruby-]1.9.2[-p320] ... [ruby-]1.9.3-[p194] [ruby-]1.9.3-head ruby-head
To install a new Ruby from the list, for example 'ruby-1.9.1-p431', you just have to execute:
$ rvm install ruby-1.9.1-p431
RVM will take care of downloading, unpacking and compiling the requested Ruby
How to uninstall an existing Ruby
$ rvm uninstall ruby-1.9.1-p431
Managing Gem sets
RVM allows you to define multiple groups of gems (gemsets) for your RVM-managed Rubies. This is specially useful in those applications not managed using Bundler
How to list the available gem sets for a given ruby
After selecting an installed ruby using:
$ rvm use 1.9.3
You can get the list of defined gem sets by executing:
$ rvm gemset list gemsets for ruby-1.9.3-p194 (found in /opt/rubystack-3.2.3-0/rvm/gems/ruby-1.9.3-p194) global project-sample
In the example, two gemsets are available, global (the default gemset will the gems that are available to all other gemsets) and "project-sample", a custom gemset create for our 'sample' project.
How to create a gemset
$ rvm gemset create my_new_gemset
How to select a gemset for a given ruby
$ rvm gemset use my_new_gemset
How to share gems between all the gem sets
RVM defines a 'global' gemset which is shared among all the gem sets for an specific installed Ruby version. To install a shared gem, you just have to execute:
$ rvm gemset use global $ gem install rails $ rvm gemset use my_new_gemset $ gem list rails
Tweaking RVM
Enabling per project .rvmrc
This feature allows to define project specific settings such as the Ruby and Gems to use. Those settings will be loaded when entering the project directory.
To enable this feature, you must first edit the file '<installdir>/rvm/.rvmrc' and change the property 'rvm_project_rvmrc=0' to 'rvm_project_rvmrc=1'. The result will be similar to:
export rvm_project_rvmrc=1 export rvm_bin_path="/home/bitrock/rubystack-3.2.3-0/rvm/bin" export rvm_usr_path="/home/bitrock/rubystack-3.2.3-0/common" export rvm_patch_names="osx-arch-fix" export rvm_path="/home/bitrock/rubystack-3.2.3-0/rvm" export rvm_prefix="/home/bitrock/rubystack-3.2.3-0"
After enabling this feature, when entering a directory with a .rvmrc, you will receive a warning message asking for permissions to use the '.rvmrc':
==================================================================================== = NOTICE = ==================================================================================== = RVM has encountered a new or modified .rvmrc file in the current directory = = This is a shell script and therefore may contain any shell commands. = = = = Examine the contents of this file carefully to be sure the contents are = = safe before trusting it! ( Choose v[iew] below to view the contents ) = ==================================================================================== Do you wish to trust this .rvmrc file? (/home/bitrock/rubystack-3.2.3-0/rvm/.rvmrc) y[es], n[o], v[iew], c[ancel]>
If you know the contents of the file and that is safe, you can accept. You will receive this message for every time a new .rvmrc file is found or when one of the know .rmrc files changed.
Now you can cd into your project directory and use rvm to create the sample '.rvmrc' file. For example, if you want to use Ruby 1.9.3 with the gemset 'bitrock-sample':
cd /home/bitrock/projects/rubystack-3.2.3-0 rvm --rvmrc --create 1.9.3@bitrock-sample
Now if you leave and reenter the directory you will get prompted with the above mentioned dialog and the configured environment will be loaded. You can also manually trust the new .rvmrc executing:
rvm rvmrc trust

Comments