Moritz Schepp

Setup rbenv with passenger on Ubuntu 14.04

The following proved to be a convenient setup in the past. For my own future reference and anyone who wants to setup ConedaKOR or similar RackĀ applications, I collected the setup steps for

and make them work together nicely. Basically, this involves installing some dependencies, rbenv itself and then integrating that with apache and passenger which we can both install via apt-get:

Installing basic dependencies

This can be done with a well known

apt-get install autoconf bison build-essential libssl-dev \
  libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev \
  libffi-dev libgdbm3 libgdbm-dev git-core apache2

Installing rbenv

This involves checking out a few git repos from github and configuring shell environment variables. Also, we are going to install rbenv globally for all users (though only root can add ruby versions and global gems. That is no problem since we will be using bundler with deployment-specific bundles). Let’s first clone everything we need:

git clone https://github.com/sstephenson/rbenv.git /opt/rbenv
git clone https://github.com/rbenv/ruby-build.git \
  /opt/rbenv/plugins/ruby-build
git clone https://github.com/sstephenson/rbenv-gem-rehash.git \
  /opt/rbenv/plugins/rbenv-gem-rehash

This will fetch rbenv itself and the two plugins ruby-build (to compile ruby versions for us) and rbenv-gem-rehash (for symlinking new gem binaries). Now we will add the file /etc/profile.d/rbenv.sh with contents

export RBENV_ROOT="/opt/rbenv"
export PATH="/opt/rbenv/bin:$PATH"
eval "$(rbenv init -)"

This will load rbenv whenever a new interactive shell is started. Additionally, we need to change /etc/environment to include RBENV_ROOT and the PATH should be prefixed with the rbenv executable directories, for example:

PATH="/opt/rbenv/shims:/opt/rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
RBENV_ROOT="/opt/rbenv"

Make sure you restart your shell now by logging out of the server and logging back in again. Then we can start installing rubies and gems, for example:

rbenv install 2.2.3
rbenv shell 2.2.3
gem install bundler

This should provide the basics for running every ruby version we like.

Integrating with passenger on apache

The passenger apache module allows us to use rbenv so that every rack application can choose its own ruby interpreter. However, this requires passenger >= 4.0.0. Although a recent enough version is available from within the official ubuntu 14.04 package repository, I experienced frequent errors with that version. I recommend you follow the installation instructions on the Phusion webpage.

Then, within /etc/apache2/mods-enabled/passenger.conf, modify the PassengerDefaultRuby to use rbenv:

PassengerDefaultRuby /opt/rbenv/shims/ruby

Now restart apache and you’re done:

service apache2 restart

Now, every application can choose the specific ruby version to use by having a file .ruby-version in the application root directory where it defines the version (see also the rbenv local command).

Let me know if there is anything I can add or that might be confusing. I’m grateful for all feedback.