How to deploy your Rails app with Capistrano (to Dreamhost)

January 2011

I’ve been doing a bit of Rails development recently. I’ve actually been doing what I would call ‘basic’ Rails development for a couple of years now on and off.

However, I’d never used Capistrano to deploy a web app before. It was always one of those other things that I would have to learn. But recently, with the re-launched TweetNI, I decided to sit down and figure it out.

It didn’t go quite as smoothly as I had planned so here’s a quick overview to help you get up and running.

What is Capistrano?

Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH. — GitHub

Sounds a bit complicated but in a nutshell, you run a command (or two) via Terminal (or Command Line) and your Rails app is ‘automagically’ up and running on your server.

If you’ve ever deployed a Rails app manually like I have before, you might have uploaded the files via FTP, then created your database in PHPMyAdmin, then ‘touched’ the restart.txt file via SSH (yep, so old skool).

Capistrano combines all this into a couple of commands.

Setup Capistrano

Assuming you have Terminal open on a mac, run the following commands:

1. Install the gem
gem install capistrano
2. Tell your app you want to deploy with Capistrano
cd your_app_folder
capify .
3. Configure deploy.rb

There is a new file for you to configure under /config/deploy.rb

I’m going to assume you’re not using SVN or Git for this project (it just so happens I didn’t use them for this project).

Copy and paste the following:

require 'bundler/capistrano'

set :application, "tweetni.com" # Your application location on your server goes here

default_run_options[:pty] = true

set :repository,  "."
set :scm, :none
set :deploy_via, :copy

set :checkout, 'export'

set :user, 'username' # Your username goes here
set :use_sudo, false
set :domain, 'tweetni.com' # Your domain goes here
set :applicationdir, "/home/#{user}/#{application}"
set :deploy_to, applicationdir

role :web, domain                 
role :app, domain                          
role :db,  domain, :primary => true 


set :chmod755, "app config db lib public vendor script script/* public/disp*"

namespace :deploy do
  
  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
  end
  
end

Make sure you change your app folder, username and domain to your settings.

4. Setup your server
cap deploy:setup

You only need to run this command once.

Each time you deploy, SSH will ask for your password.

5. Deploy
cap deploy:cold
cap deploy:web:enable
cap deploy:migrations
You're up and running

If everything went to plan you should be up and running.

From now on any time you make any changes you just need to run the following:

cap deploy

On Dreamhost

I don’t know if I did something wrong along the process but after step 5 I couldn’t get my app to run so I sent Dreamhost a little support question and they replied saying:

Sorry about that, I needed to create a symlink to bundle. Give it a try now and write back in if you're still having an issue.

Not sure if this happens every time but try giving support a shout if it does.

Hope this is helpful to someone.

Further reading

Receive more design content like this to your inbox

I promise not to spam you. No more than one email per week.

I blog and share links about web design, user experience, product development, HTML/CSS, email design and anything else I find interesting.

No thanks