Using – Ruby Version Manager (RVM)


Overview

In this article I’ll be covering some things you’ll need to know about using the Ruby Version Manager tool.  This will be one of those growing articles that will change on a regular basis as I come across new cool things that you should know and that I find very useful.  This article assumes that you have already followed the steps that I outlined in my “Setup – Ruby & RVM” article.  This means that you’ll already have RVM installed as well as Ruby under RVM.

In this article I’ll be covering the following:

  1. What is Ruby Version Manager (RVM)
  2. Installing multiple Ruby Versions
  3. Creating multiple Gem Sets
  4. Using Gem Sets
  5. Setting default RVM
  6. Setting default Gem Set
  7. Switching RVM environment when changing directories
  8. Conclusion

Once you have an understanding on the point mentioned above you should have a good foundation on how to use RVM as well as how to manage your different Ruby environments.

What is Ruby Version Manager (RVM)

Ruby Version Manager (RVM) is a way to create virtual environments where you can mix different Ruby versions with different Gem Sets, where each Gem Set could contain different Gems as well as different version of Gems.  So, if you take a real world scenario where you could have multiple environments for Production, UAT, Development and some others like Integration, you may end up with applications running on different versions of Ruby as well as difference versions of Gems.

RVM gives you a way to not only have all these differences on the same machine, but also a way to manage them.  As a developer you may be asked to fix a production issue on some previous release that had a different version of some Gem file.  Under normal circumstances you would then have to first uninstall your Development environment, install the specific versions that are running on the Production server and only then would you be able to actually run the code and fix the problem.  As most of us know, this tends to happen more often than not.  All of us would in a case like this rather just upgrade the server to the version we’re running, but we also acknowledge that the risks involved in this could sometimes be too big for the company and the products we develop, not to mention the fact that in some cases where the issue is a “show stopper” we simply won’t get the time to run through the whole Release Cycle of launching this new platform.

So, as mentioned above, RVM gives you a boxed environment where you can mix and match different versions of Ruby and Gems together, which means that you have an endless possibility of tests that you can run against an endless possibility of environments.  You won’t have to worry where your tests will pass, seeing that you’ll be able to run them against any and every environment you have on one machine.  On top of this all, you will also be able to upgrade to a new version of Ruby or some Gem, run your tests and if they pass do a check in (this is assuming you doing Test Driven Development).

Now that I’ve done my share of marketing, let’s get to the fun of it J

Installing multiple Ruby Versions

As you might have seen in my “Setup – Ruby & RVM” article, it’s very straightforward and simple to install a specific version of Ruby under RVM.  For this article I’ll be installing two different Ruby versions.  The first one is the same one that we installed in the Setup article and the second one is the previous 1.8.7 version of Ruby.  So, to start I’ll cover installing Ruby version 1.9.2 patch 0 and then I’ll move onto installing Ruby version 1.8.7 patch 330.  So, to do this you simply have to open a Terminal Window and run the following command to install version 1.9.2:

rvm install 1.9.2-p0

To install version 1.8.7 you need to run:

rvm install 1.8.7-p330

Now that you have these two versions of Ruby you may want to switch into their respective RVM environments.  First you should be able to run the following command to see what Rubies you have installed:

rvm list

You should see something like the following when you run this command.

What you’re seeing here is the two different Ruby environments that you installed under RVM.  So, to switch to any one of these two you simply use the following command:

rvm use <environment>

An example of a command you would run for the Ruby version 1.8.7 environment that we installed would be something like:

rvm use ruby-1.8.7-p330

Once in the environment you should be able to run your code or your unit tests.  The next thing you’ll want to do is install Gems.

Creating multiple Gem Sets

In this section we’ll cover how to create a Gem Set and how to install Gems under that Gem Set.  I’m not going to go into the use of Gem Sets seeing that I’ll cover that in the next section, but I will show you how to create a Gem Set and how to install Gems into that Gem Set.  So, let’s say we would like to create a Gem Set for our Test environment.  For this environment I would like to install the following Gems:

bundler (1.0.10)
rack (1.2.1)
rake (0.8.7)

To do this you would want to create a Gem Set with the name “TestGemSet”.  To do this you would first have to switch into the RVM that you would like to have this Gem Set run under.  In my example, I’ll pick the Ruby version 1.9.2 patch 0 environment by running the following command:

rvm use ruby-1.9.2-p0

Now that you’re in this environment you should be able to create the Gem Set by running:

rvm gemset create TestGemSet

You should see a message like the following if it was successful:

‘TestGemSet’ gemset created (/home/jaco/.rvm/gems/ruby-1.9.2-p0@TestGemSet).

Once you created this Gem Set, you would probably want to install the abovementioned Gems into that Gem Set.  To do this you’ll first have to switch onto that Gem Set environment by running the following command:

rvm gemset use TestGemSet

This line tells RVM that you want to use the Ruby version 1.9.2 with the Gem Set “TestGemSet”.  To install the above gems now you can simply run the gem install command as:

