Build and Deploy a Rails VPS, Part 1

Start by provisioning an Ubuntu 14.04 VPS, then install Ruby with rbenv.

In this tutorial I’ll walk you through setting up an Ubuntu 14.04 virtual private server (VPS) with rbenv and Ruby 2.3.0 installed under a deployer user account. Your server will have everything it needs to deploy a simple Rails app. The Rails portion is covered in Part 2: Rails and Capistrano.

Although rbenv’s primary purpose is as a manager and switcher of multiple versions of Ruby, and therefore most useful in development, it can also be used in production. Here’s why I deploy with rbenv:

In this tutorial, commands prefixed with sudo should be run as root (or a user with sudo privleges). All other commands must be run as the deployer user unless otherwise specified.

1 Create an Ubuntu 14.04 VPS

You can use any cloud computing provider that offers Linux instances to provision a VPS running Ubuntu 14.04; for this tutorial I recommend DigitalOcean.

DigitalOcean is nice for quick experiments like these, because it is cheap and the control panel is fast and simple: choose a hostname, select Ubuntu 14.04 x64, and then press “create droplet”. You’ll have root access to a new VPS in about 1 minute!

Here’s a quick walkthrough:

Setting up a new VPS at DigitalOcean (time-lapse). DigitalOcean’s UI has changed a bit since this was recorded.

2 Install packages

Log into your new VPS as root and run:

sudo aptitude update
sudo aptitude install -y \
  curl git-core \
  build-essential libreadline-gplv2-dev tklib nodejs zlib1g-dev \
  libsqlite3-dev libssl-dev libxml2 libxml2-dev libxslt1-dev

You’ll need the basic curl and git packages in order to download the rbenv installer. The others will come in handy later when we compile Ruby and deploy a Rails application.

3 Create a deployer user

sudo adduser deployer

It’s not a good idea to run general-purpose applications as root. Instead, create a deployer user. We’ll install rbenv into this user’s home directory.

You do not need to give sudo privileges to the deployer user.

4 Run the rbenv installer

Now log in as deployer and run:

curl -fsSL | bash

This rbenv-installer script will install rbenv into ~/.rbenv.

5 Install rbenv plugins

Rbenv becomes even more useful with plugins. We’ll take advantage of rbenv-vars in the second part of this tutorial. It’s also nice to have rbenv-update, to make updating rbenv and its plugins easy.

Logged in as deployer, run these commands:

git clone ~/.rbenv/plugins/rbenv-vars
git clone ~/.rbenv/plugins/rbenv-update

6 Modify .bashrc

if [ -d $HOME/.rbenv ]; then
  export PATH="$HOME/.rbenv/bin:$PATH"
  eval "$(rbenv init -)"

Place this snippet at the very top of the deployer’s .bashrc. This ensures that rbenv is always loaded when the deployer user runs commands (like executing a Rails app).

Don’t forget to reload the new .bashrc so that rbenv is loaded in the current shell:

source ~/.bashrc

7 Compile Ruby and activate it

Next, once again logged in as the deployer account, run:

CFLAGS=-O3 rbenv install 2.3.0
rbenv global 2.3.0
gem install bundler

The rbenv install command delegates to ruby-build (which was installed for us by rbenv-installer) to download the Ruby 2.3.0 source code and compile it. This will take 5-10 minutes.

The rbenv global command tells rbenv that we want Ruby 2.3.0 to be the default Ruby version. Whenever we run a Ruby command as the deployer user, rbenv will make sure to execute the Ruby 2.3.0 we just compiled.

Finally, gem install bundler installs Bundler into this new Ruby environment.

That’s it! You now have a VPS with a deployer user that has everything needed to deploy and run a Rails app with Ruby 2.3.0.

Continue with Part 2: Rails and Capistrano.