Topher Fangio

« VirtualBox Networking (NAT & Bridged) | Main | App Stability »

Random Act of Stupidity #6 / Debian - Rails 3 Setup Guide

Random Act of Stupidity #6

I am so fantastically excited that Ruby on Rails version 3.0 is out that I just had to install and play with it. Turns out, this wasn't such a fantastic idea as I wound up hosing our development/demo server in the process right when we really needed the demo sites to be working. Thanks to Rackspace, a quick restore from the previous night's snapshot got us back up and running with minimal effort and no major loss on our part.

So, the reason this is an RAoS and that it took down our server is because I attempted to upgrade our server to Debian Testing (squeeze). Why did I do this you may ask? Simple, I couldn't install the proper gems that Rails needed without upgrading some of my existing packages. All-in-all, this was a really bad idea, and I have since worked through a much better approach to running Rails 3 on Debian Stable. So, without further ado, I give you the:

Debian - Rails 3 Setup Guide

This guide will walk you through the steps required to get Ruby on Rails setup and running on Debian Stable (lenny). If you find any faults or have any recommendations, please leave a comment and I'll fix/update the guide!

Note: This guide assumes that you have an already working Debian system. It's really easy to setup, and there are tons of guides regarding how to do it, so I'm leaving that out of my guide.

Install RVM (the Ruby Version Manager)

Note: If you're planning to install this on a production or development server where the only real "users" are the different services and many of them need elevated privileges (such as using sudo), you may want to look at the new System Wide Install available for RVM.

RVM is an awesome program that allows you to install multiple version of ruby and have separate gemsets for each version. This is really handy if you are developing multiple Rails applications and need to easily switch between different versions of Ruby. For instance, your Rails 2.3.5 app may need to use Ruby 1.8.7 while your new Rails 3 app requires some of the features of Ruby 1.9.2. RVM allows you to easily do this and to easily switch between the versions of Ruby, all while keeping the gems cleanly separated and installed.

Change to a directory in which you can download and unzip files without causing havoc (~/working is what I generally use) and then run the following commands:

curl > rvm-install-latest
chmod 755 rvm-install-latest
sudo ./rvm-install-latest

Once the install is finished, read the last part of the output as you will most likely need to change some of your bash-specific files to work with RVM. You MUST make these changes if it tells you to, otherwise, you'll get lots of fun errors and spend lots of time trying to figure out why it doesn't work.

Install Ruby

If you already have some version of Ruby installed using apt-get or aptitude, uninstall it now by running:

sudo apt-get remove ruby

Then, use your new installation of RVM to install Ruby 1.9:

rvm install 1.9.2
rvm use 1.9.2 --default

If you need to temporarily switch to another version of Ruby, simply run:

rvm use 1.8

or whatever version you require. Just note that when you open a new terminal window (or console) you'll be back to using Ruby 1.9 since it's set as the default (above).

Install RubyGems

Note: As noted by Chris Roberts in the comments, Ruby 1.9.2 now includes RubyGems by default, so you should be able to skip this step entirely. You can check to make sure RubyGems is properly installed by running gem -v and ensuring that it outputs at least version 1.3.7.

RubyGems is the de facto standard in installing Ruby/Rails-related plugins and functionality; and in fact, is what we will use to install Rails itself in the next step. Read up on how to use gems properly, but the most important fact is that (since we are using RVM) you SHOULD NOT use sudo when installing gems as it will bypass RVM and install them in the incorrect location, causing you all sorts of trouble.

When you're ready, run the following commands to install RubyGems (you may need to install wget first if you don't already have it; just use sudo aptitude install wget):

tar -xvzf rubygems-1.3.7.tgz
cd rubygems-1.3.7
sudo ruby setup.rb

Install Rails

Now, on to the good stuff! Remember, though, DO NOT use sudo to install any gems, including Rails:

gem install rails

Woah! That's it! Seriously?!? Yup. That was really hard. Actually, installing Rails proper isn't difficult at all, it's installing some of the optional dependencies that can be cumbersome (namely SQLite3).

Install SQLite3

Rails can connect to many types of databases, but the most common one for development purposes is SQLite3. It's fast, lightweight, and stores all of it's database stuff in one file that's easy to manage. It's really useful for development, but it doesn't scale very well (all data in one file), so you definitely don't want to use it for production. I would recommend MySQL or PostgreSQL. They are two of the most popular databases, have all of the features you want in a production database (like caching, sharding, clustering, etc.) and both have excellent support from Rails.

Now, Debian Stable usually lags behind the latest and greatest curve because they want everything to be stable. Thus, there are some vulnerabilities in the version of SQLite3 included with Debian Stable and the sqlite3-ruby gem requires at least version 3.6.16 in order to function properly. Using Debian Backports is the preferred method of installing newer packages than what Debian Stable has to offer, so that's what we're going to do. Feel free read the instructions on how to use Backports as I basically copied/pasted what they have on their site.

Open /etc/apt/sources.list and add the following line to the end, then save the file:

deb lenny-backports main contrib non-free

Then, run the following command to make sure the GPG signatures are correct and don't give you warnings when running apt commands:

sudo aptitude update
sudo aptitude install debian-backports-keyring

Finally, you can install SQLite3:

sudo aptitude update
sudo aptitude -t lenny-backports install sqlite3 libsqlite3-dev
gem install sqlite3-ruby

Install Nokogiri (optional)

This last step is optional, but many gems depend upon the excellent Nokogiri gem in order to parse XML or other languages. So, I thought I would include the steps I took to get it installed. Again, you should read the instructions on the Nokogiri website about how to install it under Debian/Ubuntu since I copied/pasted from their site and changed it for Ruby 1.9 (they still list Ruby 1.8).

sudo aptitude install libreadline-ruby1.9 libruby1.9 libopenssl-ruby1.9
sudo aptitude install libxslt-dev libxml2-dev
gem install nokogiri


It took me a little while and a lot of research to work through this, so I hope that this has saved you some time. Again, if you see anything wrong with the guide, please leave a comment and I'll fix it accordingly.

I wanted to take this opportunity to publicly thank the Rails community for creating such an awesome framework. They have put so much time and effort into creating something that is elegant, feature-rich, and that saves all of us enormous amounts of time. I am very grateful for all that they have done and continue to do with Rails. Thanks guys, you're the best.

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (4)

I believe that rubygems are included by default in 1.9.2, and thus your section for installing it should be skipped.

October 18, 2010 | Unregistered CommenterChris Roberts

@Chris Roberts: Thanks for the info, I hadn't heard that yet. I have updated the guide to mention that you should be able to skip the step, but left the instructions there in case anyone has problems or in case it doesn't install by default on their system.

October 18, 2010 | Registered CommenterTopher Fangio

Thank you for this post! I've just installed newest rails on my debian machine:)

November 15, 2011 | Unregistered Commenterregisu

Hey, glad I could help! Thanks for stopping by :-)

November 16, 2011 | Registered CommenterTopher Fangio

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>