Capistrano – Server Automation and Deployment Tool

01 / Apr / 2014 by ravi 0 comments

What it is ?

Capistrano is an open source tool for running scripts on multiple servers; its main use is deploying web applications. It automates the process of making a new version of an application available on one or more web servers, including supporting tasks such as changing databases.
It’s written in Ruby, but it can easily be used to deploy any language. If your application or the framework has any special deployment requirements, Capistrano can be easily extended to accommodate any such tasks.

Why use it ?

So here are some use cases that may inspire you to use it :

  • Capistrano becomes quite handy while Automating a distributed environment.
  • You can easily deploy your application in parallel on multiple machines without any downtime.
  • It can also perform database related tasks associated with the release deployment.
  • It can also integrate with SCM like GIT and SVN to deploy the pull code from the your repository
  • Due to its clean directory structure , reverting to any previous release becomes quick and easy.
  • You can also perform pre and post-deploy functions like restarting a webserver, purging cache, renaming files and so on.
  • It can perform all the tasks in a single transaction, such that if any step fails during the deployment all the changes done will be rolled back automatically.

How to use it ?

In order to install Capistrano you will need Ruby and RubyGems installed on your computer, you can use this reference to learn how to install Ruby and RubyGems.

Then run the follwing command :

gem install capistrano-ext

Preparing for Project Deployment

Navigate to your application’s root directory in Terminal and run the following command:

capify .

This will create a file called capfile in your project and add default deployment recipe at config/deploy.rb in your project. Delete everything from the deploy.rb file.

How to create a Capistrano Recipe

In your empty deploy.rb file, let’s enter the name of your app :

set :application, “your_app_name”

Now lets add a Git repository :

set :scm, :git
set :repository, ""
set :scm_passphrase, ""

Lets add the user that will ssh into the servers

set :user, "server-user-name"

In this example we’ll deploy to Pre-Production and Production environment. For that we’ll require mutistage at the top of the deploy.rb file :

require 'capistrano/ext/multistage'

Then specify your environments, or “stages”:

set :stages, ["pre-prod", "production"]
set :default_stage, "pre-prod"

Next, create a directory deploy inside your app’s config directory, and then add production.rb and pre-prod.rb files to it. You’ll need per file per stage and also make sure that you naming the files same.
Now let’s populate our production.rb settings:

server "IP_ADDR-OR-FQND", :app, :web
set :deploy_to, "/var/www/my_WebApp"

And then staging.rb:

server "IP_ADDR-OR-FQND", :app, :web
set :deploy_to, "/var/www/my_WebApp-preProd"

Validating your recipe

Capistrano needs to create it’s directory structure on the server in the deploy_to directory. Run the following command from the root of your application :

cap deploy:setup

If something goes wrong with the permission or the SSH, you’ll get an error.

Now before we do actual deployment make sure everything is setup correctly , to test that run the following command :

cap deploy:check


Once you have verified and tested everything , run the following command :

 cap deploy

To deploy to any specific stage/environment , suppose production, run the following command :

cap production deploy

Further Reading

Idea of this blog was to give a brief introduction to Capistrano , in order to use it fully on a production scale, we suggest you to read the official Wiki of Capistrano.


Leave a comment -