Posted by Vince Wadhwani on Nov 16, 2007

Update: If you prefer Apache, use this tutorial instead.
Update 2: If you are on Ubuntu 8.04 LTS (Hardy Heron), check out this HowTo.

This is a follow-up article to the HowTo I wrote for installing Rails on Feisty Fawn. Most of the same tricks and methodologies from that HowTo in Feisty still apply to Gutsy so be sure to check out that article as well if you have problems. There are some different apt-get commands and the organization of the document is a bit different so if you're unsure you may as well start here. My thanks go to all the great developers and bloggers who have helped me along the way. Let's get started!

Step 1. Uncomment all your repositories in /etc/apt/sources.list. This is what mine looks like:

deb http://us.archive.ubuntu.com/ubuntu/ gutsy main restricted
deb-src http://us.archive.ubuntu.com/ubuntu/ gutsy main restricted

deb http://us.archive.ubuntu.com/ubuntu/ gutsy-updates main restricted
deb-src http://us.archive.ubuntu.com/ubuntu/ gutsy-updates main restricted

deb http://us.archive.ubuntu.com/ubuntu/ gutsy universe
deb-src http://us.archive.ubuntu.com/ubuntu/ gutsy universe

deb http://us.archive.ubuntu.com/ubuntu/ gutsy multiverse
deb-src http://us.archive.ubuntu.com/ubuntu/ gutsy multiverse

deb http://us.archive.ubuntu.com/ubuntu/ gutsy-backports main restricted universe multiverse
deb-src http://us.archive.ubuntu.com/ubuntu/ gutsy-backports main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu gutsy-security main restricted
deb-src http://security.ubuntu.com/ubuntu gutsy-security main restricted
deb http://security.ubuntu.com/ubuntu gutsy-security universe
deb-src http://security.ubuntu.com/ubuntu gutsy-security universe
deb http://security.ubuntu.com/ubuntu gutsy-security multiverse
deb-src http://security.ubuntu.com/ubuntu gutsy-security multiverse                                                                      

Step.2 Update your copy of Gutsy:

sudo apt-get update
sudo apt-get dist-upgrade

Step 3: We'll be installing some software that needs to be built so we'll need to get packages required for compiling. In one swoop, you can type this command and get everything you need:

sudo apt-get install build-essential

Step 4: Once you've got the tools, it's time to grab MySQL and Ruby. Just copy and paste this command into your terminal if you're in a hurry:

sudo apt-get install ruby ri rdoc mysql-server libmysql-ruby ruby1.8-dev irb1.8 libdbd-mysql-perl libdbi-perl libmysql-ruby1.8 libmysqlclient15off libnet-daemon-perl libplrpc-perl libreadline-ruby1.8 libruby1.8 mysql-client-5.0 mysql-common mysql-server-5.0 rdoc1.8 ri1.8 ruby1.8 irb libopenssl-ruby libopenssl-ruby1.8

The above includes files necessary for irb (i.e. ruby script/console) and also some ssl libraries needed if you use akismet. It doesn't hurt to have them installed but if you are picky about disk space feel free to omit them.

If you hadn't previously installed MySQL you'll be asked to set a root password. Don't forget what you choose!

Step 5: Grab the latest ruby gems and install them:

wget http://rubyforge.org/frs/download.php/20989/rubygems-0.9.4.tgz
tar xvzf rubygems-0.9.4.tgz
cd rubygems-0.9.4
sudo ruby setup.rb

Once it's done you can remove the .tgz file and erase the rubygems-0.9.4 directory too.

Step 6: Install ruby on rails!!! Note that when the next version of Ruby Gems is released (0.9.5 or whatever), you won't have to use the --include-dependencies. It should be the default (yay!) But for now, we'll have to copy or type a few extra bytes-worth:

sudo gem install rails --include-dependencies

By now veterans know that when you run this the first time you'll get an error that says:

Bulk updating Gem source index for: http://gems.rubyforge.org
ERROR: While executing gem ... (Gem::GemNotFoundException)
Could not find rails (> 0) in any repository

Solution? Run it again. No idea why it always craps out and frankly it's getting old but maybe someday it'll get fixed..

Step 7: Let's install mongrel now that we've got ruby gems and rails working. Future versions of Ruby Gems will also be able to detect your platform. Since we're still on 0.9.4 let's choose the right version for the job. In this example we're installing on Debian or Ubuntu Gutsy so choose the option for ruby. By the way - to Zed Shaw: if you ever read this, thank you!

