Skip to content
Menu
Railshero
  • Blog
  • About Me
  • Contact Me
  • Privacy Policy
Railshero
railshero.pw deploying with capistrano

Deploying With Capistrano

Posted on June 29, 2023June 30, 2023 by Rails Hero
Let’s walk through the journey of deploying a Rails application employing the power of Capistrano, a robust tool designed to automate tasks across one or more remote servers. Capistrano is a preferred choice to untangle complicated deployment procedures like updates, migrations, and asset compilation. Let’s assume that you already have a Rails app that you want to deploy, and SSH access to a server, where you’ll deploy the app.

Prerequisites

  • A functioning Ruby on Rails app.
  • A server where you’ll deploy the app. Here, we’ll use an Ubuntu server.
  • SSH access to your server, which we’ll address as your_server_ip for the sake of simplicity..

Getting Started

Setting up Capistrano

First, add the Capistrano gem to your Gemfile:

group :development do
gem 'capistrano', require: false
gem 'capistrano-rvm', require: false
gem 'capistrano-rails', require: false
gem 'capistrano-bundler', require: false
gem 'capistrano3-puma', require: false
end

Now, run bundler:

bundle install

After the gems are installed, run the Capistrano installer:

cap install

This will create several files in your project:

  • Capfile
  • config/deploy.rb
  • config/deploy/production.rb
  • config/deploy/staging.rb

Capfile

Your Capfile should look like this:

# Load DSL and set up stages
require "capistrano/setup"
require "capistrano/deploy"

require "capistrano/scm/git"
install_plugin 'Capistrano::SCM::Git'

require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/puma'

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

deploy.rb

Next, configure your config/deploy.rb file:

set :application, "my_app_name" # the name of your app
set :repo_url, "git@example.com:me/my_repo.git" # the URL of your git repository

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

# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, "/var/www/#{fetch(:application)}"

# Default value for :format is :airbrussh.
# set :format, :airbrussh

# You can configure the Airbrussh format using :format_options.
# These are the defaults.
# set :format_options, command_output: true, log_file: "log/capistrano.log", color: :auto, truncate: :auto

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

# Default value for :linked_files is []
# append :linked_files, "config/database.yml"

# Default value for linked_dirs is []
# append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system"

# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }

# Default value for local_user is ENV['USER']
# set :local_user, -> { `git config user.name`.chomp }

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

# Uncomment the following to require manually verifying the host key before first deploy.
# set :ssh_options, verify_host_key: :secure

# Puma settings
set :puma_init_active_record, true

production.rb

Now configure the config/deploy/production.rb file:

server 'your_server_ip', user: 'deploy', roles: %w{app db web}

Replace 'your_server_ip' with the IP address of your server, where you will be deploying the app. And replace 'deploy' with the user that you want Capistrano to use when logging into the server.

Preparing the Server

Before you can deploy your app, you need to prepare your server. This includes creating a deploy user, installing necessary software & dependencies, then finally setting up the database.

Create a Deploy User

SSH into your server and create a new user (we’ll use deploy as an example):

sudo adduser deploy

Then, add the deploy user to the sudo group:

sudo usermod -aG sudo deploy

Installing Necessary Software

To install Ruby, Node.js, Yarn, and Rails; run the following command

sudo apt-get update
sudo apt-get install curl gpg -y
\curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm install ruby
rvm --default use ruby
ruby -v

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs

npm install --global yarn

gem install rails

Setting Up the Database

You can install any database of your choice. But for simplicity’s sake, here we’ll use PostgreSQL. To install PostgreSQL, run the following command:

sudo apt-get install postgresql postgresql-contrib libpq-dev

Then, create a new PostgreSQL user:

sudo -u postgres createuser -s deploy

Nginx and Puma

To install Nginx, run the following command:

sudo apt-get install nginx

Now, go back in your local project, add the ‘puma’ gem to your Gemfile:

gem 'puma'

Then, run bundler

bundle install

Deploying Your Application

First Time Deployment

With everything set up, you can now deploy your application for the first time:

cap production deploy

The process of deployment may take some time, but upon completion, your application should be up and running.

Subsequent Deployments

For subsequent deployments, all you need to do is run the following command and capistrano will take care of the rest.

cap production deploy

Rollbacks

If an issue arises, you can effortlessly rollback to the previous version:

cap production deploy:rollback

Conclusion

Congratulations! You’ve successfully deployed a Ruby on Rails application using Capistrano. Although this article covers the fundamental aspects of deploying a Rails application with Capistrano, there’s much more to explore. As you gain comfort with the tool, you can begin to investigate more advanced features, like automating database backups, executing custom scripts on the server, and beyond. Happy deploying!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tags

active record active storage assets assign to many associations attachment attachments authentication authorization avatar bootstrap cdn config database deploy deployments devise DRY environment variables file uploads Gemfile gems has_many helpers heroku index indexing initializer javascript pagination parameters postgres production public routes.rb ruby gem ruby gems search sendgrid server smtp stylesheets variants views voting

Recent Posts

  • Understanding the DRY Principle in Rails
  • Building Multi-tenant Applications with Rails
  • Rails Basics: Templating Engines
  • Deploying With Capistrano
  • Automated Testing in Rails

Archives

  • July 2023
  • June 2023
  • October 2021
  • September 2021
  • August 2021
  • July 2021

Categories

  • Active Record
  • Activity Logging
  • Apps
  • Assets
  • Attachments
  • Audio
  • Authentication
  • Authorization
  • Deployments
  • Error Pages
  • File Uploads
  • General
  • Heroku
  • Heroku
  • Pagination
  • Rails Basics
  • RubyGems
  • Search Engine Optimization
  • Search/Indexing
  • Testing
  • User Interface
  • Video
  • Views & Templating
  • Voting
  • Web Security
©2025 Railshero | Theme: Wordly by SuperbThemes
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT