Discover Our Blog

Automating deployment of Ruby application using Capistrano

Automating deployment of Ruby application using Capistrano


Capistrano is an automation tool for executing commands on the server through SSH. Capistrano basically a script which provides various commands to execute which helps to deploy the code on the server. It helps to reduce the manual efforts to deploy code and use the same command to deploy code every time. In this document, I am going to describe how to deploy your application from GitHub to the production server.

Capistrano Flow

The Capistrano have main flow as follows:

1. First, you need to install and configure Capistrano in your application.

2. Once the installation and configuration are done every time you have to push the latest code to GitHub and then run Capistrano script to deploy it to the server.


Step 1: Install the following gems in Gemfile.

gem 'capistrano', require: false

gem 'capistrano-rails', require: false

gem 'capistrano-rvm', require: false

gem 'capistrano-bundler'


Step 2: Run following commands to install these gems and initialize Capistrano

$ bundle install

$ bundle exec cap install


Step 3: Capfile

require 'We need to uncomment some lines according to our requirement. As I want to run bundle install automatically I have uncommented line capistrano/bundler'. In the same way for precompiling assets and running migration require 'capistrano/rails/assets' , require 'capistrano/rails/migrations'. As I am using RVM, I have uncommented the line require "capistrano/rvm". Similarly I am using GitHub for code deployment require "capistrano/scm/git"

, install_plugin Capistrano::SCM:: Git.


 Step 4: config/deploy.rb

deploy. In rb file mention name of the application. Then mention GitHub repository path as I have mentioned ‘’.

Mention username and password of your GitHub repository. We also have to connect server where we have to deploy our application with GitHub. For that, we have to enter ssh public key in settings of GitHub so that it will connect GitHub with the server.


Step 5: config/deploy/production.rb

There are two ways to define servers. First one is with simple role-based syntax and another one is extended server based syntax. You should use only one from both, not both of them. In this, we have to set the environment of our rails application like ‘production’. Mention the branch of your code and also the path of application folder on the server in front of ‘deploy_to’.


(b) production.rb

Step 6: Run script

Now, run following command to run the script.

$ cap deploy production


Step 7: Server Setting

In this tutorial, we simply cloned our application code from GitHub to our server. While first time deployment of code it will use a defined directory structure on the server.

1. ‘release’ folder holds all deployments in a folder. Every time you run Capistrano to deploy code, it makes clones GitHub repository to a new subdirectory inside release.

2. ‘current’ folder is a symlink pointing to the latest release inside the releases folder. This symlink will update at every deployment. If deployment fails in any step the current symlink still points to the old release.

3. ‘repo’ folder holds the copy of GitHub repository.

4. shared’ folder contains the configuration which will not be taken from the GitHub repository. As Capistrano deploy works by cloning the GitHub repository into a release It will contain files like (e.g. config/database.yml and config/secrets.yml). You have to put those files in shared and instruct Capistrano to symlink them to release directory on every deploy. This is done through the linked_files and linked_dirs configuration options in deploy.rb file.

Mention the path to the current folder in configuration settings of the server as it contains the latest code from GitHub repository and then restarts the server.I have mention the path like DocumentRoot /home/path/to/your_rails_app/current/public in /etc/apache2/sites-available/application_name.conf file.


Leave a comment: