[Ubuntu, RVM, Ruby] RVM – installation, usage, general conception

Hi,

Today I’ll try to explain what really is RVM, when and is it really needed.

RVM(Ruby Version Manager) is a script to manage versions of Ruby, RubyGems and gemsets. Slowly – step by step – let’s imagine that we are first day in new job(Ror developer) and on the welcome we get two projects which we need to get working on our local machine with just installed Ubuntu. Usually it end that first application works ok but second need for example new gem rails and they can’t use one gem by first and second by second. It’s very simple example but situation can be much more complicated – each application need other version of RubyGems(that was my case) to work or even each need other version of Ruby.

After this short entry i will write how to install RVM and explain the way how it’s work, his hierarchy and describe usage – let’s do this:
First step is get packages that RVM use and other to install it – write in command line:

sudo apt-get install curl git-core ruby

now as is written in official RVM instruction the simplest way to receive it is download it by command line:

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

After few seconds it should write a lot of text with thanks in the end. Yes RVM is already installed but when we write in console “RVM” it will returns that “no command RVM found”. The reason of this is that path to RVM wasn’t added to system variable $PATH(which holds all path to applications available from the console). Adding application to PATH simply add the path to application to it. Let’s check actually wrote PATH(watch out – variable name is case sensitive):

echo $PATH

To add path to RVM(/home/user_name/.rvm/bin) we need to edit .bashrc file located in our user’s home dir(/home/user_name/). We need to find line:

[ -z "$PS1" ] && return

and change it to:

if [[ -n "$PS1" ]]; then

and on the end of file add 2 lines:

if [[ -s $HOME/.rvm/scripts/rvm ]] ; then source $HOME/.rvm/scripts/rvm ; fi
fi

Now we need to restart bash configs only by typing:

. ~/.bashrc

Now we can check what’s is written to $PATH variable by typing:

echo $PATH

In the end of output we should see something like that: /home/user_name/.rvm/bin – if not – try to check if the editing of file went ok.
We can check the version of RVM by typing:

rvm -v

We should see the info about actually installed version of RVM. Congratulations – now when You have your own installed rvm we can go further and learn how to make him work for us.

This is the concept of how the RVM is working – it’s not so complicated – simply I don’t have art talent :) . Image shows the situation which I have already on my computer. I’ve installed some ruby and gems. This solution(left side of picture – OS without RVM is horrible) was do it job until I don’t have to install second Rails application which required other ruby version to work. Let’s better think now of hierarchy on right side which shows us structure of RVM’s. At first level we have RVM script, in it we have 2 environments(second level), in each one I’ve created 2 gemset’s for two projects. Gemset is a virtual space where application runs. We can install there gems required by application(many application can use the same gemset but this is a bad practice and they would need to have the same gem requirements). Of course we can install only one RVM but the number of rubies version installed and gemsets inside them is countless.

OK enough theory – let’s practice, but before let’s make some guidelines. Create two directories, which contains fictional applications named A and B. Application A need to work on ruby version 1.9.2, application B need ruby 1.8.7 – we need to configure RVM environment to work well with two application in the same time – so let’s begin!

To check the list of installed ruby’s versions inside the RVM write in console:

rvm list

we should see header “rvm rubies” and nothing more – that means that we don’t install any ruby yet inside RVM – it’s time to do that – at first ruby for A environment:

rvm install 1.9.2

after longer moment(script will download, configure, compile and install – it can take a while) we should receive information that “Install of ruby-1.9.2-p180 – #complete”. We can check is it true by inserting in console:

rvm list

which this time returns a list something like that:
rvm rubies
ruby-1.9.2-p180 [ i386 ]
We have already installed some part of environment for application A, now it’s time to do the same with application B. Repeat previous command now for version 1.8.7:

rvm install 1.8.7

after a while we should receive: “Install of ruby-1.8.7-p334 – #complete”.
In console write:

rvm list

You should get:
rvm rubies
ruby-1.8.7-p334 [ i386 ]
ruby-1.9.2-p180 [ i386 ]
(in You have other numbers after “p” char, You don’t need to worry this is only version of actually installed patch)
At this moment we have installed two versions of ruby for two different environments, it’s a time to install each individual gems but I prefer to create new gemset for each new project – if it’s complicated I’ll try to explain that in the moment.

We have Ruby 1.9.2 environment for application A. Let’s say that we started third application which runs on 1.9.2 version of ruby – if we installed gems on global gemset we have a problem. In that case we should create gemset for each application inside ruby and RVM. To help figure it out please in console:

rvm use 1.9.2

We get answer „Using /home/user_name/.rvm/gems/ruby-1.9.2-p180″ so as the image “says” we are actually on level 2 inside ruby 1.9.2(this would be true until we close console or change ruby version).
To receive gemsets list inside actually used ruby version write:

 rvm gemset list

execution of this command should echo:
gemsets for ruby-1.9.2-p180 (found in /home/nazwa_uzytkownika/.rvm/gems/ruby-1.9.2-p180)
global
So all gems that we would install in that moment will be install inside global gemset – that’s a bad practise. The better way – as I wrote before – is to create gemset named as project codename. We can do this by writing:

rvm gemset create "application_A"

we’ll receive information that gemset was successfully created – to check is it true write:

rvm gemset list

Function should return list that contains “application_A” gemset:
gemsets for ruby-1.9.2-p180 (found in /home/user_name/.rvm/gems/ruby-1.9.2-p180)
aplikacja_1
global
To go one step further to “level 3″ we need to start using gemset like:

rvm gemset use application_A

We receive information that we use this gemset so we can start to install gems – by simply writing:

gem install rails

ATTENTION: NEVER use sudo because it will cause errors.
To uninstall gem simply write:

gem uninstall rails

but this command won’t uninstall dependencies that was installed with gem(all gems with the same version number as rails for example activesupport). If we want to delete whole gemset simply write:

rvm delete application_B

Now result of command:

rvm list

won’t contain application_A gemset because it was deleted.

After short time we should realise that changing programming environments(rubies, gemsets) is at least annoying. We can simply change it by shorter command in which we can define ruby version and gemset name that we want to use:

rvm use 1.9.2@application_A

the result should look like this:
Using /home/user_name/.rvm/gems/ruby-1.8.7-p334 with gemset application_A
We can go one step further and dump the responsibility for version changing on OS. Let’s say that we have directories structure like that:
web
–abc
–xyz
Application A use gemset abc1.1 and it’s files are located in abc directory(it uses ruby 1.8.7 version). Application B uses gemset xyz2.7 and file are located in xyz directory(it uses ruby 1.9.2 version). The goal is that when we enter by console to application directory we want to OS change for us the actually used ruby and gemset version. To achieve that we create .rvmrc file inside directory abc and write inside it:

rvm use 1.8.7@abc1.1

Save it and close the editor. Now after first entering the directory the warning should appear, we need to write “yes” – yes from now every time when user enter to that directory OS will change version of ruby and gemset. To be clear in .rvmrc file in xyz directory we write:

rvm use 1.9.2@xyz2.7

Save it, close editor and enjoy of using the power of RVM :)


I would be thankful if You leave comment if you like it :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>