Deploying Ruby on Rails 3.1 on Ubuntu

Posted: December 27th, 2011     Reminders to myself   Web Development

This serves as a reminder for me on getting Ruby on Rails 3.1.3 running on Ubuntu (tested on 10.01 LTS).

Install git using apt-get:

sudo apt-get install git-svn

Install build dependencies:

sudo apt-get install gcc g++ build-essential libssl-dev libreadline5-dev zlib1g-dev linux-headers-generic libsqlite3-dev

Install rvm (Ruby Version Manager)

bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )

Put RVM into .bash_rc file so that you can run it easily:

echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_rc

Reload bash settings:

source ~/.bash_rc

That should install rvm. Now you can use it to install Ruby:

rvm install 1.9.2

Make sure to switch into the correct version of Ruby:

rvm use 1.9.2

Install Rails:

gem install rails

This takes a while….

Install Apache:

sudo apt-get install apache2

Install Phusion Passenger:

gem install passenger
passenger-install-apache2-module

Follow the instructions on screen.

Setup Apache virtual hosting the way you normally do. Make the DocumentRoot the rails public directory.

<VirtualHost *:80>
        ServerName ec2-107-21-154-173.compute-1.amazonaws.com
        RailsEnv production
        DocumentRoot /home/ubuntu/depot/public
        <Directory /home/ubuntu/depot/public>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
 
</VirtualHost>

When deploying an app make sure to run bundle install to make sure all the dependencies are installed. This includes mysql, etc.

Rails 3.1 needs a Javascript runtime.

Add this to your gemfile:

gem 'execjs'
gem 'therubyracer'

Run:

bundle install

Make sure to setup your DB using:

rake db:setup RAILS_ENV="production"

Restart apache:

sudo service apache2 restart

Load your browser and visit the site. Hopefully it works, if it doesn’t, check logs/production.log and trace from there.

Asset Pipeline Gotchas

The new asset pipeline in Rails may cause your app to fail.

When you deploy an app, make sure that you precompile the assets using:

bundle exec rake assets:precompile

If you are including css files that are not in your application.css manifest, you need to manually include them in your config/environments/production.rb:

config.assets.precompile += ['960.css']

Note: After recompiling assets you will need to restart your rails app. Do this either by restarting apache or creating/modifying “tmp/restart.txt” in your rails app. Passenger will look at the timestamp for restart.txt and restart rails accordingly.

 
Leonard Teo
CEO, Ballistiq
Montreal, Canada