Skip to content

Instantly share code, notes, and snippets.

@kurotoshiro
Forked from MicahElliott/rbenv-howto.md
Created September 22, 2013 09:18
Show Gist options
  • Select an option

  • Save kurotoshiro/6658282 to your computer and use it in GitHub Desktop.

Select an option

Save kurotoshiro/6658282 to your computer and use it in GitHub Desktop.

Setting up and installing rbenv, ruby-build, rubies, rbenv-gemset, and bundler

Documenting the process of moving from RVM to rbenv. Also get some of the cool tools working. This does not include anything about rails. And none of this should require sudo. You’ll probably want to consult the individual project pages at some point to learn more, but this guide should contain every setup instruction needed for all the tools to work.

(Note that this is a dump of the steps I performed in week #1 and is not yet fully cleaned up/ordered/tested. I’ve worked through these steps on Arch and Ubuntu linux.)

This little tutorial enables you to install (ruby-build) and use (rbenv) multiple versions of ruby, isolate project gems (gemsets and/or bundler), and automatically use appropriate combinations of rubies and gems.

The two things to accomplish:

  1. Having at least one reliable locally installed Ruby
  2. Project-specific Gemsets

(Note that I’m using using Zsh in the examples.)

I’ve loved using RVM. It did do a surreptitious thing with my environment (a shell file clobber setting) that I still don’t have a handle on (beyond overriding things with shell functions). And it’s a little too sophisticated to keep in my head everything it’s doing. If rbenv can eliminate any of the magic of environment management, then I’ll give it a go.

(ignore this, NYI) As this is a multi-repo configuration, you might want to consider using git magnet.

Install rbenv

Git it.

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

Enable it.

% echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc
% echo 'eval "$(rbenv init -)"' >> ~/.zshrc  # or equivalent
% exec $SHELL

You haven’t done anything interesting yet. Hang tight.

For more rationale details, consult the rbenv project page.

(skip this) See how it works:

##% which ruby
##/home/mde/.rbenv/shims/ruby
##% <`!!`
##«see that it just runs: rbenv exec ruby …»

Install the related ruby-build ruby installer

Your system should have gcc et al installed already. In Ubuntu this is build-essential and in Arch you’ve already got them, of course.

% mkdir -p ~/.rbenv/plugins
% cd ~/.rbenv/plugins
% git clone git://github.com/sstephenson/ruby-build.git

Now we’re done manually installing things to specific places.

% cd ~/anywhere

Install rubies

For Ubuntu users, make sure you’ve installed the zlib1g-dev apt package before building any rubies (to avoid this issue).

Find out which rubies are available. Wow, you can even tab-complete these:

% rbenv install 1.8.7-p«tab»
1.8.7-p249  1.8.7-p302  1.8.7-p334  1.8.7-p352  1.8.7-p357  1.8.7-p358

Get a few (be patient):

% rbenv install 1.9.3-p125
% rbenv install jruby-1.6.7
% rbenv install rbx-2.0.0  # rubinius; why not

You need to “rehash” to regenerate rbenv’s “shims” after installing a new ruby.

% rbenv rehash

Now do it again for practice:

% rbenv rehash

You’re going to do that almost every time you install a gem to generate the shims. Get used to it or make rbenv install an alias to also rehash.

You’ve probably already got a system ruby installed. See them all now. (Actually, I believe system ruby is not listed here.)

% rbenv versions
  1.8.7-p358
  1.9.3-p125 (set by /home/mde/.rbenv/version)
  jruby-1.6.7
  rbx-2.0.0-dev

Turn one on for persistent “global” use across your system (for your user anyway). Note to RVM users: forget the verb use. global, local, and shell are your new enablers.

% rbenv global jruby-1.6.7
% ruby -ve 'puts "it works"'
jruby 1.6.7 (ruby-1.8.7-p357) (2012-02-22 3e82bc8) (Java HotSpot(TM)…
it works

Quickly switch back and forth in a session with: rbenv shell 1.«tab»

You should know that these are all being installed to your ~/.rbenv/versions dir.

(If you intend to install ruby 1.8.7, you might need this workaround.)

Enable RVM-style “gemsets” (optional)

You can skip this section if you want to just always install gems directly into your projects.

Use the rbenv “plugin” subtool rbenv-gemset.

% cd ~/.rbenv/plugins
% git clone git://github.com/jamis/rbenv-gemset.git

You should now have a new gemset command in rbenv:

% rbenv gemset «tab»
active   create   delete   file     list     version

% rbenv gemset create 1.9.3-p125 helloset

(Note that tab-completion is not yet working for the gemset command.)

That set up a directory for you in ~/.rbenv/versions/1.9.3-p125/gemsets/helloset. But now it’s up to you to do the other half manually: setting the name of your gemset. You need to create a tiny file, in this case containing helloset.

% >.rbenv-gemsets
helloset

Now when you gem install (see “Bundler” section below) something it will go to your helloset gem area.

You can combine use of multiple gemsets with it.

The big benefit to this setup is enabling each project to use a different set of differently versioned gems.

Set up to use a gemset in a project

% cd $TMPDIR/hello

% >.rbenv-gemsets
helloset

Set up Bundler for dependency management

Bundler is the tool for managing your project dependencies. In fact, we’ll (almost) never even install a gem directly, without Bundler.

NOTE: Although the tool is called “Bundler,” its command is actually bundle. This has caused some confusion. You’ll actually be fine whether you install the bundle or bundler gem, but the command will alwyas be bundle.

Now that you’re working with multiple rubies, every ruby will need its own bundler.

Now, what gem do we actually have?

% whence -av gem
gem is /home/mde/.rbenv/shims/gem
gem is /usr/bin/gem

That didn’t make it too clear. Every ruby actually provides its own gem command (er, maybe not 1.8?). Here’s how to see it:

% rbenv which gem
/home/mde/.rbenv/versions/1.9.3-p125/bin/gem

And what Bundler?

% rbenv which bundle
rbenv: bundle: command not found

Hmm. Now we can see that we need to install it manually. So this is bootstrapping; it’ll be the only time we need to use gem directly for installing. (bundler is also a good global gemset install; see below.)

% gem install bundler
…

% rbenv which bundle
/home/mde/.rbenv/versions/1.9.3-p125/bin/bundle

Great. That was quick and easy.

% which -a bundle
bundle is /home/mde/.rbenv/shims/bundle
bundle is /usr/bin/bundle

Now do it for all your rubies (and every time you install another ruby).

Global gemsets

Like RVM, rbenv can do global gemsets, too (though I haven’t tried it yet).

Install stuff for your project (finally)

% >Gemfile
source "http://rubygems.org"
gem "sinatra", "1.3.2"
gem "compass", "0.12.1"

% bundle install

Typical workflow (demo)

% cd ~/proj/foo
% rbenv shell 1.9.3-p125
% rbenv local «tab»
1.8.7-p358  1.9.3-p125   jruby-1.6.7   rbx-2.0.0-dev  system   --unset
% rbenv local jruby-1.6.7

Set a local persistent version.

% rbenv local rbx-2.0.0-dev
% cat .rbenv-version
rbx-2.0.0-dev

Speeding things up a tad

rbenv is a handful to be typing often. I presently use:

% alias rb=rbenv

though r and b are terribly distant to type on QWERTY.

Upgrading

Eventually new rubies will release and you’ll want to keep up. This means moving your installed gems along with them.

FIXME: I HAVEN'T DONE THIS YET

Set up an informative rbenv-aware prompt (optional)

http://wiki.iany.me/rbenv/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment