11

I have ruby 1.9.3 and 2.1.2. When I open the terminal by default it uses ruby 1.9.3 but I want to use ruby 2.1.2.

How can I set ruby 2.1.2 to be default?

Zanna
  • 72,312
Rasool
  • 381

4 Answers4

19

/usr/bin/ruby is usually a link to /etc/alternatives/ruby, which in turn links to the executable of the default ruby version (e. g. /usr/bin/ruby1.9). You can change the configured default version with:

sudo update-alternatives --config ruby

If the desired version did not set up itself as alternative for ruby (e. g. if it isn't set up by the package manager), you can do it yourself with

sudo update-alternatives --install /usr/bin/ruby ruby /path/to/ruby2.1 <PRIORITY>

where <PRIORITY> is a positive integer. Then you can perform the first step.

For details see the manual of update-alternatives(8).

David Foerster
  • 36,890
  • 56
  • 97
  • 151
6

You should install RVM to manage your Ruby versions. To install RVM do

sudo apt-get install zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2 libxml2-dev libxslt-dev gawk libgdbm-dev libncurses5-dev automake libtool bison libffi-dev nodejs
\curl -sSL https://get.rvm.io | bash -s stable

If that breaks for some reason then follow the instructions here: https://stackoverflow.com/a/9056395/2376036

Then to make the default 2.1.2 do

rvm --default use 2.1.2
Termhn
  • 401
  • 2
  • 7
5

On Ubuntu 14.04 you can install ruby2.0 package. Currently the package offers version 2.0.0p384 (eventually the package might offer Ruby 2.1 or newer). This is the simples way how to install ruby binaries:

sudo apt install ruby2.0

Then you'll have binaries ruby2.0 and gem2.0. For replacing default ruby 1.9 by 2.0 you can do following (NOTE: you might break applications that strictly requires Ruby <= 1.9).

sudo update-alternatives --set /usr/bin/ruby ruby /usr/bin/ruby2.0 10
sudo update-alternatives --set /usr/bin/gem gem /usr/bin/gem2.0 10

Other possibilities are using some ruby version manager like rbenv or RVM. The first one is more complicated to setup, but less hacky than RVM. Both allow install almost any version of Ruby you would like to use.

Tombart
  • 999
3

If you’re using Ubuntu 14.04 (Trusty) or newer then you can add this PPA:

sudo apt-get install software-properties-common
sudo apt-add-repository ppa:brightbox/ruby-ng
sudo apt-get update

Then you can install whichever package you want, for example

sudo apt-get install ruby2.2

To be able to build native extensions you'll need to dev package too

sudo apt-get install ruby2.2-dev

To easily switch between installed Ruby versions, install ruby switch:

sudo apt-get install ruby-switch

Commands of utiliy

ruby -v
ruby-switch --list

Example

sudo ruby-switch --set ruby2.1

From: https://www.brightbox.com/docs/ruby/ubuntu/

Zanna
  • 72,312
Gamaliel
  • 131