Linux‎ > ‎Ruby & Rails‎ > ‎

Install Ruby using RVM

posted Dec 29, 2013, 7:48 PM by Chris Martin   [ updated Apr 5, 2022, 8:05 PM ]
Don't install the ruby packages supplied with your linux distro.  they are usually out of date
Instead, install via the RVM (Ruby Version manager). This will also allow you to install different versions and configurations for different projects.   The beautiful part of this is that it installs RVM and Ruby to our home directory, providing a sandboxed environment just for us.  

First update the app repository and install some prerequisites.
$ sudo apt-get update
$ sudo apt-get install build-essential openssl libreadline8 libreadline-dev curl git zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev libncurses-dev automake libtool bison subversion pkg-config

Now install RVM
$ sudo apt-get install curl
$ curl -L | bash -s stable --auto-dotfiles # This installs RVM

reload our shell environment
$ . ~/.bash_profile

The next problem you will encounter is that ~/.bash_profile is only automatically sourced if you have logged into the shell.  If you are opening a terminal window you are not going to have the functions included automatically.  If you look in  ~/.bash_profile you will see that RVM added the following:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

You will now need to remove this from ~.bash_profile and add it to ~/.bashrc

Install the packages we need:
$ rvm requirements

Install Ruby
$ rvm install 1.9.3

Now tell rvm that we want to use this version 
$ rvm use 1.9.3 --default

Check the version
$ ruby -v
ruby 1.9.3p545 (2014-02-24 revision 45159) [x86_64-linux]

Users can, and should, use a gemset when possible so that they don't pollute their 'default' which is what is selected when a gemset is not specified in either a project's .rvmrc, or at the command-line. Each installed Ruby has a '@global' gemset. This is used to share gems with other gemsets created under that specific Ruby, and with the 'default' gemset. This can be selected by running 'rvm gemset use global' and then installing the gems you wish to share to other gemsets including 'default'. You can, of course simply install in each gemset but this will cause needless duplication and use up more disk-space and bandwidth.

Now that we have installed RVM and a version of Ruby, we can install Rails. Because RVM is installed to our home directory, we don't need to use sudo to install things.  This will install the rails gem and the multitude of gems that it and its dependencies depend on, including Bundler.
$ gem install rails -v 3.2.16

Install the Postgresql gem:
first make sure that postgresql is installed
$ sudo apt-get install postgresql

install the gem:  note the dependency on libpq-dev (for Ubuntu)
$ sudo apt-get install libpq-dev
$ gem install pg

Get a copy of the database and install it in to your own local database (see the posting on postgress backup and restore)

Next you can checkout the source using git (the separate post on setting up git).

To get all the gems loaded in your project change to the root directory of the application and run
bundle install

To run the application in a web server use
rails server