Saturday, March 31, 2012

Install Rails 3 using RVM on ubuntu


1. Installing RVM

Ruby Version Manager is a great tool for isolating your development environment to each individual project.  This allows you to have specific ruby versions with specific gemsets isolated from the rest of your development environment, virtually eliminating any potential conflicts.  Although not absolutely required to use Rails or Ruby, RVM will greatly reduce headaches with managing ruby.  To learn more about RVM, visit the RVM homepage.
Luckily, RVM provides an install script that we can download and execute with a single command.  This command will download the latest code from the github repository, so you need to have git and curl installed on the system.  If you are using a recent linux distro like Ubuntu 11.10, then you might already have git and curl installed. If not, execute the following command:

>$ sudo apt-get install git curl
Start the RVM installation by running this command:

>$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

Now that the installation completed, we need to add the loader for RVM to our .bash_profile. Open the file in your favorite editor:

>$ gedit .bash_profile
Next, past the following command into the text file:

[[ -s "/home/artem/.rvm/scripts/rvm" ]] && source "/home/artem/.rvm/scripts/rvm"
For the .bash_profile to take effect, we need to restart the bash shell.  Once the bash shell restarts, let’s verify that RVM is properly installed by running the following command:



>$ type rvm | head -1
out put is like this
rvm is a function
Next, let’s check what dependencies we need to install on our platform by running:

>$ rvm requirements
This returns a number of requirements you will need for different ruby compilers. We will use MRI (official) ruby interpreter, so copy and paste the command under that and run it:

>$ sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion

2. Installing Ruby

Now that we got RVM all ready to go, let’s jump right into installing Ruby.  First, let’s see what version of ruby are available to us through RVM:

>$ rvm list known
We can see there are quite a few different ruby installations available:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# MRI Rubies
11.8.6[-p420]
11.8.6-head
11.8.7[-p352]
11.8.7-head
11.9.1-p378
11.9.1[-p431]
11.9.1-head
11.9.2-p180
11.9.2[-p290]
11.9.2-head
11.9.3-preview1
11.9.3-rc1
11.9.3[-p0]
11.9.3-head
ruby-head
# GoRuby
goruby
# JRuby
jruby-1.2.0
...
# Rubinius
rbx-1.0.1
...
# Ruby Enterprise Edition
ree-1.8.6
...
# Kiji
kiji
# MagLev
maglev[-head]
..
# Mac OS X Snow Leopard Only
macruby[-0.10]
...
# IronRuby -- Not implemented yet.
ironruby-0.9.3
...
Let’s start by installing Ruby 1.8.7:

>$ rvm install 1.8.7
This will download and install Ruby 1.8.7.  After installation is complete let’s set RVM to use this Ruby installation:


>$ rvm use 1.8.7@defaultgems --create
Using /home/artem/.rvm/gems/ruby-1.8.7-p352 with gemset defaultgems
You can also set this to be the default version, overriding the system installation of Ruby, by running the following command:


>$ rvm use 1.8.7@defaultgems --default
Using /home/artem/.rvm/gems/ruby-1.8.7-p352 with gemset defaultgems
You can always switch back to the system ruby by running:

>$ rvm use system
3. Installing RubyGems and Rails
Ruby is fully functional now, but we are not done yet.  We want to get Rails set up on this installation, so let’s start by installing RubyGems (ruby package manager).  Start by downloading the setup file from RubyForge, then extract the archive and execute the setup.rb file:

>$ sudo ruby setup.rb
Once the installation finishes, simply execute the following command to install Rails:

>$ gem install rails

4. Starting the first project and configuring the basics

We are finally ready to get to the good part of actually setting up a new project.  Start by navigating to the directory where you want to store rails project and create a new rails project:


>$ rails new Project -T
>$ cd Project
Note the ‘-T’ option; it tells rails not to use Test::Unit.  Instead, we will use RSpec that we will install shortly.
Remember we installed Ruby 1.8.7?  Let’s say for this project we want to use version 1.9.2 instead (because that’s what our server uses), but we only want 1.9.2 to execute for this project AND we want it to that automatically.  Additionally, we want to isolate the gems for this project so that we don’t have any hard to trace conflicts between unused gems.  It’s a tough order, but that’s exactly what RVM is made to do.  We can customize the RVM behavior by placing .rvmrc file inside the root directory of our project:

>$ gedit .rvmrc
Now we need to put one line to load the ruby we want with a gemset that’s specific to the project.  Recall that we wanted to use Ruby 1.9.2, so paste the following:

>$ rvm 1.9.2@project_name
Save the file and exit the directory and enter it again:


>$ cd ..
>$ cd Project
RVM will ask you if you trust this .rvmrc file (and you should! you made it).  Click yes, and it should prompt you with an error that we didn’t install this version of Ruby yet.  Copy and paste the instructions given by the error:


>$ rvm install 1.9.2
>$ rvm gemset create 'project_name'
At this point, we need to reinstall rails for this specific gemset, so run:

>$ gem install rails

No comments:

Post a Comment