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:
- Consistency between development and production environments
- Ability to use the very latest versions of Ruby
- Simplicity versus other version managers
- No special setup needed to run Rails or use Capistrano
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.
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:
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
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.
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.
Now log in as
deployer and run:
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer | bash
This rbenv-installer script will install rbenv into
Logged in as
deployer, run these commands:
git clone https://github.com/sstephenson/rbenv-vars.git ~/.rbenv/plugins/rbenv-vars git clone https://github.com/rkh/rbenv-update.git ~/.rbenv/plugins/rbenv-update
if [ -d $HOME/.rbenv ]; then export PATH="$HOME/.rbenv/bin:$PATH" eval "$(rbenv init -)" fi
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:
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
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.
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.
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.