Mislav Marohnić 681435e0b4 | před 11 roky | |
---|---|---|
bin | před 11 roky | |
completions | před 11 roky | |
libexec | před 11 roky | |
test | před 11 roky | |
.gitignore | před 11 roky | |
.travis.yml | před 11 roky | |
LICENSE | před 11 roky | |
README.md | před 11 roky |
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 cd
in 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.
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.
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:
/usr/local/bin:/usr/bin:/bin
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
.
rbenv works by inserting a directory of shims at the front of your
PATH
:
~/.rbenv/shims:/usr/local/bin:/usr/bin:/bin
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:
PATH
for an executable file named rake
rake
at the beginning of your PATH
rake
, which in turn passes the command along to
rbenvWhen you execute a shim, rbenv determines which Ruby version to use by reading it from the following sources, in this order:
The RBENV_VERSION
environment variable, if specified. You can use
the rbenv shell
command to set this environment
variable in your current shell session.
The first .ruby-version
file 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-version
file 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-version
file in the current working
directory with the rbenv local
command.
The global ~/.rbenv/version
file. You can modify this file using
the rbenv global
command. 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.
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
.
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.
If you're on Mac OS X, consider installing with Homebrew.
This will get you going with the latest version of rbenv and make it easy to fork and contribute any changes back upstream.
Check out rbenv into ~/.rbenv
.
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
Add ~/.rbenv/bin
to your $PATH
for access to the rbenv
command-line utility.
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
Ubuntu Desktop note: Modify your ~/.bashrc
instead of ~/.bash_profile
.
Zsh note: Modify your ~/.zshrc
file instead of ~/.bash_profile
.
Add rbenv init
to your shell to enable shims and autocompletion.
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
Same as in previous step, use ~/.bashrc
on Ubuntu, or ~/.zshrc
for Zsh.
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 install
command that simplifies the process of
installing new Ruby versions.
If you've installed rbenv manually using git, you can upgrade your installation to the cutting-edge version at any time.
$ cd ~/.rbenv
$ git pull
To use a specific release of rbenv, check out the corresponding tag:
$ cd ~/.rbenv
$ git fetch
$ git checkout v0.3.0
If you've installed via Homebrew, then upgrade
via its brew
command:
$ brew update
$ brew upgrade rbenv ruby-build
As an alternative to installation via GitHub checkout, you can install rbenv and ruby-build using the Homebrew package manager on Mac OS X:
$ brew update
$ brew install rbenv ruby-build
Afterwards you'll still need to add eval "$(rbenv init -)"
to your
profile as stated in the caveats. You'll only ever have to do this
once.
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/shims
to your $PATH
.
Installs autocompletion. This is entirely optional but pretty
useful. Sourcing ~/.rbenv/completions/rbenv.bash
will set that
up. There is also a ~/.rbenv/completions/rbenv.zsh
for 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 rbenv rehash
manually.
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. The sh dispatcher doesn't do
anything crazy like override cd
or hack your shell prompt, but if
for some reason you need rbenv
to 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.
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:
# list all available versions:
$ rbenv install -l
# install a Ruby version:
$ rbenv install 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.
As time goes on, Ruby versions you install will accumulate in your
~/.rbenv/versions
directory.
To remove old Ruby versions, simply rm -rf
the directory of the
version you want to remove. You can find the directory of a particular
Ruby version with the rbenv prefix
command, e.g. rbenv prefix 1.8.7-p357
.
The ruby-build plugin provides an rbenv uninstall
command to
automate the removal process.
Like git
, the rbenv
command delegates to subcommands based on its
first argument. The most common subcommands are:
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.
$ rbenv local 1.9.3-p327
When run without a version number, rbenv local
reports the currently
configured local version. You can also unset the local version:
$ rbenv local --unset
Previous versions of rbenv stored local version specifications in a
file named .rbenv-version
. For backwards compatibility, rbenv will
read a local version specified in an .rbenv-version
file, but a
.ruby-version
file in the same directory will take precedence.
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.
$ rbenv global 1.8.7-p352
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.
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.
$ rbenv shell jruby-1.7.1
When run without a version number, rbenv shell
reports the current
value of RBENV_VERSION
. You can also unset the shell version:
$ rbenv shell --unset
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:
$ export RBENV_VERSION=jruby-1.7.1
Lists all Ruby versions known to rbenv, and shows an asterisk next to the currently active version.
$ 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
Displays the currently active Ruby version, along with information on how it was set.
$ rbenv version
1.8.7-p352 (set by /Volumes/37signals/basecamp/.ruby-version)
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 rehash
Displays the full path to the executable that rbenv will invoke when you run the given command.
$ rbenv which irb
/Users/sam/.rbenv/versions/1.9.3-p327/bin/irb
Lists all Ruby versions with the given command installed.
$ rbenv whence rackup
1.9.3-p327
jruby-1.7.1
ree-1.8.7-2011.03
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:
$ bats test
$ bats test/<file>.bats
Please feel free to submit pull requests and file bugs on the issue tracker.
0.4.0 (January 4, 2013)
.ruby-version
files to .rbenv-version
files
for specifying local application-specific versions. The
.ruby-version
file has the same format as .rbenv-version
but is
compatible with other Ruby version
managers.ruby-local-exec
and moved its functionality into the
standard ruby
shim. See the ruby-local-exec wiki
page for
upgrade instructions.$PATH
.rbenv init
by avoiding rbenv reinitialization and by
using a simpler indexing approach. (Users of
chef-rbenv should upgrade
to the latest version to fix a compatibility
issue.)rbenv help
so that usage and documentation is stored as a
comment in each subcommand, enabling plugin commands to hook into
the help system.rbenv which
and rbenv prefix
with system Ruby versions.rbenv exec
to avoid prepending the system Ruby location to
$PATH
to fix issues running system Ruby commands that invoke other
commands.rbenv rehash
to ensure it exits with a 0 status code under
normal operation, and to ensure outdated shims are removed first
when rehashing.rbenv rehash
to run hash -r
afterwards, when shell
integration is enabled, to ensure the shell's command cache is
cleared.+=
operator to support older versions of Bash.rbenv versions
output to include system
, if
present.rbenv versions
not to display a warning if the currently
specified version doesn't exist.rbenv
shell
function wrapper.rbenv shell
to ensure it exits with a non-zero status on
failure.rbenv --version
for printing the current version of rbenv./usr/lib/rbenv/hooks
to the plugin hook search path.rbenv which
to account for path entries with spaces.rbenv init
to accept option arguments in any order.0.3.0 (December 25, 2011)
rbenv root
command which prints the value of
$RBENV_ROOT
, or the default root directory if it's unset.rbenv rehash
.readlink
for paths with spaces.rbenv rehash
creates or removes shims only when necessary instead
of removing and re-creating all shims on each invocation.RBENV_DIR
, when specified, would be incorrectly
expanded to its parent directory.set-default
and set-local
commands.--no-rehash
option to rbenv init
for skipping the
automatic rehash when opening a new shell.0.2.1 (October 1, 2011)
rbenv
command to ensure that RBENV_DIR
is always an
absolute path. This fixes an issue where Ruby scripts using the
ruby-local-exec
wrapper would go into an infinite loop when
invoked with a relative path from the command line.0.2.0 (September 28, 2011)
rbenv set-default
to rbenv global
and rbenv set-local
to rbenv local
. The set-
commands are deprecated and will be
removed in the next major release.greadlink
on Solaris.ruby-local-exec
command which can be used in shebangs in
place of #!/usr/bin/env ruby
to properly set the project-specific
Ruby version regardless of current working directory.rbenv rehash
when no binaries are present.rbenv-sh-*
commands, which run inside the
current shell instead of in a child process.rbenv shell
command for conveniently setting the
$RBENV_VERSION
environment variable.~/.rbenv
with the $RBENV_ROOT
environment variable.set -x
when the
$RBENV_DEBUG
environment variable is set.~/.rbenv/plugins
as documented
in issue #102./usr/local/etc/rbenv.d
to the list of directories searched
for rbenv hooks.$RBENV_DIR
environment variable which
defaults to the current working directory for specifying where rbenv
searches for local version files.0.1.2 (August 16, 2011)
$PATH
.rbenv rehash
command operate atomically.rbenv init
script to automatically run rbenv rehash
so that shims are recreated whenever a new shell is opened.0.1.1 (August 14, 2011)
rbenv help
command.-e
from the shebang in favor of set -e
at the top of
each file for compatibility with operating systems that do not
support more than one argument in the shebang.0.1.0 (August 11, 2011)
(The MIT license)
Copyright (c) 2013 Sam Stephenson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.