The "Postmodern" Ruby environment

Prelude

Like many people who write Ruby I've made the transition from RVM to rbenv and rbenv-build. Recently I've been hearing about an alternative workflow: chruby and ruby-install. After taking some time to experiment with chruby and ruby-install I've decided to switch over permanently (although rbenv-build and ruby-install are not mutually exclusive - more on this later). In this post I'm going to document my reasoning and instructions on getting started.

Why chruby and ruby-install?

So, why are chruby and ruby-install better than rbenv and rbenv-install? Two reasons: simplicity and built-in depdency resolution when building Ruby. chruby is a very small (~90 LOC) shell script that doesn't add hooks to 'cd' and doesn't require binary shims or 'rehashing' after a gem installs a script.

ruby-install integrates with common package managers, such as homebrew or apt-get, and automatically pulls down any depdendencies while compiling ruby. This addresses a huge pain point for me. I can't tell you how many times I've compiled ruby, either from source or via rbenv-build, only to get an error complaining about a missing library or headers such as openssl, sqlite, libxml2 or zlib.

Scope

This post covers getting a Ruby environment configured on Mac OS X (Mavericks) and Debian 7. With minor tweaks these steps should apply to any UNIX. If you're on Mac OSX you can use homebrew to install both chruby and ruby-install

Getting Started

To get started with Debian 7 and derivatives like Ubuntu follow the steps below. Only git-core and build-essential are required since ruby-install should pull down the other depdencies but you may prefer to install them explicitely yourself. Note: these are also the dependencies you'll want to install if compiling Ruby from source.

# Note: this is one line broken up for formatting
> apt-get install git-core build-essential libsqlite3-dev
          libxslt1-dev libxml2-dev zlib1g-dev libssl-dev 
libreadline-dev libyaml-dev

The official chruby instructions suggest downloading a release tarball from GitHub and then running make install but I prefer cloning from GitHub and using git to update (git pull). Since releases are tagged you can list releases by running git tag and then checkout the relase you want to install. I think this is a cleaner approach to managing updates but you may prefer to download the tarball.

> git clone https://github.com/postmodern/chruby.git
# See all releases
> git tag
> git checkout v0.3.8 # Or more recent release
> make
> sudo make install
> source /usr/local/share/chruby/chruby.sh

> git clone https://github.com/postmodern/ruby-install.git # See all releases > git tag > git checkout v0.4.2 # Or more recent release > make > sudo make install

If you're on Mac OS X feel free to clone chruby and ruby-install from GitHub and install them or you can use homebrew.

> brew install chruby ruby-install

Once that is done you can use ruby-install to install Ruby, JRuby or Rubinius

# To see the versions available
> ruby-install

# To build/install specific revisions > ruby-install ruby 1.9.3 > ruby-install ruby 2.0.0

# To list all known rubies > chruby

# To change current Ruby environment > chruby 1.9.3

As I mentioned earlier ruby-install and rbenv-build are not mutually exclusive. Although rbenv-build is positioned as a plugin to rbenv it works stand-alone. Currently rbenv-build supports building/installing more ruby implementations than ruby-install does. If you find your platform of choice is missing you can install rbenv-install and as long as you either install into ~/.rubies/ or set the RUBIES environment variable to include the path where you installed ruby, chruby will be able to manage that ruby as well.

I also use PostgreSQL as my database. The easiest way to install PG on Mac OSX is via homebrew. If you're using Debian the PostgreSQL project hosts an official APT repository with up-to-date packages. You can find more details on the PostgreSQL APT Wiki. To use this repo you'll need to figure out the release codename you're running.

> lsb_release -c
Codename:  wheezy

# As root add apt repository key > wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -

# Create file /etc/apt/sources.list.d/pgdg.list and add the right # repository for your distribution (output from lsb_release -c above) > sudo echo "deb http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main" > /etc/apt/sources.list.d/pgdg.list

# Update package list and install Postgresql 9.3 > sudo apt-get update > sudo apt-get install postgresql-9.3 postgresql-server-dev-9.3 postgresql-client-9.3 postgresql-contrib-9.3

That's it. Hope this helps.