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.
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.
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:
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.