You are currently viewing Use Chef to Fast Track Your Remote Rails Environment Setup

Use Chef to Fast Track Your Remote Rails Environment Setup

  • Post published:May 10, 2017

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:

 $: rvm gemset create chef_article.

To use a newly created gemset, run the following command:

$: rvm gemset use chef_article.

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:

$: rvm gemset list

The new gemset will be marked with an asterisk (*) if it is currently used. The next item you need is a bundler gem. You can easily install it using the following command:

$: gem install bundler

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:

$: mkdir chef_repo && cd $_

Now, with the help of the bundler gem, initialize a Gemfile:

$: bundle init

Edit your Gemfile and then add the following Gems:

gem 'knife-solo', '0.3.0'
gem "chef", "~> 11.10.0"
gem 'chef-zero', '1.7.2'
gem "berkshelf", "~> 2.0.14"

Run the following command:

$: bundle install

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 “-i identity_file” 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 chefrepo_ directory and execute the following command:

$: knife solo init .

Your console will output these lines while creating the repo structure:

WARNING: No knife configuration file found
Creating kitchen...
Creating knife.rb in kitchen...
Creating cupboards...
Setting up Berkshelf...

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

  ├── Berksfile
  ├── cookbooks
  ├── data_bags
  ├── environments
  ├── nodes
  ├── roles
  └── site-cookbooks

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

site :opscode

This is the site where the cookbooks will be downloaded. Now, start populating the Berksfile with cookbook names, and repositories where these cookbooks can be found. Add the following lines just below “site :opscode“:

> cookbook 'users', git: ''
> cookbook 'sudo', git: ''
> cookbook 'rvm', git: ''
> cookbook 'nodejs', git: ''
> cookbook 'postgresql', git: ''
> cookbook 'redisio', git: ''
> cookbook 'nginx', git: ''
> cookbook 'imagemagick', git: ''

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.