Groom your app’s Ruby environment with rbenv.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.
Powerful in development. Specify your app's Ruby version once, in a single file. Keep all your teammates on the same page. No headaches running apps on different versions of Ruby. Just Works™ from the command line and with app servers like Pow. Override the Ruby version anytime: just set an environment variable.
Rock-solid in production. Your application's executables are its interface with ops. With rbenv and Bundler binstubs you'll never again need to
cdin a cron job or Chef recipe to ensure you've selected the right runtime. The Ruby version dependency lives in one place—your app—so upgrades and rollbacks are atomic, even when you switch versions.
One thing well. rbenv is concerned solely with switching Ruby versions. It's simple and predictable. A rich plugin ecosystem lets you tailor it to suit your needs. Compile your own Ruby versions, or use the ruby-build plugin to automate the process. Specify per-application environment variables with rbenv-vars. See more plugins on the wiki.
Why choose rbenv over RVM?
- How It Works
- Command Reference
- Environment variables
PATH, determines which Ruby version has been specified by your application, and passes your commands along to the correct Ruby installation.
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:
PATHare 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/bindirectory will be searched first, then
Through a process called rehashing, rbenv maintains shims in that directory to match every Ruby command across every installed version of Ruby—
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 named
- Find the rbenv shim named
rakeat the beginning of your
- Run the shim named
rake, which in turn passes the command along to rbenv
RBENV_VERSIONenvironment variable, if specified. You can use the
rbenv 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 the
- The global
~/.rbenv/versionfile. You can modify this file using the
rbenv 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.
Each Ruby version is installed into its own directory under
~/.rbenv/versions. For example, you might have these versions installed:
If you're on Mac OS X, consider installing with Homebrew.
- Check out rbenv into
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ cd ~/.rbenv && src/configure && make -C src
$PATHfor access to the
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
Zsh note: Modify your
~/.zshrcfile instead of
~/.rbenv/bin/rbenv initfor shell-specific instructions on how to initialize rbenv to enable shims and autocompletion.
- Restart your shell so that PATH changes take effect. (Opening a new
terminal tab will usually do it.) Now check if rbenv was set up:
$ type rbenv #=> "rbenv is a function"
- (Optional) Install ruby-build, which provides the
rbenv installcommand that simplifies the process of installing new Ruby versions.（强烈建议安装ruby-build:
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build/
$ cd ~/.rbenv $ git pull
$ cd ~/.rbenv $ git fetch $ git checkout v0.3.0
$ brew update $ brew upgrade rbenv ruby-build
Afterwards you'll still need to run
$ brew update $ brew install rbenv
rbenv initfor instructions as stated in the caveats. You'll only ever have to do this once.
rbenv initis 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 initactually does:
- Sets up your shims path. This is the only requirement for rbenv to
function properly. You can do this by hand by prepending
- Installs autocompletion. This is entirely optional but pretty
~/.rbenv/completions/rbenv.bashwill set that up. There is also a
~/.rbenv/completions/rbenv.zshfor Zsh users.
- 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
- Installs the sh dispatcher. This bit is also optional, but allows
rbenv and plugins to change variables in your current shell, making
rbenv shellpossible. The sh dispatcher doesn't do anything crazy like override
cdor hack your shell prompt, but if for some reason you need
rbenvto be a real script rather than a shell function, you can safely skip it.
rbenv init -for yourself to see exactly what happens under the hood.
(请先运行apt-get install -y libreadline-dev）
rbenv installcommand 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:
# list all available versions: $ rbenv install -l # install a Ruby version: $ rbenv install 2.0.0-p247 (此步骤耗时约10分钟）
又比如：rbenv install 2.2.0 (此步骤耗时约10分钟）
会显示：Installed ruby-2.2.0 to /root/.rbenv/versions/2.2.0
root@AR:~/.rbenv/plugins/ruby-build# ruby -v-bash: ruby: 未找到命令root@AR:~/.rbenv/plugins/ruby-build# cd /root/.rbenv/versions/2.2.0root@AR:~/.rbenv/versions/2.2.0# lsbin include lib shareroot@AR:~/.rbenv/versions/2.2.0# ls binerb gem irb rake rdoc ri rubyroot@AR:~/.rbenv/versions/2.2.0# ~/.rbenv/versions/2.2.0/bin/ruby -vruby 2.2.0p0 (2014-12-25 revision 49005) [i686-linux]root@AR:~/.rbenv/versions/2.2.0#
(运行~/.rbenv/versions/2.2.0/bin/gem install shadowsocks 即可安装shadowsocks的ruby版，
root@AR:~/.rbenv/versions/2.2.0/bin# which ss-serverroot@AR:~/.rbenv/versions/2.2.0/bin# ss-server-bash: ss-server: 未找到命令root@AR:~/.rbenv/versions/2.2.0/bin# find / -name ss-server/root/.rbenv/versions/2.2.0/bin/ss-server/root/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/shadowsocks-0.12/bin/ss-serverroot@AR:~/.rbenv/versions/2.2.0/bin# echo 'export PATH=$PATH:~/.rbenv/versions/2.2.0/bin/' >> /etc/profile
root@AR:~/.rbenv/versions/2.2.0/bin# . /etc/profile )
installcommand, 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.
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:
$ gem install bundler
Check the location where gems are being installed with
$ gem env home # => ~/.rbenv/versions/<ruby-version>/lib/ruby/gems/...
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 the
rbenv prefixcommand, e.g.
rbenv prefix 1.8.7-p357.
The ruby-build plugin provides an
rbenv uninstallcommand to automate the removal process.
- 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 like
rubywill execute the system Ruby version, as before rbenv.
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
If you've installed rbenv using a package manager, as a final step perform the rbenv package removal. For instance, for Homebrew:
rm -rf `rbenv root`
brew uninstall rbenv
rbenvcommand delegates to subcommands based on its first argument. The most common subcommands are:
.ruby-versionfile in the current directory. This version overrides the global version, and can be overridden itself by setting the
RBENV_VERSIONenvironment variable or with the
When run without a version number,
$ rbenv local 1.9.3-p327
rbenv localreports the currently configured local version. You can also unset the local version:
$ rbenv local --unset
~/.rbenv/versionfile. This version can be overridden by an application-specific
.ruby-versionfile, or by setting the
The special version name
$ rbenv global 1.8.7-p352
systemtells rbenv to use the system Ruby (detected by searching your
When run without a version number,
rbenv globalreports the currently configured global version.
RBENV_VERSIONenvironment variable in your shell. This version overrides application-specific versions and the global version.
When run without a version number,
$ rbenv shell jruby-1.7.1
rbenv shellreports 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 shell --unset
$ export RBENV_VERSION=jruby-1.7.1
$ rbenv versions 1.8.7-p352 1.9.2-p290 * 1.9.3-p327 (set by /Users/sam/.rbenv/version) jruby-1.7.1 rbx-1.2.4 ree-1.8.7-2011.03
$ rbenv version 1.9.3-p327 (set by /Users/sam/.rbenv/version)
~/.rbenv/versions/*/bin/*). Run this command after you install a new version of Ruby, or install a gem that provides commands.
$ rbenv rehash
$ rbenv which irb /Users/sam/.rbenv/versions/1.9.3-p327/bin/irb
$ rbenv whence rackup 1.9.3-p327 jruby-1.7.1 ree-1.8.7-2011.03
||Specifies the Ruby version to be used.
||Defines the directory under which Ruby versions and shims reside.
||Outputs debug information.
||see wiki||Colon-separated list of paths searched for rbenv hooks.|
||Directory to start searching for
Tests are executed using Bats:
Please feel free to submit pull requests and file bugs on the issue tracker.
$ bats test $ bats test/<file>.bats