Ruby, Sass and Compass in Ubuntu 14/04 the right way with RBENV and Bundler

Submitted by wilbur on Wed, 09/10/2014 - 4:16pm

Last time around, we installed Ruby and gems from the command line in Ubuntu. That approach turns out to be unmanageable when you have multiple projects which all require multiple versions of Gems. Here's how to get it right with RBENV and Bundler, which manage your Ruby and Gem installs.

Prep

First, get your machine straight by uninstalling Ruby, and your Gems. Do what you need to, but get rid of it.

Now install a PPA to get the latest version of Node.js

sudo apt-add-repository -y ppa:chris-lea/node.js

We need to be sure weh have python properties installed to help you manage the repositories

RBENV and Ruby

Update your .bashrc to evaluate and set your path location to RBENV, add this to the top of the file

export RBENV_ROOT="${HOME}/.rbenv"

if [ -d "${RBENV_ROOT}" ]; then

export PATH="${RBENV_ROOT}/bin:${PATH}"

eval "$(rbenv init -)"

fi

Next, reload the shell.

source ~/.bashrc

We now use the installer tool to update any missing dependencies. Ubuntu will go get what it needs to make this work.

rbenv bootstrap-ubuntu-12-04

YES, it looks like it's meant for 12.04, but that's just the name of the script. We are now ready to install Ruby and Gems!

Let's install Ruby! But what version? Find a list of the latest versions with this

rbenv install --list

As of 9/10/14 the latest stable build is 2.1.2, so install it now

rbenv install 2.1.2

rbenv rehash

(Grab a beer or cup o' coffee, this will take a while)

Then set this version as the global version to be used by default

rbenv global 2.1.2

Bundler

Bundler is the brains of the operations, it sets up config file with a list of required gems for our project, and manages downloading/installing/using the right gems - by project! So let's install this gem:

gem install bundler

If your project comes with a gemfile, you are all ready! Just let bundler install:

Project files

We can use RBENV to create a local .ruby-version file for each project. Check this in with your code, to tell rbenv which ruby version we need for our project, and MANAGE that version for us! Run this in your project folder

rbenv local 2.1.2

We can do the same thing with Bundler, this creates a gemfile, and gemfile.lock, that you can add to your project repo.