sudo gem install mongrel --include-dependencies
Select which gem to install for your platform (i486-linux)
 1. mongrel 1.1.1 (java)
 2. mongrel 1.1.1 (ruby)
 3. mongrel 1.1.1 (mswin32)
 4. mongrel 1.1 (java)
 5. mongrel 1.1 (ruby)
 6. mongrel 1.1 (mswin32)
 7. Skip this gem
 8. Cancel installation
> 2

At this point you have a complete rails stack ready for local development on Gutsy, Sidux, or Debian. If you want to get things going for a server (install a webserver, php, etc) then read on. Otherwise, you, my friend, are done!

Step 8: Still reading? Ok, let's get nginx! Why nginx? Because people smarter than me really like it. It's also never let me down on my production server.

sudo apt-get install libpcre3 nginx libfcgi-dev libfcgi-ruby1.8 libfcgi0c2

Step 9: Like the previous HowTo's I wrote, this one will install PHP5 too. There's usually a handy PHP app that you want to install but if not feel free to skip this step. In this example I'll be grabbing phpmyadmin but feel free to ignore that too.

sudo apt-get install libxml2 ucf php5-common php5-cgi php5-mysql libltdl3 libmcrypt4 php5-mcrypt phpmyadmin

When it asks you what webserver to pre-configure leave everything blank. We're using nginx not apache this time. (Apache 2 with Ruby on Rails is covered here.)

Step 10: Let's install and configure mongrel cluster. This is a lengthy set of tasks each of which is important so please pay special attention and seek help in your favorite forums if you aren't familiar with linux. For new users: tab-autocomplete is your friend.

sudo gem install mongrel_cluster --include-dependencies

Copy the init file over to /etc/init.d/ by typing this all on one line:

sudo cp /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/resources/mongrel_cluster /etc/init.d/mongrel_cluster

You may need to change the above command if a version other than mongrel_cluster-1.0.5 was installed.

Next, add a path statement to mongrel_cluster file just above the CONF_DIR variable:

sudo vi /etc/init.d/mongrel_cluster
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local:/usr/local/sbin:/usr/local/bin
Thanks to Russ Brooks for the above tip. You may also want to change the USER=mongrel to USER=www-data but I'll leave that up to you.

Let's modify permissions and make sure we boot mongrel on startup:

sudo chmod +x /etc/init.d/mongrel_cluster
sudo update-rc.d mongrel_cluster defaults

We're not quite done yet -- we need to setup the mongrel cluster correctly [source]. This assumes your primary rails app is in /var/www/myrailsapp. Modify the below line according to your setup.

sudo mongrel_rails cluster::configure -e production \ -p 8000 -N 3 -c /var/www/myrailsapp -a 127.0.0.1 \ --user www-data --group www-data

Now let's create a symlink to that file from within /etc where all our configs live:

sudo mkdir /etc/mongrel_cluster
cd /etc/mongrel_cluster/
sudo ln -s /var/www/myrailsapp/config/mongrel_cluster.yml

You can download a sample mongrel_cluster file HERE. Regardless, I think it's a good idea to download it and compare it to what the above command produced.

Step 11: IF you are installing PHP5 as part of this HowTo, then download this script and put it into /var/www/phpmyadmin If your install didn't create /var/www/phpmyadmin, run this command from the /var/www/ directory:

sudo ln -s /usr/share/phpmyadmin/ 

Inspiration for this script came from Alexey N. Kovyrin. It has been modified only for Ubuntu's PHP path. Don't forget to put it into /var/www/phpmyadmin In order to start this script automatically each time your server reboots (power failures happen!) run this command from your /etc/init.d/ directory:

sudo ln -s /var/www/phpmyadmin/fastcgi_script
sudo chmod +x fastcgi_script
sudo update-rc.d fastcgi_script defaults

Step 12: We're *almost* done. Next step is to configure Nginx. Here's a sample nginx.conf file for your /etc/nginx/ folder. It's set up to handle one rails app and phpmyadmin. Adding additional servers just means more server blocks. I strongly suggest backing up your original nginx.conf file before copying this over.

The last step is to turn everything off and then turn it on again:

PHP: . /var/www/phpmyadmin/fastcgi_script
Mongrel (stop): sudo /etc/init.d/mongrel_cluster stop
Mongrel (start): sudo /etc/init.d/mongrel_cluster start
Nginx (stop):sudo /etc/init.d/nginx stop
Nginx (start): sudo /etc/init.d/nginx start

Troubleshooting: If your rails app fails to start, make sure your database settings are correct in your config/database.yml file. Next also be sure that your rails app has the correct permissions. You can do that by running sudo chown -R www-data:www-data /var/www/railsapp This command may change depending on how you set up nginx and mongrel!

That should be it. I always seem to miss some small detail or another so feel free to leave a comment if I overlooked or skipped a step. Happy coding!