Installing Ruby with RVM on Ubuntu

You’ve probably noticed a few articles as of late on SitePoint and its properties discuss Heroku and their offerings; they’re definitely shaking things up in the cloud services arena! In fact, I recently wrote an article myself discussing Heroku’s database-as-a-service offering, Heroku Postgres. But as a PHP developer of close to 10 years now, I stumbled over a frustrating obstacle when I was exploring their Facebook integration offering… I had to install Ruby!

Well, no, it wasn’t quite like that. You see, I didn’t have Ruby installed on my system and so I first tried to install it using my system’s package management utilities. I’m running Ubuntu 11.10, and so I used apt-get. It all seemed to go well, but I encountered problems when trying to use the heroku gem. After researching everything I ended up installing Ruby myself using the wonderful utility, RVM.

I wanted to share the process of installing Ruby on Ubuntu with RVM since I saw several others had experienced similar problems with the heroku gem, and Glenn was nice enough to oblige. If you’re a Ruby guru, I’m sorry… consider this article the ramblings of an unenlightened PHP developer. But if you’re a either a Ruby newbie or have experienced issues with the gem on Ubuntu, hopefully this brief article will be helpful.

Installing RVM

You could go out and install Ruby from source yourself, but I recommend installing it instead using the Ruby Version Manager (RVM). RVM is actually a slick set of scripts which let you install multiple versions of Ruby on your system easily and keeps their environments sandboxed from each other; you don’t have to worry about incompatible gem versions fighting with each other and all the other potential nasties that can happen.

First of all, install the dependencies used by RVM itself (it’s safe to use the default packages for this).

sudo apt-get install curl git-core

Then, install RVM following the instructions on the RVM website. Trust me, you really do want to install it manually instead of using the packaged version. Not all of the dependencies you need later would be installed by the Ubuntu package, there’s some conflicts over libreadline, and I still had problems trying to get things to work after after installing the dependencies with the packaged version.

bash < <(curl -sk https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc
source ~/.bashrc

Pay attention to the additional dependencies listed in the output from the installation. You’ll need to install them in just a moment. But first, a brief explanation of what the above commands do.

curl retrieves the most recent version of the RVM installer script from GitHub. The -s option suppresses the command’s normal output (progress meter, error messages, etc) and the -k option allows curl to establish an insecure HTTPS connection if there was a problem with GitHub’s SSL certificate. Personally, I disagree with the recommendation to use -k so I omitted it when I ran the command on my own system and had no ill effects. The output from the download is then redirected to a Bash shell where the installer script is executed.

RVM is installed locally in your home directory under the hidden directory .rvm. The second command adds a test to your .bashrc file to check if the rvm file which contains important shell commands is available and if so loads them into your terminal session. [[ ]] is a built-in Bash operator that checks whether the expression is true or not, and -s is the test if a file exists. source reads and executes the commands in the file to update your Bash environment.

Installing Ruby

Heroku’s quick-start guide recommends using Ruby 1.9.2, which sounds like a rather nice number so I didn’t argue. Installing multiple versions of Ruby is trivial using RVM. To make sure the compilation goes smoothly, scroll back in your terminal session and look at the list of prerequisite packages that should be installed (it’s probably best to cut and paste the whole lot of names to save yourself from typing errors).

sudo apt-get install build-essential openssl libreadline6 libreadline6-dev zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev autoconf libc6-dev libncurses5-dev automake libtool bison subversion

Now you can use use rvm to download, compile, and install Ruby.

rvm install 1.9.2
rvm use 1.9.2 --default

The first command (rvm install) triggers the download/compile/install process, and installs Ruby and its gems under ~/.rvm, safely away from everything else on your system. The second command (rvm use) sets the 1.9.2 version of Ruby as the active default.

You can verify 1.9.2 is available to you now with the following:

ruby -v

Summary

With the help of RVM I was able to install Ruby quickly and effortlessly when the distro-packaged installation failed me. The nice thing about this approach too is that the Ruby environments are sandboxed so I don’t have to worry about different version trampling over each other. Not only can I have different version gems sitting side-by-side, But I can easily install other Ruby implementations too (MRI, REE, JRuby, etc.).

Truth be told, I wouldn’t mind if PHP had a tool like this, although I think it’d be a bit tricky to manage things given how PHP interacts with servers like Apache.

Once you have RVM installed, you might like to play around with it and see what else it can do to help make managing your environment a bit easier. I stumbled across the How to Use RVM screencast which goes into switching between Ruby environments and then using the .rvmrc file to switch environments based on your working project.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • http://adrien-schuler.com Adrien Schuler

    Hi, thank you for this informative article.
    Is that ok to install RVM if we already have a ruby install (with apt) ?
    You didn’t tell us how you managed that, i guess you removed your packaged install before installing RVM ?
    Additionnal informations on how to do so will be appreciated.

    • http://www.ruprict.net/ Glenn Goodrich

      Adrien,

      RVM doesn’t touch your existing ruby install, but it does label it ‘system’. So, rvm use system will take you back to that ruby. Also, any gems you have installed when you setup rvm will not be used by RVM unless you rvm gemdup system into another ruby.

      Make sense?

      • http://adrien-schuler.com Adrien Schuler

        Thank you, it seems clearer now.

  • http://ididitmyway.heroku.com/ Darren Jones

    Nice article Thomas, thanks.

    Using rvm is definitley the way forward. If you have your home directory in a separate partition it also means you don’t have to go through the whole procedure of installing Ruby each time you update the OS.

    cheers,

    DAZ

  • http://ididitmyway.heroku.com/ Darren Jones

    PS – now you have Ruby installed, how long before you ditch PHP? ;)

    • http://zaemis.blogspot.com Timothy Boronczyk

      Ha ha! Probably not for a while yet, Darren. I’m very happy with my day-job employer and they’re not a Ruby shop, and I’ve been doing PHP for ages so it’s my comfort zone when it comes to personal projects. In fact, I can do PHP in my sleep– which some might say is a nightmare but I prefer to look at it as a convenient way to catch some extra ZZZs on the clock! :) j/k

  • Pavel

    I don’t really get why there’re so many posts on this topic. People know how to “make install”. It would be nice to read about using default package manager to manage rubies and gems for once.

  • Anders

    Because Ubuntu uses Debian Ruby installation, you can find lots of information there.

    Here you have a sort of sum up of what can happen to Ubuntu 12.04. http://www.lucas-nussbaum.net/blog/?p=708

    You can also help with documentation and locating buggs.

    http://wiki.debian.org/Teams/Ruby (info. from the Debian/Ruby team)
    http://wiki.debian.org/Ruby (how to switch versions of Ruby at system level)

  • daBayrus

    Why not use rbenv instead.. It’s way so much better..

    https://github.com/sstephenson/rbenv

  • http://gilgomesp.wordpress.com Gil Gomes

    Really helped me, thanks!