rbenv
Overview
Seamlessly manage your app’s Ruby environment with rbenv. Here is wiki.
Use rbenv to pick a Ruby version for your application and guarantee that your development environment matches production. Put rbenv to work with Bundler for painless Ruby upgrades and bulletproof deployments.
Installation
Compatibility note: rbenv is incompatible with RVM. Please make sure to fully uninstall RVM and remove any references to it from your shell initialization files before installing rbenv.
Using Package Managers
On macOS, we recommend installing rbenv with Homebrew.
Set up rbenv in your shell.
Follow the printed instructions to set up rbenv shell integration.
(e.g. For zsh, you need to add
eval "$(rbenv init - zsh)"to~/.zshrc)Close your Terminal window and open a new one so your changes take effect.
Verify that rbenv is properly set up using this rbenv-doctor script:
That's it! Installing rbenv includes ruby-build, so now you're ready to install some Ruby versions using
rbenv install.
How rbenv hooks into your shell
Skip this section unless you must know what every line in your shell profile is doing.
rbenv init is the only command that crosses the line of loading extra commands into your shell. Coming from RVM, some of you might be opposed to this idea. Here's what rbenv init actually does:
Sets up your shims path. This is the only requirement for rbenv to function properly. You can do this by hand by prepending
~/.rbenv/shimsto your$PATH.Installs autocompletion. This is entirely optional but pretty useful. Sourcing
~/.rbenv/completions/rbenv.bashwill set that up. There is also a~/.rbenv/completions/rbenv.zshfor Zsh users. (For m1 devices, the path is/opt/homebrew/Cellar/rbenv/<version>/libexec/../completions/rbenv.zsh)Rehashes shims. From time to time you'll need to rebuild your shim files. Doing this automatically makes sure everything is up to date. You can always run
rbenv rehashmanually.Installs the sh dispatcher. This bit is also optional, but allows rbenv and plugins to change variables in your current shell, making commands like
rbenv shellpossible. The sh dispatcher doesn't do anything invasive like overridecdor hack your shell prompt, but if for some reason you needrbenvto be a real script rather than a shell function, you can safely skip it.
Run rbenv init - for yourself to see exactly what happens under the hood.
Installing Ruby versions
The rbenv install command doesn't ship with rbenv out of the box, but is provided by the ruby-build project. If you installed it either as part of GitHub checkout process outlined above or via Homebrew, you should be able to:
Set a Ruby version to finish installation and start using commands rbenv global 2.0.0-p247 or rbenv local 2.0.0-p247
Alternatively to the install command, you can download and compile Ruby manually as a subdirectory of ~/.rbenv/versions/. An entry in that directory can also be a symlink to a Ruby version installed elsewhere on the filesystem. rbenv doesn't care; it will simply treat any entry in the versions/ directory as a separate Ruby version.
Installing Ruby gems
Once you've installed some Ruby versions, you'll want to install gems.
First, ensure that the target version for your project is the one you want by checking rbenv version (see Command Reference). Select another version using rbenv local 2.0.0-p247, for example. Then, proceed to install gems as you normally would:
You don't need sudo to install gems. Typically, the Ruby versions will be installed and writeable by your user. No extra privileges are required to install gems.
Check the location where gems are being installed with gem env:
Uninstalling Ruby versions
As time goes on, Ruby versions you install will accumulate in your ~/.rbenv/versions directory.
To remove old Ruby versions, simply
rm -rfthe directory of the version you want to remove. You can find the directory of a particular Ruby version with therbenv prefixcommand, e.g.rbenv prefix 1.8.7-p357.The ruby-build plugin provides an
rbenv uninstallcommand to automate the removal process.
Uninstalling rbenv
The simplicity of rbenv makes it easy to temporarily disable it, or uninstall from the system.
To disable rbenv managing your Ruby versions, simply remove the
rbenv initline from your shell startup configuration. This will remove rbenv shims directory from PATH, and future invocations likerubywill execute the system Ruby version, as before rbenv.While disabled,
rbenvwill still be accessible on the command line, but your Ruby apps won't be affected by version switching.To completely uninstall rbenv, perform step (1) and then remove its root directory. This will delete all Ruby versions that were installed under
`rbenv root`/versions/directory:If you've installed rbenv using a package manager, as a final step perform the rbenv package removal:
Homebrew:
brew uninstall rbenvDebian, Ubuntu, and their derivatives:
sudo apt purge rbenvArch linux and its derivatives:
sudo pacman -R rbenv
How It Works
At a high level, rbenv intercepts Ruby commands using shim executables injected into your PATH, determines which Ruby version has been specified by your application, and passes your commands along to the correct Ruby installation.
Understanding PATH
When you run a command like ruby or rake, your operating system searches through a list of directories to find an executable file with that name. This list of directories lives in an environment variable called PATH, with each directory in the list separated by a colon:
Directories in PATH are searched from left to right, so a matching executable in a directory at the beginning of the list takes precedence over another one at the end. In this example, the /usr/local/bin directory will be searched first, then /usr/bin, then /bin.
Understanding Shims
rbenv works by inserting a directory of shims at the front of your PATH:
Through a process called rehashing, rbenv maintains shims in that directory to match every Ruby command across every installed version of Ruby: irb, gem, rake, rails, ruby, and so on.
Shims are lightweight executables that simply pass your command along to rbenv. So with rbenv installed, when you run, say, rake, your operating system will do the following:
Search your
PATHfor an executable file namedrakeFind the rbenv shim named
rakeat the beginning of yourPATHRun the shim named
rake, which in turn passes the command along to rbenv
Choosing the Ruby Version
When you execute a shim, rbenv determines which Ruby version to use by reading it from the following sources, in this order:
The
RBENV_VERSIONenvironment variable, if specified. You can use therbenv shellcommand to set this environment variable in your current shell session.The first
.ruby-versionfile found by searching the directory of the script you are executing and each of its parent directories until reaching the root of your filesystem.The first
.ruby-versionfile found by searching the current working directory and each of its parent directories until reaching the root of your filesystem. You can modify the.ruby-versionfile in the current working directory with therbenv localcommand.The global
~/.rbenv/versionfile. You can modify this file using therbenv globalcommand. If the global version file is not present, rbenv assumes you want to use the "system" Ruby, i.e. whatever version would be run if rbenv weren't in your path.
Locating the Ruby Installation
Once rbenv has determined which version of Ruby your application has specified, it passes the command along to the corresponding Ruby installation.
Each Ruby version is installed into its own directory under ~/.rbenv/versions. For example, you might have these versions installed:
~/.rbenv/versions/1.8.7-p371/~/.rbenv/versions/1.9.3-p327/~/.rbenv/versions/jruby-1.7.1/
Version names to rbenv are simply the names of the directories in ~/.rbenv/versions.
Command Reference
Like git, the rbenv command delegates to subcommands based on its first argument. The most common subcommands are:
rbenv local
Sets a local application-specific Ruby version by writing the version name to a .ruby-version file in the current directory. This version overrides the global version, and can be overridden itself by setting the RBENV_VERSION environment variable or with the rbenv shell command.
When run without a version number, rbenv local reports the currently configured local version. You can also unset the local version:
rbenv global
Sets the global version of Ruby to be used in all shells by writing the version name to the ~/.rbenv/version file. This version can be overridden by an application-specific .ruby-version file, or by setting the RBENV_VERSION environment variable.
The special version name system tells rbenv to use the system Ruby (detected by searching your $PATH).
When run without a version number, rbenv global reports the currently configured global version.
rbenv shell
Sets a shell-specific Ruby version by setting the RBENV_VERSION environment variable in your shell. This version overrides application-specific versions and the global version.
When run without a version number, rbenv shell reports the current value of RBENV_VERSION. You can also unset the shell version:
Note that you'll need rbenv's shell integration enabled (step 3 of the installation instructions) in order to use this command. If you prefer not to use shell integration, you may simply set the RBENV_VERSION variable yourself:
rbenv versions
Lists all Ruby versions known to rbenv, and shows an asterisk next to the currently active version.
rbenv version
Displays the currently active Ruby version, along with information on how it was set.
rbenv rehash
Installs shims for all Ruby executables known to rbenv (i.e., ~/.rbenv/versions/*/bin/*). Run this command after you install a new version of Ruby, or install a gem that provides commands.
rbenv which
Displays the full path to the executable that rbenv will invoke when you run the given command.
rbenv whence
Lists all Ruby versions with the given command installed.
Environment variables
You can affect how rbenv operates with the following settings:
RBENV_ROOT
~/.rbenv
Defines the directory under which Ruby versions and shims reside.
(Also see rbenv root)
RBENV_DEBUG
Outputs debug information.
(Also as: rbenv --debug <subcommand>)
RBENV_DIR
$PWD
Directory to start searching for .ruby-version files.
Development
The rbenv source code is hosted on GitHub. It's clean, modular, and easy to understand, even if you're not a shell hacker.
Tests are executed using Bats:
Last updated