How to install and use different versions of ruby?

14

2

I'm learning rails from different books that use different versions of both ruby and rails. Right now I have ruby 1.87 installed on my Mac OS X Snow Leopard (in /usr/bin), but need to also use ruby 1.9 for a different rails application.

Can anyone tell me how to make this work? I'm new to this, so as many instructions as possible would be greatly appreciated.

Michael

Posted 2011-09-27T17:24:14.710

Reputation: 395

Answers

15

There are two "Ruby version managers" out there, which you can choose from:

These allow you to keep multiple versions of Ruby on the same system. Once you've installed a version manager, and installed your own Ruby version, you won't mess with your system's Ruby and its Gems, which is the greatest benefit. No more sudo! No more permissions errors and Gem conflicts.

Which one should I choose?

Both do the same thing, but they follow different philosophies. The choice is up to you.

I'd personally recommend rbenv for its simplicity. I've been using it since years and it has always worked well. Previously I would have recommended RVM (and an earlier version of this answer actually mentioned it), but I feel it's harder to get into.

How do I install them?

If you choose rbenv:

  • Follow the installation instructions
  • Install ruby-build
  • Run rbenv install x.x.x where x.x.x is the version (use rbenv install --list to see which ones are available
  • Run rbenv global x.x.x to change your global Ruby version

If you choose RVM:

  • Use the secure installation method
  • Read the installation instructions — you probably want the single-user configuration
  • Use rvm list known to list available Rubies and then run rvm install x.x.x to install a specific version.
  • Use rvm use x.x.x --default to change your default Ruby

slhck

Posted 2011-09-27T17:24:14.710

Reputation: 182 472

It appears the link to RVM needs updated: https://rvm.io/

– Jacob Ewald – 2016-01-04T16:51:53.707

1

See also rbenv.

– user1686 – 2011-09-27T17:44:53.840

@grawity Why not post another answer? Good to have alternatives, haven't really looked into rbenv yet. – slhck – 2011-09-27T17:47:10.033

thanks so much, does it also help switch between versions of Rails? different books I'm using employ 3.05 (I think) and 3.1 and it's causing problems... – Michael – 2011-09-27T18:14:42.853

I tried the install line you wrote (and which is also on rvm site) and got an error message: bash: line 152: git: command not found bash: line 154: git: command not found – Michael – 2011-09-27T18:17:57.937

Ah, you need git, of course. Sorry, I forgot that. You can install Git with the OS X installer (just select the latest version at the top).

– slhck – 2011-09-27T18:22:52.903

And yes, Rails is in essence just a gem (together with others), so you can switch it with Named Gem Sets as explained in the RVM manual – even specifically for the Rails case. @Michael

– slhck – 2011-09-27T18:23:59.520

this is weird, the commments appear and disappear. I thought you had a comment here that linked to the git installer... – Michael – 2011-09-27T18:26:06.580

thanks a lot. Git comes iwth a file "set up git for non-terminal programs". There's a bit of code in there...should I put it somewhere? – Michael – 2011-09-27T18:29:50.647

@Michael See the part in README for that. You don't need it, but it doesn't hurt to have it. Just open that file with a Terminal. If it's too complicated, just skip that part. – slhck – 2011-09-27T19:16:03.017

7

I think rbenv deserves at least its own answer.

There is a constant battle between fans of rbenv and those of RVM but I personally like rbenv a lot more. As the Sam Stephenson (the author) states, rbenv it solely concerned with switching Ruby versions (as opposed to RVM, which does a lot more).

On OS X, it's especially easy to give it a try. Just follow the excellent installation instructions on the Github page (if you have Homebrew installed, it's basically just a brew install rbenv ruby-build).

As for switching Rails versions, I once wrote an article about that which my be of interest for you.

Michael Trojanek

Posted 2011-09-27T17:24:14.710

Reputation: 181

1Here's a brief description of rbenv's author on the main differences with RVM and reasons to choose rbenv. The simplicity of rbenv was the main reason for me to migrate away from RVM. Managing sets of application-specific gems is IMO better done with Bundler - you don't need a Ruby version manager for that. – Jochem Schulenklopper – 2015-05-31T13:32:05.543

Exactly – managing sets of application-specific gems is Bundler's job. I have never been a fan of gemsets tied to version managers.

– Michael Trojanek – 2015-06-16T20:36:19.067