23

I'm finding the task of installing ruby 2.0 on the latest Ubuntu 14.04 rather frustrating. I'm aware that I could forgo the packages and install from source - but I'd far rather install a package - if that is possible.

I found this question/answer about ruby on 13.10 - which looked like what I wanted.

How to install ruby?

Unfortunately, the strategy doesn't seem viable - the ruby-switch package has been deleted in 14.04.

http://www.ubuntuupdates.org/package/core/trusty/universe/base/ruby-switch

The deletion of the package references a bug which, to me, looks entirely unrelated.

I'm bemused about why installing ruby2.0 using apt-get installs ruby1.9 and makes it the default ruby interpreter. I do get a ruby2.0 binary - but scripts (which depend upon a 2.0 interpreter when executing ruby) don't pick it up. Furthermore, when I use gem et. al. to install ruby packages - these seem to be installed for ruby1.9 rather than 2.0. Very unsatisfactory.

Can anyone offer a hint as to the most straightforward way to install ruby 2.0 as the standard ruby interpreter? How am I expected to switch from ruby 1.9 to 2.0 without ruby-switch?

29

For 14.04 I found the following PPAs that come with Ruby 2.x

  • Adds Ruby 2.1 to Ubuntu 14.04

    sudo add-apt-repository ppa:brightbox/ruby-ng
    

I also found in GoRails a set of instructions to install Ruby on 14.04 but I am guessing since they are long you would want a PPA instead.

  • Adds Ruby 1.9, 2.0 or 2.1 to Ubuntu 14.04

    sudo add-apt-repository ppa:brightbox/ruby-ng-experimental
    

After you add one of them simply:

sudo apt-get update
sudo apt-get install ruby2.1 # In the case you want 2.1
  • 6
    Is it really necessary to add a 'Personal Package Archive' in order to install Ruby 2.0 in Ubuntu 14.04 - given that there is a package for "Ruby2.0" available without? – aSteve Apr 21 '14 at 17:41
  • 1
    You asked for the most straightforward way. This would be it, at least until the official 2.x version of Ruby appears on the Software Center or until the Ruby 2.0.x version updates to the actual number that it is and not like the bug you got. – Luis Alvarado Apr 21 '14 at 19:26
  • Hmm... doesn't ubuntuupdates.org/package/core/trusty/main/base/ruby2.0 suggest that 2.0 is already included in the "main" repository? – aSteve Apr 21 '14 at 19:41
  • 3
    Yes but as you mentioned it is not the 2.x branch but the 1.9. So they should fix that. – Luis Alvarado Apr 21 '14 at 20:18
  • 2
    It looks as if both the 2.0 and 1.9 interpreters are in the main repository (with a weird dependency) - but ruby-switch has been deleted for a spurious reason. – aSteve Apr 22 '14 at 8:50
8

Debian version of Ruby does not support several ruby installations on the same system. Debian obsoleted the package and Ubuntu just removed it from the repositories. You should use rbenv to switch between different ruby versions. This is the recommended way by ruby-switch package description and was the method that has been discussed in bug 737782 as preferred, through it's said that there shouldn't be more than only one version of ruby in the repositories.

Ruby 2.0 is installed by default when installing the ruby2.0 package and no other action is needed. If the package ruby was installed is recommended to remove it. You should have the binary /usr/bin/ruby2.0 available on your system.

  • 2
    Hmmm... When I installed the ruby2.0 package, I got a ruby2.0 executable and a ruby 1.9 executable. When interacting with gem, ruby1.9 seemed to be assumed as the system ruby version. On further investigation, I discovered that the scripts (while claiming to need ruby 2.0) actually run fine with ruby 1.9 - so I have abandoned the ruby2.0 package. I only wanted one ruby version - i.e. runy 2.0 - but the ubuntu 14.04 packages didn't seem to permit that. – aSteve Apr 30 '14 at 14:03
  • @aSteve in Debian I don't seem to have that problem, through Ubuntu uses that method. If you like, try creating a virtual environment for ruby 2.0 with rbenv instead, just to be on the safe side. – Braiam Apr 30 '14 at 14:30
  • 2
    i am definitely seeing that, and it makes no sense. on apt-get install ruby2.0 I get: The following NEW packages will be installed: libjs-jquery libruby1.9.1 libruby2.0 ruby ruby1.9.1 ruby2.0 rubygems-integration – Josh Nankin May 6 '14 at 22:28
  • 5
  • 1
    @jla no, actually, Debian chooses not to support several versions of Ruby, as you can see in the changelog. Debian says it will support just one version and the tools to switch versions are somehow broken, that's more or less the situation in Debian. Ubuntu decided not to make the leap for 2.0 at that time. – Braiam Oct 20 '14 at 23:26
