Use Chef to Fast Track Your Remote Rails Environment Setup

In this tutorial, we will walk through the process of setting up Ruby on Rails server deployment with Chef – in order to automate remote server configuration. Chef is a tool that allows us to manage configurations, similar to Puppet, but it’s considerably more popular as it’s written in Ruby. This tool can help you manage your infrastructure dependencies, create folder structures (with ‘knife’) and bootstrap the entire system. It can even update configurations with just a few commands.

Step 1: Ruby, Ruby Version Manager and Gemset

Supposedly, you already have some Ruby Version Manager (RVM or rbenv most likely) and Ruby. The Ruby version used in this tutorial is Ruby 2.2.1. It is recommended that you create a separate gemset for the Chef configuration. In RVM, you can do this using the following command:

This will separate your Chef gems from the other gems, which can save you from the trouble later. If everything went right, you should have RVM, Ruby and the new gemset on your local machine. To display the new gemset on the list, run the following command:

Step 2: Knife, Chef and Berkshelf Gems

The workbench is prepared in the previous step. To start creating your Chef repo, few tools are required.

At first, you will need to create the Gemfile where the Ruby version, gemset (that we want to use) and all the needed gems will be specified. Start by creating a directory for the repo. You can name it in your own way, for example:

Step 3: Copy SSH Key to the Target Server

Assuming that you already have the ssh-key setup, we will skip the process of creating a new ssh-key here. Start by copying and pasting your ssh-key to the target server:

Note: This will copy the public key of your default identity (use “” for other identities) to the remote host.

Step 4: Creating a Chef Repo

At first, you need to initialize a Chef repo. Go to your console, make sure that you are in the  directory and execute the following command:

Now, let’s see how the Chef repo structure looks; your  should have the following content:

To add cookbooks, open Berksfile. It should be populated with:

This set will be sufficient for setting up the Rails environment. With the cookbooks in place, it is the time to set up some roles.

Leave a Reply

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