HowTo: Install Ruby on Rails on Ubuntu Gutsy with Apache2
Focuses of the How-To
My how-to focuses on two specific aspects: 1) Ubuntu Gutsy server edition, and 2) the Ubuntu packages. I will comment on additional things you have to install with the desktop edition. The Ubuntu packages are a not the latest and greatest, typically to allow for stability. If you're looking for the latest and greatest, I will try to point you in the right direction.
Install Server Edition of Ubuntu
The entirety of this how-to focuses on the server edition of Ubuntu Gutsy. I will comment on additional things you have to install if you are working with the desktop edition. Also, this how-to focuses on the Ubuntu way
Toward the end of the server edition install, the installer presents you the options of software package bundles to install. Select the OpenSSH Server and LAMP Server options. For those who don't know, the LAMP server option installs MySQL, Apache 2, Perl, Python and PHP packages. I recommend choosing OpenSSH just because it's such a common need. I love that the server install presents me the option, since it's a common addition to my desktop installation.
If you are working with the desktop edition or did not select the LAMP Server options, install Apache 2 at this point. The package name is apache2.
Once you've done this, you need to get the latest updates. Before I run the commands to do so, I typically comment out the CD repository in /etc/apt/sources.list. It's up to you whether or not to do this. The update commands:
sudo apt-get update
sudo apt-get upgrade
Install Ruby on Rails
Install Ruby
First things first. We need Ruby. Here are the packages you need (you can cut and paste this in the terminal).
sudo apt-get install ruby ri rdoc \
libmysql-ruby ruby1.8-dev irb1.8 \
libmysql-ruby1.8 libreadline-ruby1.8 \
libruby1.8 rdoc1.8 ri1.8 ruby1.8
If you're using the desktop edition, you will also need this list of packages. This includes MySQL and some Perl stuff.
sudo apt-get install mysql-server libmysql-ruby \
libdbd-mysql-perl libdbi-perl libmysql-ruby1.8 \
libmysqlclient15off libnet-daemon-perl libplrpc-perl \
mysql-client-5.0 mysql-common mysql-server-5.0
Install Ruby Gems
In order to install Rails, we need the gems package. I use the Ubuntu package and you can install it using this command:
sudo apt-get install rubygems libgems-ruby1.8 libopenssl-ruby1.8
In Gutsy, the version for rubygems is 0.9.4. You can install a later version by following these steps instead:
wget http://rubyforge.org/frs/download.php/29548/rubygems-1.0.1.tgz
tar xvfz rubygems-1.0.1.tgz
cd rubygems-1.0.1
sudo ruby setup.rb
Ruby Gems 1.0.1 released on 12/20/2007 and was the latest version as of 1/8/2008. Check rubyforge (http://rubyforge.org/projects/rubygems/) for updates and replace the URL and filename accordingly if a newer version is available. I did not completely test this
Install Rails
Now we can install rails. Here's the command with the packages we need the Ubuntu way:
sudo apt-get install liberb-ruby libredcloth-ruby1.8 rails rake
The RoR way (for the latest and greatest):
sudo gem install rails --include-dependencies
If you get the message below, try again. I suspect this occurs because of a time out. When it works, it responds much faster.
ERROR: While executing gem ... (Gem::GemNotFoundException)
Could not find rails (> 0) in any repository
Install FCGI
sudo apt-get install libfcgi-dev libfcgi-ruby1.8 libfcgi0c2 libapache2-mod-fcgid
The libapache2-mod-fcgid package automatically enables mod_fcgi. You can optionally restart Apache now, or wait until later, since we have a couple more things to do. The command is:
sudo /etc/init.d/apache2 force-reload
Setup a test Ruby project
mkdir src
cd src
rails rubytest
cd rubytest
Thanks to Bill Walton and Curt Hibbs and their excellent tutorial Rolling with Ruby on Rails Revisited for some of the next stuff (you might recognize the cookbook app).
Create the database for the ruby test (we want to make everything works):
mysql -u root -p
Tell me you setup a password on the database. Enter it now.
At the mysql> prompt, type these commands:
create database rubytest_development;
grant all on rubytest_development.* to 'rbtest'@'localhost';
exit
Open a text editor and save the following SQL to db/create.sql:
drop table if exists recipes;
drop table if exists categories;
create table categories (
id int not null auto_increment,
name varchar(100) not null default '',
primary key(id)
) engine=InnoDB;
create table recipes (
id int not null auto_increment,
category_id int not null,
title varchar(100) not null default '',
description varchar(255) null,
date date null,
instructions text null,
constraint fk_recipes_categories foreign key (category_id) references categories(id),
primary key(id)
) engine=InnoDB;
Once you've done that, execute the command in MySQL using the following command.
mysql -u rbtest rubytest_development < db/create.sql
Set your user in the config/database.yml. I used rbtest as the user name with no password (per the command above), so I change the connection information this way:
development:
adapter: mysql
database: rubytest_development
username: rbtest
password:
socket: /var/run/mysqld/mysqld.sock
Create scaffolding for the category:
script/generate scaffold category category
Configure Apache to point to your new Ruby project
Create a link in the /var/www folder to your development folder:
sudo ln -s ~/src/rubytest /var/www/rubytest
Set the permissions on the tmp folder in your RoR project directory:
Make the tmp and log folders and it's children group writable with the command:
chmod -R g+w tmp log
Set the group of the tmp and log folders and the children to www-data:
sudo chgrp -R www-data tmp log
Edit the public/.htaccess file and change the following lines:
AddHandler fastcgi-script .fcgi
and
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
to:
AddHandler fcgid-script .fcgi
and
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
respectively.
And before you save, set the Rewrite base to /rubytest to conform to the Apache configuration coming up. Find this line:
# RewriteBase /myrailsapp
and change it to
RewriteBase /rubytest
Edit the default site and add an entry for your test RoR application:
sudo vi /etc/apache2/sites-available/default
Add the following to the bottom of the VirtualHost element:
Alias /rubytest "/var/www/rubytest/public"
<Directory "/var/www/rubytest/public">
Options ExecCGI FollowSymLinks
AddHandler fcgid-script .fcgi
AllowOverride all
Order allow,deny
Allow from all
</Directory>
Enable the rewrite module in Apache and restart it load the module and the alias setting you added above:
sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart
Navigate to http://localhost/rubytest/category. You should get the scaffolding for recipe categories. If you do, great! If you don't, review everything above. If you find an error in my docs, let me know.
More fun
If you're new to RoR, continue with the Rolling with Ruby on Rails Revisited tutorial.
Notes
I may revisit this with a combination Apache2+Mongrel setup, if I continue to play around with RoR. Most of the RoR apps I have are personal projects that will never see the public light of day and become subject to the threading issues of Rails. I think I'm more interested in focusing on Python (pylons or django?).