I am interested in how RVM and rbenv actually work.
Obviously they swap between different versions of Ruby and gemsets, but how is this achieved? I had assumed they were simply updating symlinks, but having delved into the code (and I must admit my knowledge of Bash is superficial) they appear to be doing more than this.
Short explanation: rbenv works by hooking into your environment's PATH. The concept is simple, but the devil is in the details; full scoop below.
First, rbenv creates shims for all the commands (ruby, irb, rake, gem and so on) across all your installed versions of Ruby. This process is called rehashing. Every time you install a new version of Ruby or install a gem that provides a command, run rbenv rehash to make sure any new commands are shimmed.
These shims live in a single directory (~/.rbenv/shims by default). To use rbenv, you need only add the shims directory to the front of your PATH:
export PATH="$HOME/.rbenv/shims:$PATH"
Then any time you run ruby from the command line, or run a script whose shebang reads #!/usr/bin/env ruby, your operating system will find ~/.rbenv/shims/ruby first and run it instead of any other ruby executable you may have installed.
Each shim is a tiny Bash script that in turn runs rbenv exec. So with rbenv in your path, irb is equivalent to rbenv exec irb, and ruby -e "puts 42" is equivalent to rbenv exec ruby -e "puts 42".
The rbenv exec command figures out what version of Ruby you want to use, then runs the corresponding command for that version. Here's how:
If the RBENV_VERSION environment variable is set, its value determines the version of Ruby to use.
If the current working directory has an .rbenv-version file, its contents are used to set the RBENV_VERSION environment variable.
If there is no .rbenv-version file in the current directory, rbenv searches each parent directory for an .rbenv-version file until it hits the root of your filesystem. If one is found, its contents are used to set the RBENV_VERSION environment variable.
If RBENV_VERSION is still not set, rbenv tries to set it using the contents of the ~/.rbenv/version file.
If no version is specified anywhere, rbenv assumes you want to use the "system" Ruby—i.e. whatever version would be run if rbenv weren't in your path.
(You can set a project-specific Ruby version with the rbenv local command, which creates a .rbenv-version file in the current directory. Similarly, the rbenv global command modifies the ~/.rbenv/version file.)
Armed with an RBENV_VERSION environment variable, rbenv adds ~/.rbenv/versions/$RBENV_VERSION/bin to the front of your PATH, then execs the command and arguments passed to rbenv exec. Voila!
For a thorough look at exactly what happens under the hood, try setting RBENV_DEBUG=1 and running a Ruby command. Every Bash command that rbenv runs will be written to your terminal.
Now, rbenv is just concerned with switching versions, but a thriving ecosystem of plugins will help you do everything from installing Ruby to setting up your environment, managing "gemsets" and even automating bundle exec.
I am not quite sure what IRC support has to do with switching Ruby versions, and rbenv is designed to be simple and understandable enough not to require support. But should you ever need help, the issue tracker and Twitter are just a couple of clicks away.
Disclosure: I am the author of rbenv, ruby-build, and rbenv-vars.
I wrote an in-depth article: http://niczsoft.com/2011/11/what-you-should-know-about-rbenv-and-rvm/
The basic difference is where the shell environment is changed:
RVM: it's changed when you change Ruby.
rbenv: it's changed when you run a Ruby/gem executable.
Also, the thing about RVM is, it covers a lot more then just managing Rubies, it has a lot more than any other tool (there are others apart from RVM and rbenv: https://twitter.com/#!/mpapis/status/171714447910502401)
Do not forget about instant support you get on IRC in the "#rvm" channel on the Freenode servers.
So to summarise the excellent answers above, the main practical difference between RVM and rbenv is when the version of Ruby is selected.
rbenv:
rbenv adds a shim to the start of your path, a command with the same name as Ruby. When you type ruby at a command line the shim is run instead (because it is also called "ruby" and comes first in the path). The shim looks for an environment variable or .rbenv_version file to tell it which version of Ruby to delegate to.
RVM:
RVM allows you to set a version of Ruby directly by calling rvm use. In addition, it also overrides the cd system command. When you cd into a folder that contains a .rvmrc file, the code inside the .rvmrc file is executed. This can be used to set a Ruby version, or anything else you fancy.
Other differences:
There are of course other differences. RVM has gemsets out of the box, while rbenv requires just a little more hacking (but not much). Both are functional solutions to the problem.
The main difference seems to be when and how ruby is switched. Ruby is switched:
for RVM manually (rvm use) or automatically during change of directories
for rbenv automatically each time a ruby command is executed
RVM relies on the modified cd command and manual selection of Ruby by rvm use. rbenv uses wrappers or "shims" for all basic ruby commands as the default mechanism to select ruby. RVM creates wrappers for basic command line tools like gem, rake, ruby, too. They are used for example in CronJobs ( see http://rvm.io/integration/cron/ ), but they are not the default mechanism to switch the Ruby version.
Thus both methods select "automatically" the right Ruby version by overwriting commands and using wrappers. rvm overrides shell commands like cd. rbenv overrides all basic ruby commands such as ruby, irb, rake and gem.
rvm system
env > before
rvm jruby # or whatever
env > after
diff after before
Gives you approximately:
< GEM_HOME=$HOME/.gem/ruby/1.9.1
---
> GEM_HOME=$HOME/.rvm/gems/jruby-1.6.6
< GEM_PATH=$HOME/.gem/ruby/1.9.1
---
> GEM_PATH=$HOME/.rvm/gems/jruby-1.6.6:$HOME/.rvm/gems/jruby-1.6.6#global
*bunch of rvm_*
> MY_RUBY_HOME=$HOME/.rvm/rubies/jruby-1.6.6
> RUBY_VERSION=jruby-1.6.6
> IRBRC=$HOME/.rvm/rubies/jruby-1.6.6/.irbrc
And it prepends:
$HOME/.rvm/gems/jruby-1.6.6/bin:$HOME/.rvm/gems/jruby-1.6.6#global/bin
to $PATH
Related
Why do we use "rvm alias create default ruby-2.X.X"?
I see ruby-2.X.X#global and ruby-2.X.X in /usr/local/rvm directory.
If we have multiple rails apps running on the same ruby version and bundler is taking care of the different gem versions, can't we just do "rvm install ruby-2.X.X && rvm use ruby-2.X.X --default" and be done with it?
I am not familiar with rvm alias feature but I do use rbenv alias extensively, which I imagine is similar.
One purpose of aliases is to save typing. Instead of typing rbenv shell 2.7.1 I could type rbenv shell 2.7. Saves 2 characters each time.
Another purpose of aliases is to provide a constant name/identifier for something that is variable. For example, "default" could refer to different versions over time, but a script that uses "default" would keep working throughout.
// Start background info.
I just want to install ruby on rails for development (OSX El Capitan).
Pain point:
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
Solution: Installed rbenv to manage / modify a separate ruby.
rbenv is currently using my system ruby - so I've downloaded an identical version through rbenv install.
// end background info.
Actual Question: Do I set rbenv local, global or shell ruby version to the newly downloaded version?
Usually rbenv does the dirty work for you if loaded correctly, but if you need to change the global setting, you can always update it:
rbenv global 2.3.0
Then you can check that's properly applied with:
rbenv versions
The * indicates the currently active ruby. Test with:
ruby -v
That should be the version you're asking for.
Using rbenv is a lot better than the system ruby, so I hope it works out for you.
According to the official rbenv documentation in Github, their differences are the following:
# Sets a local application-specific Ruby version
# by writing the version name to a `.ruby-version`.
$ rbenv local <version>
# Sets the global version of Ruby to be used in all shells
# by writing the version name to the `~/.rbenv/version` file
$ rbenv global <version>
# Sets a shell-specific Ruby version by setting the
# RBENV_VERSION environment variable in your shell.
# This for temporary use and will only work during the terminal session
$ rbenv shell <version>
Note that your terminal session will no longer respect any .ruby-version files. You need to run rbenv shell --unset to enable the auto switch again.
Happy Coding :)
Working on a project which uses jRuby, but locally when I work I often use MRI because it's faster.
Now switching between to is a major annoyance, this is how I switch from jruby to MRI:
rvm use ruby-2.1.5
sed -i.bak 3s/.*/ruby" '2.1.5'"/ Gemfile
Which replaces my Gemfile and specifies the right version of ruby. Now I wanted to create two executables from bin/ folder of my rails project, one called mri and another one jruby so I could switch back and forth between the two using a single command, and so that everyone who work in a team can use the same.
Problem with this is that, when the shell executes, the RVM doesn't change the ruby version in my current session. So naturally I get this message :
Your Ruby version is 2.0.0, but your Gemfile specified 2.1.5
Is this a good approach to what I'm trying to do?
pretty sure I'm not the first person trying this
Do you already have something that works for you?
As the comments pointed out, you need to re-source the rvm scripts.
You probably could just source ~/.bash_profile or ~/.bash_rc again, as they should be sourced there.
That being said, I haven't had this problem with rbenv, but I only swap between MRI versions.
There is also a spec for a .ruby-version file see here that both RVM and rbenv honor.
For bundling bin stubs you can specify a bin path with bundle binstubs --path mri-bin.
I am running my rails app from a virtualbox build by vagrant using puppet scripts. Every time I login to the box, I have the following problem:
When I run rvm list one of the things it lists is the following:
=* ruby-2.1.1
But when I try to run rails console, it tells me I need to install missing gems. When I run rvm use default, and then run rails console, it works. Why is the default and current setting in rvm not working--why do I have to go to the extra step of also telling rvm which ruby version to use?
Note: I do have a .ruby-version file with 2.1.1 in it. I'm using rvm version 1.25.25
Because you have to tell rvm which version to use.
In earlier version of rvm we have to define .rvmrc file which mention which ruby and gemset to use.
In recent version of rvm we have to define .ruby-version file with ruby version in it and .ruby-gemset file with the name of gemset.
If you just want a quick solution then in your rails directory make a .ruby-version file with content 2.1.1
correct syntax is:
rvm --default use ruby-2.1.1#global
This command sets ruby to selected default permanently. All new terminals will use your default Ruby. Also you don't mention anything about gemset, so I presume global would exist if you didn't mess up your setup.
This solved it:
I added rvm use --default to the machine's ~/.bashrc file.
I am wondering how RVM manages the gem/gemsets. For instance, I have by default been using ruby 1.9.2#global and installed rails 3.1.3 in this environment. Later I copied a rails project from other, which is based on rails 3.0.10. By cd into the folder, I ran rails -v, it recommended me to run bundle install. I did so. After this, lots of gems were installed including rails 3.0.10. But when I do rvm 1.9.2 do gemset list, there is no new gemset( this is expected though). Then how do I manage the two versions of rails?
Thank you in advance
You can have more than one path into your Gem.path.
Try running ruby -r rubygems -e "p Gem.path" and check the output, you'll see that both #global and your current gemset are included.
Rubygems use the $GEM_PATH variable to figure out where to look/install gems, and that's one of the things RVM sets when you change a ruby version/gemset.
Also, it might be worth to look at a few environment variables RVM sets. Run this:
env | grep -i rvm||path
There might be a few extra ones (including $PATH), but you'll general you'll see a lot of RVM environment variables.
That's why some people like rbenv, a simpler way to manage ruby versions.