33

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.

slhck
  • 223,558
  • 70
  • 607
  • 592
Michael
  • 615
  • 1
  • 11
  • 19

4 Answers4

47

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

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 personally recommend rbenv for its simplicity. I've been using it for years and it has always worked well.

How do I install them?

If you choose rbenv:

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
  • 223,558
  • 70
  • 607
  • 592
  • 1
    See also [rbenv](https://github.com/sstephenson/rbenv). – u1686_grawity Sep 27 '11 at 17:44
  • @grawity Why not post another answer? Good to have alternatives, haven't really looked into `rbenv` yet. – slhck Sep 27 '11 at 17:47
  • 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 Sep 27 '11 at 18:14
  • 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 Sep 27 '11 at 18:17
  • Ah, you need `git`, of course. Sorry, I forgot that. You can install Git with the [OS X installer](http://code.google.com/p/git-osx-installer/downloads/list) (just select the latest version at the top). – slhck Sep 27 '11 at 18:22
  • And yes, Rails is in essence just a gem (together with others), so you can switch it with [Named Gem Sets](http://beginrescueend.com/gemsets/basics/) as explained in the RVM manual – even specifically for the Rails case. @Michael – slhck Sep 27 '11 at 18:23
  • this is weird, the commments appear and disappear. I thought you had a comment here that linked to the git installer... – Michael Sep 27 '11 at 18:26
  • 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 Sep 27 '11 at 18:29
  • @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 Sep 27 '11 at 19:16
  • It appears the link to RVM needs updated: https://rvm.io/ – Jacob Ewald Jan 04 '16 at 16:51
8

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.

  • 1
    [Here's a brief description](https://github.com/sstephenson/rbenv/wiki/Why-rbenv%3F) 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 May 31 '15 at 13:32
  • Exactly – managing sets of application-specific gems is [Bundler's job](http://www.relativkreativ.at/articles/managing-multiple-rails-versions). I have never been a fan of gemsets tied to version managers. – Michael Trojanek Jun 16 '15 at 20:36
  • on OSX you don't really need it, as you can install different versions of Ruby with brew, and add in the path the one that you want to use – maxadamo Apr 22 '23 at 11:41
1

Assuming you have installed the rbenv ruby version: run,

rbenv init

Then;

eval "$(rbenv init - zsh)" 

To confirm the switched version, run;

which ruby

It should be something like;

/Users/MacbookAir/.rbenv/shims/ruby
Patrick
  • 11
  • 2
1

This is what worked for me, I don't have sudo:

#!/usr/bin/env bash

ruby -v
if ! command -v ruby &> /dev/null
then
    echo "Going to try to install ruby (ideally 3.1.2)"
    # - install rebenv (following ruby-build really is needed eventhough it doesn't look like it)
    mkdir -p ~/.rbenv
    cd ~/.rbenv
    git clone https://github.com/rbenv/rbenv.git .
    # if $HOME/.rbenv/bin not in path append it, otherwise don't change it
    echo $PATH | tr ':' '\n' | awk '{print "  " $0}';
    if [[ ":$PATH:" != *":$HOME/.rbenv/bin:"* ]]; then
      echo "might want to put $HOME/.rbenv/bin in your path"
      export PATH="$HOME/.rbenv/bin:$PATH"
#      echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc.lfs
    fi
    eval "$(rbenv init -)"
    rbenv -v

    # - install ruby-build, odd, this really is needed for ruby to install despite it not looking like ruby build is need at the bottom
    mkdir -p ~/.ruby-build
    cd ~/.ruby-build
    git clone https://github.com/rbenv/ruby-build.git .
    # if $HOME/.ruby-build/bin not in path append it, otherwise don't change it
    echo $PATH | tr ':' '\n' | awk '{print "  " $0}';
    if [[ $PATH != *"$HOME/.ruby-build/bin"* ]]; then
      echo "might want to put $HOME/.ruby-build/bin in your path"
      export PATH="$HOME/.ruby-build/bin:$PATH"
#      echo 'export PATH="$HOME/.ruby-build/bin:$PATH"' >> ~/.bashrc.lfs
    fi
    ruby-build --version

    # - install ruby without sudo -- using rbenv
    mkdir -p ~/.local
    #    ruby-build 3.1.2 ~/.local/
    rbenv install 3.1.2
    rbenv global 3.1.2
fi
ruby -v

# - wontfix: above worked but what was ruby's official way to do this? doesn't matter but answer might be here some day: https://discuss.rubyonrails.org/t/what-is-the-official-way-to-install-ruby-ideally-3-1-2-on-ubuntu/82226

# -old prover bot ignore doesn't work on SNAP
# Proverbot's way to install ruby
#    # First, install Ruby, as that is for some reason required to build the "system" project
#    git clone https://github.com/rbenv/ruby-build.git ~/ruby-build
#    mkdir -p ~/.local
#    PREFIX=~/.local ./ruby-build/install.sh
#    ~/.local/ruby-build 3.1.2 ~/.local/
# ref: https://superuser.com/questions/340490/how-to-install-and-use-different-versions-of-ruby/1756372#1756372

see this for more general answers: https://askubuntu.com/questions/339/how-can-i-install-a-package-without-root-access

related: https://stackoverflow.com/questions/75330125/why-would-only-using-rbenv-and-ruby-build-work-to-install-ruby