You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

413 lines
15 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
  1. # Simple Ruby Version Management: rbenv
  2. rbenv lets you easily switch between multiple versions of Ruby. It's
  3. simple, unobtrusive, and follows the UNIX tradition of single-purpose
  4. tools that do one thing well.
  5. <img src="http://i.sstephenson.us/rbenv2.png" width="894" height="464">
  6. ### rbenv _does…_
  7. * Let you **change the global Ruby version** on a per-user basis.
  8. * Provide support for **per-project Ruby versions**.
  9. * Allow you to **override the Ruby version** with an environment
  10. variable.
  11. ### In contrast with rvm, rbenv _does not…_
  12. * **Need to be loaded into your shell.** Instead, rbenv's shim
  13. approach works by adding a directory to your `$PATH`.
  14. * **Override shell commands like `cd`.** That's dangerous and
  15. error-prone.
  16. * **Have a configuration file.** There's nothing to configure except
  17. which version of Ruby you want to use.
  18. * **Install Ruby.** You can build and install Ruby yourself, or use
  19. [ruby-build](https://github.com/sstephenson/ruby-build) to
  20. automate the process.
  21. * **Manage gemsets.** [Bundler](http://gembundler.com/) is a better
  22. way to manage application dependencies. If you have projects that
  23. are not yet using Bundler you can install the
  24. [rbenv-gemset](https://github.com/jamis/rbenv-gemset) plugin.
  25. * **Require changes to Ruby libraries for compatibility.** The
  26. simplicity of rbenv means as long as it's in your `$PATH`,
  27. [nothing](https://rvm.beginrescueend.com/integration/bundler/)
  28. [else](https://rvm.beginrescueend.com/integration/capistrano/)
  29. needs to know about it.
  30. * **Prompt you with warnings when you switch to a project.** Instead
  31. of executing arbitrary code, rbenv reads just the version name
  32. from each project. There's nothing to "trust."
  33. ## Table of Contents
  34. * [1 How It Works](#section_1)
  35. * [2 Installation](#section_2)
  36. * [2.1 Basic GitHub Checkout](#section_2.1)
  37. * [2.1.1 Upgrading](#section_2.1.1)
  38. * [2.2 Homebrew on Mac OS X](#section_2.2)
  39. * [2.3 Neckbeard Configuration](#section_2.3)
  40. * [3 Usage](#section_3)
  41. * [3.1 rbenv global](#section_3.1)
  42. * [3.2 rbenv local](#section_3.2)
  43. * [3.3 rbenv shell](#section_3.3)
  44. * [3.4 rbenv versions](#section_3.4)
  45. * [3.5 rbenv version](#section_3.5)
  46. * [3.6 rbenv rehash](#section_3.6)
  47. * [3.7 rbenv which](#section_3.7)
  48. * [3.8 rbenv whence](#section_3.8)
  49. * [4 Development](#section_4)
  50. * [4.1 Version History](#section_4.1)
  51. * [4.2 License](#section_4.2)
  52. ## <a name="section_1"></a> 1 How It Works
  53. rbenv operates on the per-user directory `~/.rbenv`. Version names in
  54. rbenv correspond to subdirectories of `~/.rbenv/versions`. For
  55. example, you might have `~/.rbenv/versions/1.8.7-p354` and
  56. `~/.rbenv/versions/1.9.3-rc1`.
  57. Each version is a working tree with its own binaries, like
  58. `~/.rbenv/versions/1.8.7-p354/bin/ruby` and
  59. `~/.rbenv/versions/1.9.3-rc1/bin/irb`. rbenv makes _shim binaries_
  60. for every such binary across all installed versions of Ruby.
  61. These shims are simple wrapper scripts that live in `~/.rbenv/shims`
  62. and detect which Ruby version you want to use. They insert the
  63. directory for the selected version at the beginning of your `$PATH`
  64. and then execute the corresponding binary.
  65. Because of the simplicity of the shim approach, all you need to use
  66. rbenv is `~/.rbenv/shims` in your `$PATH`.
  67. ## <a name="section_2"></a> 2 Installation
  68. **Compatibility note**: rbenv is _incompatible_ with rvm. Things will
  69. appear to work until you try to install a gem. The problem is that
  70. rvm actually overrides the `gem` command with a shell function!
  71. Please remove any references to rvm before using rbenv.
  72. ### <a name="section_2.1"></a> 2.1 Basic GitHub Checkout
  73. This will get you going with the latest version of rbenv and make it
  74. easy to fork and contribute any changes back upstream.
  75. 1. Check out rbenv into `~/.rbenv`.
  76. $ git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
  77. 2. Add `~/.rbenv/bin` to your `$PATH` for access to the `rbenv`
  78. command-line utility.
  79. $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
  80. **Zsh note**: Modify your `~/.zshenv` file instead of `~/.bash_profile`.
  81. 3. Add rbenv init to your shell to enable shims and autocompletion.
  82. $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
  83. **Zsh note**: Modify your `~/.zshenv` file instead of `~/.bash_profile`.
  84. 4. Restart your shell so the path changes take effect. You can now
  85. begin using rbenv.
  86. $ exec $SHELL
  87. 5. Install Ruby versions into `~/.rbenv/versions`. For example, to
  88. install Ruby 1.9.2-p290, download and unpack the source, then run:
  89. $ ./configure --prefix=$HOME/.rbenv/versions/1.9.2-p290
  90. $ make
  91. $ make install
  92. The [ruby-build](https://github.com/sstephenson/ruby-build) project
  93. provides an `rbenv install` command that simplifies the process of
  94. installing new Ruby versions to:
  95. $ rbenv install 1.9.2-p290
  96. 6. Rebuild the shim binaries. You should do this any time you install
  97. a new Ruby binary (for example, when installing a new Ruby version,
  98. or when installing a gem that provides a binary).
  99. $ rbenv rehash
  100. #### <a name="section_2.1.1"></a> 2.1.1 Upgrading
  101. If you've installed rbenv using the instructions above, you can
  102. upgrade your installation at any time using git.
  103. To upgrade to the latest development version of rbenv, use `git pull`:
  104. $ cd ~/.rbenv
  105. $ git pull
  106. To upgrade to a specific release of rbenv, check out the corresponding
  107. tag:
  108. $ cd ~/.rbenv
  109. $ git fetch
  110. $ git tag
  111. v0.1.0
  112. v0.1.1
  113. v0.1.2
  114. v0.2.0
  115. $ git checkout v0.2.0
  116. ### <a name="section_2.2"></a> 2.2 Homebrew on Mac OS X
  117. You can also install rbenv using the
  118. [Homebrew](http://mxcl.github.com/homebrew/) package manager on Mac OS
  119. X.
  120. $ brew update
  121. $ brew install rbenv
  122. $ brew install ruby-build
  123. The same commands can be used for upgrading.
  124. Afterwards you'll still need to add `eval "$(rbenv init -)"` to your
  125. profile as stated in the caveats. You'll only ever have to do this
  126. once.
  127. ### <a name="section_2.3"></a> 2.3 Neckbeard Configuration
  128. Skip this section unless you must know what every line in your shell
  129. profile is doing.
  130. `rbenv init` is the only command that crosses the line of loading
  131. extra commands into your shell. Coming from rvm, some of you might be
  132. opposed to this idea. Here's what `rbenv init` actually does:
  133. 1. Sets up your shims path. This is the only requirement for rbenv to
  134. function properly. You can do this by hand by prepending
  135. `~/.rbenv/shims` to your `$PATH`.
  136. 2. Installs autocompletion. This is entirely optional but pretty
  137. useful. Sourcing `~/.rbenv/completions/rbenv.bash` will set that
  138. up. There is also a `~/.rbenv/completions/rbenv.zsh` for Zsh
  139. users.
  140. 3. Rehashes shims. From time to time you'll need to rebuild your
  141. shim files. Doing this on init makes sure everything is up to
  142. date. You can always run `rbenv rehash` manually.
  143. 4. Installs the sh dispatcher. This bit is also optional, but allows
  144. rbenv and plugins to change variables in your current shell, making
  145. commands like `rbenv shell` possible. The sh dispatcher doesn't do
  146. anything crazy like override `cd` or hack your shell prompt, but if
  147. for some reason you need `rbenv` to be a real script rather than a
  148. shell function, you can safely skip it.
  149. Run `rbenv init -` for yourself to see exactly what happens under the
  150. hood.
  151. ## <a name="section_3"></a> 3 Usage
  152. Like `git`, the `rbenv` command delegates to subcommands based on its
  153. first argument. The most common subcommands are:
  154. ### <a name="section_3.1"></a> 3.1 rbenv global
  155. Sets the global version of Ruby to be used in all shells by writing
  156. the version name to the `~/.rbenv/version` file. This version can be
  157. overridden by a per-project `.rbenv-version` file, or by setting the
  158. `RBENV_VERSION` environment variable.
  159. $ rbenv global 1.9.2-p290
  160. The special version name `system` tells rbenv to use the system Ruby
  161. (detected by searching your `$PATH`).
  162. When run without a version number, `rbenv global` reports the
  163. currently configured global version.
  164. ### <a name="section_3.2"></a> 3.2 rbenv local
  165. Sets a local per-project Ruby version by writing the version name to
  166. an `.rbenv-version` file in the current directory. This version
  167. overrides the global, and can be overridden itself by setting the
  168. `RBENV_VERSION` environment variable or with the `rbenv shell`
  169. command.
  170. $ rbenv local rbx-1.2.4
  171. When run without a version number, `rbenv local` reports the currently
  172. configured local version. You can also unset the local version:
  173. $ rbenv local --unset
  174. ### <a name="section_3.3"></a> 3.3 rbenv shell
  175. Sets a shell-specific Ruby version by setting the `RBENV_VERSION`
  176. environment variable in your shell. This version overrides both
  177. project-specific versions and the global version.
  178. $ rbenv shell jruby-1.6.4
  179. When run without a version number, `rbenv shell` reports the current
  180. value of `RBENV_VERSION`. You can also unset the shell version:
  181. $ rbenv shell --unset
  182. Note that you'll need rbenv's shell integration enabled (step 3 of
  183. the installation instructions) in order to use this command. If you
  184. prefer not to use shell integration, you may simply set the
  185. `RBENV_VERSION` variable yourself:
  186. $ export RBENV_VERSION=jruby-1.6.4
  187. ### <a name="section_3.4"></a> 3.4 rbenv versions
  188. Lists all Ruby versions known to rbenv, and shows an asterisk next to
  189. the currently active version.
  190. $ rbenv versions
  191. 1.8.7-p352
  192. 1.9.2-p290
  193. * 1.9.3-rc1 (set by /Users/sam/.rbenv/global)
  194. jruby-1.6.4
  195. rbx-1.2.4
  196. ree-1.8.7-2011.03
  197. ### <a name="section_3.5"></a> 3.5 rbenv version
  198. Displays the currently active Ruby version, along with information on
  199. how it was set.
  200. $ rbenv version
  201. 1.8.7-p352 (set by /Volumes/37signals/basecamp/.rbenv-version)
  202. ### <a name="section_3.6"></a> 3.6 rbenv rehash
  203. Installs shims for all Ruby binaries known to rbenv (i.e.,
  204. `~/.rbenv/versions/*/bin/*`). Run this command after you install a new
  205. version of Ruby, or install a gem that provides binaries.
  206. $ rbenv rehash
  207. ### <a name="section_3.7"></a> 3.7 rbenv which
  208. Displays the full path to the binary that rbenv will execute when you
  209. run the given command.
  210. $ rbenv which irb
  211. /Users/sam/.rbenv/versions/1.9.2-p290/bin/irb
  212. ### <a name="section_3.8"></a> 3.8 rbenv whence
  213. Lists all Ruby versions with the given command installed.
  214. $ rbenv whence rackup
  215. 1.9.3-rc1
  216. jruby-1.6.4
  217. ree-1.8.7-2011.03
  218. ## <a name="section_4"></a> 4 Development
  219. The rbenv source code is [hosted on
  220. GitHub](https://github.com/sstephenson/rbenv). It's clean, modular,
  221. and easy to understand, even if you're not a shell hacker.
  222. Please feel free to submit pull requests and file bugs on the [issue
  223. tracker](https://github.com/sstephenson/rbenv/issues).
  224. ### <a name="section_4.1"></a> 4.1 Version History
  225. **0.3.0** (December 25, 2011)
  226. * Added an `rbenv root` command which prints the value of
  227. `$RBENV_ROOT`, or the default root directory if it's unset.
  228. * Clarified Zsh installation instructions in the readme.
  229. * Removed some redundant code in `rbenv rehash`.
  230. * Fixed an issue with calling `readlink` for paths with spaces.
  231. * Changed Zsh initialization code to install completion hooks only for
  232. interactive shells.
  233. * Added preliminary support for ksh.
  234. * `rbenv rehash` creates or removes shims only when necessary instead
  235. of removing and re-creating all shims on each invocation.
  236. * Fixed that `RBENV_DIR`, when specified, would be incorrectly
  237. expanded to its parent directory.
  238. * Removed the deprecated `set-default` and `set-local` commands.
  239. * Added a `--no-rehash` option to `rbenv init` for skipping the
  240. automatic rehash when opening a new shell.
  241. **0.2.1** (October 1, 2011)
  242. * Changed the `rbenv` command to ensure that `RBENV_DIR` is always an
  243. absolute path. This fixes an issue where Ruby scripts using the
  244. `ruby-local-exec` wrapper would go into an infinite loop when
  245. invoked with a relative path from the command line.
  246. **0.2.0** (September 28, 2011)
  247. * Renamed `rbenv set-default` to `rbenv global` and `rbenv set-local`
  248. to `rbenv local`. The `set-` commands are deprecated and will be
  249. removed in the next major release.
  250. * rbenv now uses `greadlink` on Solaris.
  251. * Added a `ruby-local-exec` command which can be used in shebangs in
  252. place of `#!/usr/bin/env ruby` to properly set the project-specific
  253. Ruby version regardless of current working directory.
  254. * Fixed an issue with `rbenv rehash` when no binaries are present.
  255. * Added support for `rbenv-sh-*` commands, which run inside the
  256. current shell instead of in a child process.
  257. * Added an `rbenv shell` command for conveniently setting the
  258. `$RBENV_VERSION` environment variable.
  259. * Added support for storing rbenv versions and shims in directories
  260. other than `~/.rbenv` with the `$RBENV_ROOT` environment variable.
  261. * Added support for debugging rbenv via `set -x` when the
  262. `$RBENV_DEBUG` environment variable is set.
  263. * Refactored the autocompletion system so that completions are now
  264. built-in to each command and shared between bash and Zsh.
  265. * Added support for plugin bundles in `~/.rbenv/plugins` as documented
  266. in [issue #102](https://github.com/sstephenson/rbenv/pull/102).
  267. * Added `/usr/local/etc/rbenv.d` to the list of directories searched
  268. for rbenv hooks.
  269. * Added support for an `$RBENV_DIR` environment variable which
  270. defaults to the current working directory for specifying where rbenv
  271. searches for local version files.
  272. **0.1.2** (August 16, 2011)
  273. * Fixed rbenv to be more resilient against nonexistent entries in
  274. `$PATH`.
  275. * Made the `rbenv rehash` command operate atomically.
  276. * Modified the `rbenv init` script to automatically run `rbenv
  277. rehash` so that shims are recreated whenever a new shell is opened.
  278. * Added initial support for Zsh autocompletion.
  279. * Removed the dependency on egrep for reading version files.
  280. **0.1.1** (August 14, 2011)
  281. * Fixed a syntax error in the `rbenv help` command.
  282. * Removed `-e` from the shebang in favor of `set -e` at the top of
  283. each file for compatibility with operating systems that do not
  284. support more than one argument in the shebang.
  285. **0.1.0** (August 11, 2011)
  286. * Initial public release.
  287. ### <a name="section_4.2"></a> 4.2 License
  288. (The MIT license)
  289. Copyright (c) 2011 Sam Stephenson
  290. Permission is hereby granted, free of charge, to any person obtaining
  291. a copy of this software and associated documentation files (the
  292. "Software"), to deal in the Software without restriction, including
  293. without limitation the rights to use, copy, modify, merge, publish,
  294. distribute, sublicense, and/or sell copies of the Software, and to
  295. permit persons to whom the Software is furnished to do so, subject to
  296. the following conditions:
  297. The above copyright notice and this permission notice shall be
  298. included in all copies or substantial portions of the Software.
  299. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  300. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  301. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  302. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  303. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  304. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  305. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.