Home rbenv
Post
Cancel

rbenv

rbenv repo

rbenv is a version manager tool for the Ruby programming language on Unix-like systems. It is useful for switching between multiple Ruby versions on the same machine and for ensuring that each project you are working on always runs on the correct Ruby version.

1
brew install rbenv ruby-build
1
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
1
echo 'eval "$(~/.rbenv/bin/rbenv init - bash)"' >> ~/.zprofile

Install ruby versions

1
2
3
4
# list versions available
rbenv install -l
# install specific ruby version
rbenv install x.x.x
You should not use sudo to install gems. Typically, the Ruby versions will be installed under your home directory and thus writeable by your user. If you get the “you don't have write permissions” error when installing gems, it's likely that your "system" Ruby version is still a global default. Change that with rbenv global <version> and try again.

Display current active ruby version:

1
rbenv version

List all ruby versions know to rbenv:

1
rbenv versions

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.

1
2
3
4
# Sets the local version of ruby 
rbenv local 3.1.2
# Unsets the local ruby version
rbenv local unset

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.

1
2
3
4
# Sets the global version of ruby
rbenv global 3.1.2
# Reports the currently configured ruby version
rbenv global

system version

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.

warning

You should not use sudo to install gems. Typically, the Ruby versions will be installed under your home directory and thus writeable by your user. If you get the “you don’t have write permissions” error when installing gems, it’s likely that your “system” Ruby version is still a global default. Change that with rbenv global <version> and try again.

Check the location where gems are being installed

1
2
gem env home
# => ~/.rbenv/versions/<version>/lib/ruby/gems/...

rbenv shell

1
2
3
4
# Sets the shell-specific ruby version
rbenv shell 3.1.2
# Unsets the shell-specific ruby version
rbenv shell --unset

rbenv rehash

Installs shims for all Ruby executables known to rbenv (~/.rbenv/versions/*/bin/*). Typically you do not need to run this command, as it will run automatically after installing gems.

1
rbenv rehash

rbenv which

Displays the full path to the executable that rbenv will invoke when you run the given command.

1
rbenv which irb

what rbenv init does?

rbenv init is a helper command to bootstrap rbenv into a shell. This helper is part of the recommended installation instructions, but optional, as an advanced user can set up the following tasks manually.

  1. Adds rbenv executable to PATH if necessary.
  2. Prepends ~/.rbenv/shims directory to PATH. This is basically the only requirement for rbenv to function properly.
  3. Installs shell completion for rbenv commands.
  4. Regenerates rbenv shims. If this step slows down your shell startup, you can invoke rbenv init - with the --no-rehash flag.
  5. 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 shell possible.

Q & A

rbenv not changing ruby version

1
echo 'eval "$(rbenv init -)"' >> ~/.zprofile
This post is licensed under CC BY 4.0 by the author.