gem install bundler -v 1.0.10

This command will install version 1.0.10 of the Gem “Bundler”.  To do it for the rest you should be able to run the following two commands:

gem install rack -v 1.2.1
gem install rake -v 0.8.7

Once you’ve run these commands you should see the list of Gems when you run the following command:

gem list

In case you’re unsure what this should look like, it should look like:

This concludes the section on creating a Gem Set and installing some Gems into it.

Using Gem Sets

As we’ve seen in the previous section you can switch to a Gem Set by running the following command:

rvm gemset use TestGemSet

The question that might come up is; Can we have more than one Gem Set and use them both at the same time.  This would then allow us to group our Gems in a manner that would make it easier for us to manage the versions of the Gems as well as grouping the Gems per project or in which ever manner you would like.  So, in this section we’ll be creating 3 different Gem Sets, each containing some of the same Gems as well as different versions of the some of the non-common Gems.  To start we’ll create the three Gem Sets as follows:

rvm gemset create development
rvm gemset create integration
rvm gemset create production

Now that we have our three Gem Sets, we’ll be adding some Gems by doing the following:

  1. Set up the development Gem Set:
    rvm gemset use development
    gem install sinatra -v 1.2.0
    gem install rake -v 0.8.8
    gem install rack -v 1.2.1
  2. Set up the integration Gem Set:
    rvm gemset use integration
    gem install sinatra -v 1.1.3
    gem install rake -v 0.7.3
    gem install rack -v 1.2.1
  3. Set up the production Gem Set:
    rvm gemset use production
    gem install sinatra -v 1.1.3
    gem install rake -v 0.7.3
    gem install rack -v 1.0.1

Let’s call these Gem Sets the base Sets.  As you can see, these base Sets are more or less the same with the differences of having different versions of the Gems.  This is probably good indication of what the various environments in real life would look like at the start of a project cycle.  The next steps now are to start changing these environments and to add another Gem Set that we would like to use across all the environments.  To do this, we’ll now change the development Gem Set by adding another Gem.  To do this you can run the following:

rvm gemset use development
gem install nokogiri

Add another Gem Set by doing the following:

rvm gemset create sharedgems
rvm gemset use sharedgems
gem install metric_fu

Now when you run the command to switch into any one of these environments, your apps will run against those versions of the Gems.  This helps in the sense that we can now test our code by running it against any one of these environments to ensure that we haven’t broken anything by upgrading certain gems or by adding another gem.  So, it helps us with ensuring backward compatibility as well as running current code running in our Development environment and fixing things that are going wrong in Integration or Production without having to reinstall our Ruby environment.  To use the “sharedgems” Gem Set at the same time as any of our other environment, we can switch to it by using the following command:

rvm use ruby-1.9.2-p0@development@sharedgems

By stringing the various Gem Sets together as show here, your environment will be set up to use the two Gem Sets.  The power of doing these kinds of things is that we can essentially release new products using the currently installed Gem Sets on an environment and adding new Gem Sets without having to worry about breaking current live systems.

Setting default RVM

To set up the default RVM environment you can use the following command:

rvm use ruby-1.9.2-p0 –default

By specifying the –default flag we’re telling RVM that we would like to use this Ruby version as our default.  So, every time we start up a new Terminal, RVM will automatically switch to the default Ruby version.

Setting default Gem Set

To set up the default Gem Set for your RVM environment you can use the following command:

rvm use ruby-1.9.2-p0@<GEMSET> –default

Once again your Terminal will start with the default Ruby version with the added benefit of also switching into your default Gem Set.

Switching RVM environment when changing directories

Here’s a feature that I’m sure most people will like quite a bit.  This allows RVM to switch into a specific RVM environment when your change your directory into a specific directory.  Think of when you have multiple projects, each project having its own Gem requirements as well as Ruby version requirements.  Now, taking this into consideration, you wouldn’t want to switch into the various environments or try and remember which environment goes with which project, not even mentioning the Gem Sets.  To make this easier RVM has a feature where you only have to set this up once.  To do this, you should create a file called “.rvmrc” in your project’s root folder.  You can create this file by running the following command from your project’s root directory:

touch .rvmrc

Now you can edit this file to add the following command to it:

rvm use ruby-1.9.2-p0@<GEMSET> –default

So, this will then ensure that every time you change your directory into this specific folder, RVM will switch your environment for you.  It’s simpler than trying to manage all this yourself, especially if you have a lot of projects at various stages.

Conclusion

As mentioned in the Overview section, this article will grow.  There are a lot of things to learn about RVM and I’m sure I haven’t covered most of them, but seeing that this document is not complete by any means, I’m not too worried about it.  As time goes by and I learn new things, I will add to this and hopefully it will becomes a complete document J

I hope you enjoy RVM and remember that if you use sudo at any time, you’re breaking it J

Advertisement
  1. A User
    October 29, 2011 at 15:41

    Thanks. Just what I was looking for !!!

  1. February 22, 2011 at 07:01

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: