Rbenv: How to Use It To Manage Your Rubies

Reading Time: 3 minutes

While working with ruby, you have some alternatives to manage your binaries using rbenv,
but as you think of them, I hope you are not really considering delegating this
task directly to your OS package manager since it is discouraged, otherwise you
would end up with a messy workstation.

There are a few tools that allow you to manage your rubies and gems, the most
popular among them is, without doubts, RVM
(Ruby Version Manager) which in all fairness is good, it provides a CLI to
switch between your rubies and gemsets. If you haven't used RVM, you should
read this guide.

Even when RVM is great, there are a couple of things that I don't like about it:

  • Personally, I had a painful situation as a Linux user, every time I wanted to
    get a fresh ruby version with support for readline and zlib libraries and its
    dependencies.
  • Its gemset management feature tends to duplicate gems across your projects. I
    get it, sometimes you need to isolate your gems to keep them compatible. But
    there is another player in the field, called bundler. (mentioned down below)
  • Plus that last bullet, some colleages have mentioned having a 5~6 GB .rvm
    folder.

A couple of days ago, I was struggling to track down a gem that I wasn't sure
where exactly came from, anyway I was about to create a new gemset to do a fresh
start when someone adviced me to check out rbenv.

The rbenv way

A highlight in favor of rbenv is that you don't actually need to worry about
maintaing your gemsets, since it relays on bundler
who takes care of all your application dependencies. Letting you care about
just the version of the ruby you want to use globally, locally and in a per
project basis. Let's check out rbenv.

RVM and rbenv aren't friends 🙁

First of all, you better avoid using both in the same environment because they
are incompatible. Don't say I didn't warn you.

Installation

1. Get rid of RVM by running:

$ rvm implode

2. To install rbenv, must be at ~ and clone it:

$ git clone git://github.com/sstephenson/rbenv.git ~/.rbenv

3. Add scope for rbenv binaries to your $PATH

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> .bash_profile
# be sure of do this to your bash source file (e.g. .bashrc, .profile)

4. Setup bash autocompletion:

$ echo 'eval "$(rbenv init -)"' >> .bash_profile

5. Restart your shell.

$ exec

There are two ways of installing rubies with rbenv. From source and make it
into "~/.rbenv/versions/" or the one I prefer using ruby-build

6. Install ruby-build

$ git clone git://github.com/sstephenson/ruby-build.git ~/.ruby-build
$ cd ~/.ruby-build
$ ./install.sh
# you may need to run with sudo, since it installs a binary in /usr/local/bin

7. Install a ruby

Now, we are ready to install a ruby version.

Note: After a couple times trying to get a ruby with readline support
for my irb, I googled and found a way.

For Ubuntu I used my readline path:

$ CONFIGURE_OPTS="--with-readline-dir=/usr/include/readline" rbenv
install 1.9.3-preview1

And there we go, we give it some time, get some coffee or play a ping pong
match. Once rbenv finishes, and every time after installing a ruby
you need to run:

$ rbenv rehash

I have to mention, that it seems tricky, but you can set an alias in
your ~/.bash_profile or export an environment variable.

Usage

Let's supposse you've got some more rubies, now, how do we specify the
version of ruby we want to use:

To setup a global ruby you do something like:

$ rbenv global 1.9.3-preview1

To setup a local (per-project) ruby you do:

$ rbenv local 1.9.2-p290
# this creates a rbenv-version file in the current folder

What version of ruby am I using?

$ rbenv version

What versions of ruby do I have?

$ rbenv versions

Conclusions

There are a couple of things that remain unexplored, but for now this is a great
start. In case you miss the gemset, this
is something you might want to look at.

So, give it a try, you might like it. For now I'm happy with my fresh
rbenv install. Let us know your rbenv experience.

Know more about us!

0 Shares:
You May Also Like