Friday 12 April 2013

rvm for rails tutorial. what is it and why do I need it.. a simple explaination - for mac osx mountain lion

The information below is for mac and linux systems, but can easily be adapted for windows.

There are lots of tutorials out there on how to use rvm, this is another one..

Why? someone asked me to explain how to use rvm, and instead of writing them a long email, I thought I would write it here.. However, this is a bit different, I am concentrating more on the workflow, why would you use it, and how to use it. There are more extensive tutorials out there on actual installation and configuration, which I will provide links to at the bottom of this post, they will provide a nice compliment to this one (or vice versa).

What is RVM? RVM stands for Ruby Environment Manager. While developing with Ruby, you might run into situations where you’d need to use multiple versions of Ruby. maybe you are using Ruby 1.9.2, and someone gives you an old rails project to look in to that uses an older version, and lots of older gems that you perhaps do not want to install in to your main system Ruby installation. Another good reason would be one I used today, you had to look at someone else's code , maybe to help answer a question on stackoverflow, you dont want to install all the gems they require for this project in your system, you only want them to install for this project, and when you are finished looking at it, you would like to be able to remove them alongside the project.

Without RVM, it’s pretty difficult to have more than one version of Ruby on your computer, and impossible to store gems relating to just one project, ruby gems will install by default in your system ruby's installation path..

You may also want to try an alternate Ruby interpreter, like Rubinius or JRuby? How can you take one out for a spin without giving up your faithful MRI install? This is where RVM steps in. It’s basically a framework that handles multiple installations of Ruby for you, and makes it easy for you to switch between them.

So, weve established there are lots of reasons that you may want to install RVM, so lets install it..

1. Install RVM

First, to install rvm, type the following in your terminal.

curl -L | bash -s stable

For more advanced installation choices, see rvm documentation, but I am assuming if you are here reading this, then you wont need those just yet. The command you ran above will create a .rvm dir in your $Home. To check that completed successfully (on a mac), type cd ~ to get to your home directory.

2. Install Ruby Versions for RVM

First thing you need to do after installation, is install some ruby versions that RVM can use. You will most likely have one already installed on your system. RVM will refer to this as your system installation.

To install newer versions, type the commands below in to your terminal:

rvm install 1.9.2

rvm install 1.9.3 (or any version that you want)

**Note, RVM needs to compile the files it downloads. If you are on a mac, you may need to install xcode command line tools, and other gcc libraries, RVM will tell you this if you do need to. If this is the case, the output in the terminal from RVM should also tell you everything you need to know to get this step working. If this is not enough, please refer to the links at the bottom of this page, they will contain full information on how to do this.

3. How to use RVM

Now you have the chance to use different versions of ruby for every project you make, and also install gems relating to just one project (for this example we are using rails projects).

Lets say you are in you home directory (get there by issuing the following command in your terminal: cd ~

First, (and you can actually be anywhere on your system when issuing this command, it wont matter), lets say we are about to create a new rails project called "myrvmtest". We want to use ruby version 1.9.2 for this, and we want to be able to install gemsets against this project only. Every time you issue a command for create or delete with RVM, first you tell it what version of ruby you will be using. So, to switch to version 1.9.2 and create the rvm gemset for myrvmtest, we type the folloiwing two commands in the terminal:

rvm use 1.9.2

rvm gemset create myrvmtest

This has created a new gemset in rvm for our new project that we are about to create.

As a side note, if you are on a mac, a nice GUI interface for rvm is JewelryBox, it is worth installing, but you really should know how to use RVM properly first before using it.

So, we now have a gemset for the new project, lets create the new project. At this stage, this could be a new project, or an existing rails project, the only step that would be different for an existing one, is that you would skip the rails new project step.

Now, create a new rails project: rails new myrvmtest

cd in to that directory: cd myrvmtest

Create a new file named .rvmrc inside the root of this directory touch .rvmrc

What this file will do, is each time you enter this directory, it will use RVM to switch to the appropriate gemset. We tell it to do this, by putting the following line in to the .rvmrc file. Open your favourite text editor, and put this in the file (note, if you cant see the file, it is because it is hidden, all files on mac and linux beginning with a . are hidden files. If you dont know how to view hidden files on your operating system, you should google it to find out)

rvm 1.9.2@myrvmtest

Now, cd back to your home directory cd ~

The cd back in to myrvmtest cd myrvmtest

You should see a message on the screen from RVM, type yes and hit enter.

You are now using the gemset we just created. To check this, and to make sure the gems you are installing are going in to the correct directories, type:

gem env gemdir

this shows where any gems you install are going to live. so if you go to your home dir in terminal and type that, the output should be /Library/Ruby/Gems.1.8 or similar. However, when you traverse back in to a dir with a .rvmrc file, the output should be similar to /Users/username/.rvm/gems/ruby-1.9.2-p32... and you should see that the path is not your system ruby, but a path inside rvm that contains your project name. You can safely be assured now, that if you type

gem install .... the gem will install in this path, nice and clean and easy to remove.

To delete the test projects we made, type the following in your terminal

rvm use 1.9.2

rvm gemset delete myrvmtest

and it will be gone.

To delete the entire rails project, from your home directory, you would type rm -rf myrvmtest

Hope all this was helpful, Paul

useful links