Rails Deployment with Capistrano 3

April 20, 2015

In this post you can find informations about the deployment process of a Rails application with a self hosted git repository using Capistrano 3 with Rails and RVM on the server side.

First of all you need a server with a rails app.
There are many tutorials on the web for example:
Rails setup in production

After this you have to make a folder for your Rails app and one folder for the git repository on the server side. Make sure to do this as the user that deploys your application. The git repository on the server side is optional, you can also use a github repository or something similar, for your whole workflow.

Now you have to init a bare git repository inside your created repo folder.
if you are on mac install ssh-copy-id via homebrew first

You can find a step by step guide for the creation of the remote git repository here:
remote git repository

On your local machine you have to add capistrano to your "Gemfile"

Afterwards make a bundle install in your console

which generates the following output:
mkdir -p config/deploy
create config/deploy.rb
create config/deploy/staging.rb
create config/deploy/production.rb
mkdir -p lib/capistrano/tasks
create Capfile
Capified

In the generated Capfile, located in the root directory of your application, uncomment the following lines:

Fill out your config/deploy/production.rb with the following.

Then fill out your config/deploy.rb.

Create your linked files in the root directory of your application on your server.

Don't forget to create your production database for your application.

If you are using rvm make sure that you created a .rvmrc into your shared folder with your preferred settings. The following line creates the gemset rails4_2 and a .rvmrc file for ruby2.0.0 and the rails4_2 gemset. For further informations visit http://rvm.io. And don't forget to add .rvmrc to the line with "set :linked_files" in your deploy.rb.

Now lets push the latest changes of your application to the remote repository

Finally deploy your app via the following Capistrano 3 command.

At this moment you have a ready to use application on your server, but it is still not up and running. You can now test if the server is able to start with the following command. The -p flag is optional and sets the port to a other port than the default port of 3000.

With the line above you started your application with Webrick which is a development server and not very common for a production environment. Now you should decide which application-server you want to use. There are some opportunities like thin, unicorn, passenger, puma just to name a view. They all have their up- and downsides and you should decide on the behaviour of your application.
Some information about the comparison of application-servers you can find under: https://blog.engineyard.com/2014/ruby-app-server-arena-pt1

In the next post you will learn how to automatically start thin from the previous Capistrano 3 deployment setup.