Screencasts

How to Use RVM

Show Notes

In our Ruby developer career there may come a time when we have to deploy our app with other versions of Ruby. We may also take a fancy to implementations such as MacRuby, and want to try them out. However, installing many different versions of Ruby on the same box can be a real headache.

Even when we're not dealing with different versions of Ruby, there can be problems when you have conflicting gems from one project clashing with gems from another. This becomes more apparent when you have Rails 2 & Rails 3 projects in active development, and use gems that are only compatible with Rails 2 or 3.

In this screencast we'll show you deal with these issues in an elegant way by using the simple and powerful RVM: Ruby Version Manager.

Links

What You'll Learn

  • How to install RVM
  • How to install and switch between Ruby implementations and versions
  • How to use the autoloading .rvmrc files for project specific Rubies and gemsets

Script

Hello and welcome to this How to Use RVM screencast.

In our Ruby developer career there may come a time when we have to deploy our app with other implementations of Ruby. We may also take a fancy to implementations like MacRuby, and want to try them out. However, installing many different versions of Ruby, for testing or purely recreational reasons, can be a real headache.

Even when we're not dealing with different implementations and versions of Ruby, there can be headaches when you have conflicts with gems from one project clashing with gems from another. This becomes more apparent when you have Rails 2 projects and new Rails 3 projects in active development, and use gems that are only compatible with Rails 2 or 3.

Enter RVM, the creation of Wayne E. Seguin. RVM stands for Ruby Version Manager. It's a command line tool that takes away the headaches of installing and managing Ruby versions, and implementation and their gems.

Installing RVM

OK, so let's get RVM installed, and then we'll go through the main usage scenarios, and some best practices for managing your projects.

If you visit the RVM homepage at rvm.beginrescueend.com, you'll see a one-liner that you can copy and paste in to your terminal.

bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )

Run this command, and you'll have RVM installed.

But if you type rvm it says "rvm: command not found". That's because there's one more additional step needed.

If we scroll up, we see there's some additional instructions. Depending on how your system is setup, you may need to do some additional tweaks to your .bash_profile or .bashrc files. So be sure to read over these instructions carefully, but since this is a straightforward, fresh install of Snow Leopard, all we need to do is add this line from step one into our .bash_profile.

OK, now that we've saved that, all we need to do is open up a new terminal and type rvm -v, and we see that the latest version is installed.

RVM Usage

To install a new implementation of Ruby, all you need to do is type in rvm install followed by the version number for MRI, or the name of the particular implementation. MRI stands for Matz's Ruby Interpreter. MRI Rubies are the official C implementations of Ruby.

Installing Different Ruby Implementations

So rvm install 1.9.2 will install Ruby 1.9.2 and rvm install jruby will install JRuby.

Switching Between Ruby Implementations

Once they're installed, type ruby -v in your terminal window to see the version currently in use. You'll see it's the default system Ruby, Ruby 1.8.7. If you want to switch to another implementation of Ruby, all you need to type is rvm use followed by the version of Ruby you want to use. So rvm use 1.9.2 switches you to Ruby 1.9.2. Type ruby -v and you'll see that it's changed.

Any gems installed now will be for this version of Ruby and sudo isn't required. If you need to go into IRB for that Ruby implementation, just type irb.

If you want to switch back to the system Ruby, all you need to do is type rvm system.

Project Specific Ruby and Gems

Wouldn't it be great if you could switch to a project directory in your terminal and have the correct Ruby implementation automatically loaded? Wouldn't it be great if once you're in the project directory, you'd know that any gems you install will be in isolation from any other project's gems? Where RVM really comes into its own is with the .rvmrc feature. By placing an .rvmrc file into the root of a project, you can have all of this. And it's only one line of configuration.

OK, let's imagine that we have two projects, one for a client on a shared host that uses Phusion's Ruby Enterprise Edition, and another project for a client who uses JRuby.

In the root of our Ruby Enterprise Edition project, let's create an .rvmrc file, and in that we'll write rvm followed by the Ruby implementation which is, ree, followed by an @ symbol, and then what we choose for the name of our Gemset. We can use any unique name here, but we'll just use the project name since that's a sensible name to use.

rvm ree@ree_project

So in our JRuby project, let's do the same, and create an .rvmrc file, but this time let's include rvm jruby, followed by an @ symbol, and then our project name.

rvm  jruby@jruby_project

OK, so let's go into the terminal and switch directories. Let's go into our Ruby Enterprise Edition project folder.

As you can see, RVM shows a warning the first time you go into this folder. Hit enter, and it shows you the contents of the file. If you're happy and you trust the file (as you should do since you've just written it), type yes and hit return. If you don't have a particular Ruby implementation installed that is required by the .rvmrc file, it asks you to install it, and gives you the command to do so. So let's copy and paste that into our command line.

rvm install ree-1.8.7-2011.03

Once RVM has done it's thing, let's cd out of the project directory, and go back in. As you can see, we're not prompted to install Ruby Enterprise Edition now. We just see that we have to create a gemset. But before we do that, let's just type ruby -v to see if we have it installed correctly. Yep, as you can see, we've got Ruby Enterprise Edition installed.

Now, on to creating the gemset. Once again, you can see that RVM tells you exactly what you need to do to create a gemset. So let's copy and paste the command.

rvm gemset create 'ree_project'

So lets cd out of the directory and back in again, and you see we have no messages.

If we type in gem env gemdir, we can see that the gem path for this project is isolated from the main install of Ruby Enterprise Edition.

Now, to our JRuby project. Just as before, we follow the instructions on screen. So yes, we trust the .rvmrc file. We see that JRuby doesn't need to be installed, as we've done this already. So, all we need to do is create our gemset. Once again, we copy and paste, hit return, and then cd in and out again, and you see no warning messages or errors.

Now let's install a gem to see if our projects are isolated.

So let's install Haml. We'll run gem install haml in our JRuby project directory. And as you can see, Haml is successfully installed.

Now let's go to our Ruby Enterprise Edition project and type haml -v. And as you can see, there is no Haml. Perfect: RVM is working correctly and isolating our projects.

And that's it. We hope you'll consider using RVM when you set up a new ruby project, as it's a really nice way to keep your project environments isolated and organized.

If you found this screencast helpful, you may also be interested in our Homebrew screencast, where we show you how to quickly and securely setup UNIX packages on Mac OS X.

Thanks for watching! Subscribe to our RSS feed, follow us on Twitter, and please leave any comments, questions or suggestions for new screencasts in the comments below. If you like our videos, and think your friends, followers or colleagues would benefit from seeing them, please feel free share via any of the links below the video. We really appreciate your support.

See you next time!

← Latest Episodes

blog comments powered by Disqus