Ruby rbenv

rbenv is for managing multiple Ruby versions and here are some of its commonly used commands summarized for quick reference.

Install rbenv itself and setup

$ brew install rbenv
# follow the printed instruction and add below entry to ~/.bash_profile
$ rbenv init
$ eval "$(rbenv init -)"

Install/Uninstall ruby versions

# list all available versions
$ rbenv install -l
# install a specific version
$ rbenv install <version>
# show the current version in use
$ rbenv version
# list all installed versions (can also check ~/.rbenv/versions/)
$ rbenv versions
# uninstall a specific version
$ rbenv uninstall <version>

Set/Unset ruby versions

# will add a .ruby-version file into current directory
# with version name to override the global version
$ rbenv local <version>
# unset and remove the .ruby-version file
$ rbenv local --unset

# will write to ~/.rbenv/version as global version but
# it can be overriden by the local one if set above
$ rbenv global <version>

# show the current local ruby version used
$ rbenv local
# show the current global ruby version used
$ rbenv global

# set local version to current system Ruby ($PATH)
$ rbenv local system
# set global version to current system Ruby ($PATH)
$ rbenv global system

# set RBENV_VERSION env to override both local/global version set above
$ rbenv shell <version>
# unset the RBENV_VERSION env
$ rbenv shell --unset

Run executables with selected ruby version (by .ruby-version or global)

  • Help doc
$ rbenv help exec
Usage: rbenv exec <command> [arg1 arg2...]

Runs an executable by first preparing PATH so that the selected Ruby
version's `bin' directory is at the front.

For example, if the currently selected Ruby version is 1.9.3-p327:
  rbenv exec bundle install

is equivalent to:
  PATH="$RBENV_ROOT/versions/1.9.3-p327/bin:$PATH" bundle install
  • Install gem to selected ruby version
$ rbenv exec gem install rails

See official docs for more.