Deploy Ruby On Rails Application Swiftly With Capistrano

Hi everybody,

In this post, I will talk about the  Capistrano  deploy tool on Ruby On Rails and I will share some code blocks that is a bash script for to start the server from zero level. I am using Ubuntu-14.04 server. Capistrano version is 3.4. Ruby version on my server is 2.2.3. Capistrano is a open source project, if you want to look the source code of it you can visit the github page. I am using DigitalOcean for server.  The servers that are price $10  are enough to run Ruby On Rails application. You can prepare your server within 2-3 hours with the following bash scripts. Those bash scripts prepare basic environment for ruby libraries and create deploy user for to use on deployment process.  Also one of them prepares ruby environment under the deploy(default deploy user name you can change it  before to run scripts) user home folder.




Base installation bash script for ruby environment is as follows. You must run this script as a root user.

  Deploy user creating script is as follows. You must run this script as a root user too. 


This script  is preparing ruby environment for deploy user. You should run this script as a deploy user.


Your server is ready to run Ruby On Rails application  with those scripts.

Now I will talk about deployment process for simple application.
Ruby On Rails as you know written with ruby programming language,  developing with MVC(Model-View-Controller) open source web framework and it includes principles like DRY(Don’t Repeat Yourself), CoC(Convetion over configuration) .

If you want to create rails application with swiftly, you should look the Cybele ruby gem. This gem provides useful gem list and creates same pages for using in every application like user register, user login, update login and profile info, admin login. Thus you don’t repeat yourself on every new project. You can look the template Gemfile of Cybele gem from github account.  Some useful deploy gems are on this file.
If you create project  rails new project_name  command or if you want to deploy project that is already initialized, my deploy commands is in this recipes_matic gem, you should look that.

I will show deploy steps using with cybele gem.

  1. Create project $ cybele project_name
  2. Edit deploy repo on this file /config/deploy.rb
     set :repo_url, ''
  3. Edit production deploy settings on this file /config/deploy/production.rb
    server "", user: "#{fetch(:local_user)}", roles: %w{app db web}, primary: true, port: 22
    set :rails_env, 'production' 
    set :branch, 'master' 
    set :project_domain, “” 
  4. Edit staging deploy settings on this file /config/deploy/staging.rb
    server "", user: "#{fetch(:local_user)}", roles: %w{app db web}, primary: true, port: 22 
    set :rails_env, 'staging' 
    set :branch, 'develop' 
    set :project_domain, “” 
  5.  Edit your_email address for to get info about the occurred errors


    config.middleware.use ExceptionNotification::Rack,
    :email => {
      :email_prefix => "[project_name]",
      :sender_address => %{"Notifier" <>},
      :exception_recipients => %w{}
  6. Edit yout SMTP settings on those files
  7. If you have private repo on github, run this commands in order for to access repo from server
    $ eval `ssh-agent -s`
    $ ssh-add
  8. Let’s deploy our application with capistrano,
    Check production server is ready to deployment
    $ bundle exec cap production deploy:check
    Setup nginx, postgresql, unicorn, backup(backup gem for to get database backup before deploy) for production server.
    $ bundle exec cap production deploy:prepare
    Do deploy to production server.
    $ bundle exec cap production deploy


Example Capfile file under project root directory

# Load DSL and set up stages
require 'capistrano/setup'

# Include default deployment tasks
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/bundler'
require 'sshkit/sudo'
require 'capistrano/maintenance'

# Include tasks from other gems included in your Gemfile
# For documentation on these, see for example:
# require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
# require 'capistrano/bundler'
# require 'capistrano/rails/assets'
# require 'capistrano/rails/migrations'
# require 'capistrano/passenger'

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

Example deploy.rb file under project config directory

# config valid only for current version of Capistrano
lock '3.4.0'

set :application, 'appname'
set :local_user, 'deploy'
set :stages, %w(staging production)
set :default_stage, 'production'
set :repo_url, "{fetch(:application)}.git"

# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp

# Default deploy_to directory is /var/www/blog2
set :deploy_to, "/home/#{fetch(:local_user)}/apps/#{fetch(:application)}"

# Default value for :scm is :git
set :scm, :git

# Default value for :format is :pretty
# set :format, :pretty

# Default value for :log_level is :debug
# set :log_level, :debug

# Default value for :pty is false
set :pty, true

# Default value for :linked_files is []
set :linked_files, fetch(:linked_files, []).push('config/database.yml')

# Default value for linked_dirs is []
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/upload', 'public/images', 'public/seat_images')

# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
set :default_env, { path: '$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH' }

# Default value for keep_releases is 5
# set :keep_releases, 5

# Look our recipes
load 'config/deploy/recipes/base.rb'

I hope it is a useful post for you.

See you next post..

Leave a Reply