rbenv is a ruby version manager. It allows you switch between different versions of ruby.

$ echo $PATH
  /Users/mo/Applications:/Users/mo/.rbenv/shims:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/local/sbin:/Users/mo/.rbenv/bin

rbenv installs the “shims” directory to your path. rehashing updates the shims in that directory to match every ruby command across every installed version of ruby. e.g. irb, gem, rake, rails, ruby.

Each shim will pass the command along to rbenv.

~/.rbenv/shims/bundle

#!/usr/bin/env bash
  set -e
  [ -n "$RBENV_DEBUG" ] && set -x

  program="${0##*/}"
  if [ "$program" = "ruby" ]; then
    for arg; do
      case "$arg" in
      -e* | -- ) break ;;
      */* )
        if [ -f "$arg" ]; then
          export RBENV_DIR="${arg%/*}"
          break
        fi
        ;;
      esac
    done
  fi

  export RBENV_ROOT="/Users/mo/.rbenv"
  exec "/Users/mo/.rbenv/libexec/rbenv" exec "$program" "$@"

When the shim passes execution to rbenv, rbenv will look for the appropriate version or ruby to execute the command.

  1. RBENV_VERSION environment variable
  2. .ruby-version file in current directory, then recurse up to the top of the file system.
  3. ~/.rbenv/version

To rebuild the shims for each version of ruby you can use the rehash command.

$ rbenv rehash

You should do this whenever you install a new ruby or a gem with a command in the bin directory.

installing rubies

rbenv does not install different versions of ruby. It allows you to choose how you want to install the different rubies on your system. rbenv does provide a plugin system to allow third party plugins to interact with rbenv. the ruby-build plugin is an extension that will install different versions of ruby using the familiar install syntax.

$ rbenv install -l
  $ rbenv install 2.1.2
  $ rbenv global 2.1.2

Each version of ruby is installed in ~/.rbenv/versions/. If you want to install ruby yourself you just have to create a symlink from the versions directory to wherever you installed ruby.

rbenv init

The rbenv init command will install ~/.rbenv/shims to your PATH environment variable. It also sources ~/.rbenv/completions/rbenv.bash for autocompletion.

$ rbenv init -
  export RBENV_SHELL=bash
  source '/Users/mo/.rbenv/libexec/../completions/rbenv.bash'
  rbenv rehash 2>/dev/null
  rbenv() {
    local command
    command="$1"
    if [ "$#" -gt 0 ]; then
      shift
    fi

    case "$command" in
    rehash|shell)
      eval "`rbenv "sh-$command" "$@"`";;
    *)
      command rbenv "$command" "$@";;
    esac
  }

rbenv is a light weight unix friendly tool for managing your different rubyies.

comments powered by Disqus