8
sudo apt-get install ruby2.0
sudo rm /usr/bin/ruby && sudo ln -s /usr/bin/ruby2.0 /usr/bin/ruby
sudo rm -fr /usr/bin/gem && sudo ln -s /usr/bin/gem2.0 /usr/bin/gem
  • you can also ln -fs to force the update and avoid using the always scary rm -rf – njzk2 Feb 7 '16 at 22:37
5

If you want to switch between rubies, I recommend using rvm (https://rvm.io). It's a package manager and it allows you to install many different rubies (not just Matz's) on one machine, either local to the user or globally for all users.

I know you asked for a "package," so perhaps this isn't going to work for you. But I figured it might help if this is your development machine.

The benefit of doing it this way is that you can install one gemset for 2.1.1 and others for 2.0 or 1.9.3, etc. It also allows you to test jruby, rubinius, etc. without committing your system to one.

I'm not using it on production, but apparently it's suitable for that as well as development.

Here's the quickstart: https://rvm.io/rvm/install

1

Late in the game but I think a perhaps more-complete solution than hard-coding symlinks is this use of update-alternatives, posted here verbosely in case others are bitten by the same frustration and would like a more native solution. This handles all binaries and man pages, preemptively removing the references first to avoid conflicts.

Caveats:

  • I think this will need to be rerun if/when one of the ruby versions is updated.
  • I'm not a guru with update-alternatives so it's possible this is overkill and/or done kludgingly.

Script (I named fix-ruby-alternatives.sh):

#!/bin/bash

# Script to insert all potentially alternative-able files within ruby versioned packages
# Bill Evans ("r2evans")
# April 19, 2015

BIN=/usr/bin
MAN=/usr/share/man/man1
MASTER="ruby"
SLAVES=( gem irb rdoc testrb rake erb ri )
VERSIONS=( 1.9.1 2.0 )

DRYRUN="yes"
[[ "$1" = "-y" ]] && unset DRYRUN

PRI=0
for ver in ${VERSIONS[@]} ; do
    PRI=$(( ${PRI} + 1 ))
    REMCMD="update-alternatives --remove ${MASTER} ${BIN}/${MASTER}${ver}"
    CMD="update-alternatives --install ${BIN}/${MASTER} ${MASTER} ${BIN}/${MASTER}${ver} ${PRI}"
    if test -f "${MAN}/${MASTER}${ver}.1.gz" ; then
        CMD="${CMD}    --slave ${MAN}/${MASTER}.1.gz ${MASTER}.1.gz ${MAN}/${MASTER}${ver}.1.gz"
    else
        echo "#  no ${MAN}/${MASTER}${ver}.1.gz" > /dev/stderr
    fi
    for sl in ${SLAVES[@]} ; do
        if test -f "${BIN}/${sl}${ver}" ; then
            CMD="${CMD}    --slave ${BIN}/${sl} ${sl} ${BIN}/${sl}${ver}"
        else
            CMD="${CMD}    --slave ${BIN}/${sl} ${sl} /dev/null"
            echo "#  no ${BIN}/${sl}${ver}" > /dev/stderr
        fi
        if test -f "${MAN}/${sl}${ver}.1.gz" ; then
            CMD="${CMD}    --slave ${MAN}/${sl}.1.gz ${sl}.1.gz ${MAN}/${sl}${ver}.1.gz"
        else
            CMD="${CMD}    --slave ${MAN}/${sl}.1.gz ${sl}.1.gz /dev/null"
            echo "#  no ${MAN}/${sl}${ver}.1.gz" > /dev/stderr
        fi
    done

    if [[ -n ${DRYRUN} ]]; then
        echo -e ${REMCMD}
        echo -e ${CMD} | sed -e 's/\s* --slave/\n    --slave/g'
        echo "# Consider: update-alternatives --auto ruby"
    else
        ${REMCMD}
        ${CMD}
        echo "# Consider: update-alternatives --auto ruby"
    fi
done
  • IIRC you can just call ${RECMD} and ${CMD} without quotes, instead of using eval. On another note: I'll be borrowing "kludgingly". :) – muru Apr 19 '15 at 23:32
  • True ... it was a near-hasty hack. Fixed. Thanks :-) – r2evans Apr 19 '15 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.