121

I dont know how to install the latest Ruby on Ubuntu.

First I installed the default Ruby 1.9.3, using

sudo apt-get install ruby

Then I tried to install the 2.0 version using

sudo apt-get install ruby2.0

My version of Ruby is still "ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux])"

What should I do?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Alek
  • 1,461
  • 2
  • 13
  • 14
  • [this answer](http://stackoverflow.com/a/34523631/4233593) was very helpful for installing the ruby version manager, which allows you to checkout and use any version you want whenever you want in your home environment without need for `sudo` – Jeff Puckett Jul 24 '16 at 04:16

5 Answers5

206

There is a PPA with up-to-date versions of Ruby 2.x for Ubuntu 12.04+:

$ sudo apt-add-repository ppa:brightbox/ruby-ng
$ sudo apt-get update
$ sudo apt-get install ruby2.4

$ ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux-gnu]
Taz
  • 1,235
  • 9
  • 16
Vlad Frolov
  • 7,445
  • 5
  • 33
  • 52
  • How do I select `ruby` to be `ruby2.3` though? – njzk2 Mar 31 '17 at 15:03
  • 1
    @njzk2 Try `sudo update-alternatives --config ruby` and follow the instructions there. On a fresh installation without official Ubuntu ruby package, the PPA package configured `ruby` alias to `ruby2.3` automatically for me. – Vlad Frolov Mar 31 '17 at 19:18
  • While this way is far easier to do, using a PPA is not really the 'best' way, PPAs can go away, often haven't been as carefully put together as official channels etc. A PPA is a repository one person has set up and runs themselves. Just be careful out there. – John Hunt Nov 17 '17 at 11:38
134

First of all, install the prerequisite libraries:

sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev

Then install rbenv, which is used to install Ruby:

cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

rbenv install 2.3.1
rbenv global 2.3.1
ruby -v

Then (optional) tell Rubygems to not install local documentation:

echo "gem: --no-ri --no-rdoc" > ~/.gemrc

Credits: https://gorails.com/setup/ubuntu/14.10

Warning!!! There are issues with Gnome-Shell. See comment below.

dubadub
  • 3,312
  • 3
  • 23
  • 28
  • 1
    It's worth noting that the user may have to run the following command as well, before installing ruby 2.2.0 sudo apt-get install libffi-dev – ThaDick Feb 10 '15 at 20:19
  • Warning! Added those lines to your .bash* files may make Gnome-Shell not load. I couldn't login until I removed those lines. – Cerin Apr 28 '15 at 05:41
  • Thnaks, @Cerin. Added warning to post. Did you manage this issue? How do you add a path? – dubadub Apr 30 '15 at 11:02
  • 5
    No, I never truly resolved this. I tried installing rvm instead, but apparently the installer (from https://get.rvm.io) detects Ubuntu and literally refuses to install, redirecting to a SO question which in turn recommends running the very same installer... So I decided instead to completely uninstall Ruby from my system and switch over all my Ruby tools to Python equivalents. – Cerin Apr 30 '15 at 16:21
  • Is the gnome-shell issue related to how the PATH variable is being modified? I've always read that new paths should be appended to PATH, not prepended. I've also read that environment variables should be added to ~/.profile or ~/.bash_profile instead of ~/.bashrc. – japhyr Mar 07 '16 at 14:49
  • After the above steps to install ruby, you can use gem to install or use the ruby related things eg, 1. sass - gem install sass 2. compass - gem install compass – Wasim Khan Aug 28 '16 at 11:54
  • I followed the procedure to the tee. However after installation, when I check the version it shows me ruby 1.9.3 is installed. What am I doing wrong? – Yahya Oct 19 '16 at 17:42
  • @Vivek please try it again, I've updated rbenv references – dubadub Oct 19 '16 at 18:50
40

Best is to install it using rvm(ruby version manager).
Run following commands in a terminal:

sudo apt-get update
sudo apt-get install build-essential make curl
\curl -L https://get.rvm.io | bash -s stable
source ~/.bash_profile
rvm install ruby-2.1.4

Then check ruby versions installed and in use:

rvm list
rvm use --default ruby-2.1.4

Also you can directly add ruby bin path to PATH variable. Ruby is installed in

$HOME/.rvm/rubies export PATH=$PATH:$HOME/.rvm/rubies/ruby-2.1.4/bin
julka
  • 1,172
  • 2
  • 13
  • 29
user3301099
  • 517
  • 3
  • 3
  • 12
    Piping arbitrary code over the net is a bad idea. Here's a "Wall of Shame" showing offending websites: curlpipesh.tumblr.com – labyrinth Jan 21 '15 at 22:34
  • 1
    `gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3` – Brett Aug 22 '16 at 14:15
  • JECompton: Better to talk to the folks at rvm about that, as this is the canonical method of installing rvm. – Lonny Eachus Jul 14 '17 at 20:43
6

Use RVM (Ruby Version Manager) to install and manage any versions of Ruby. You can have multiple versions of Ruby installed on the machine and you can easily select the one you want.

To install RVM type into terminal:

\curl -sSL https://get.rvm.io | bash -s stable

And let it work. After that you will have RVM along with Ruby installed.

Source: RVM Site

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Bartosz Łęcki
  • 332
  • 2
  • 6
  • 1
    To install RVM, follow the directions on [their installation page](http://rvm.io/rvm/install). There is a lot more to using RVM than running cURL, and the installation page goes over that. It should be read before the installation to become familiar with what it's going to do, including troubleshooting steps. – the Tin Man Oct 28 '14 at 00:05
  • @theTinMan Of course I fully agree with you. That's why I added their page as source so author of this thread can read more about it. – Bartosz Łęcki Oct 28 '14 at 10:00
  • Well, the home page for RVM is one thing, but most of the RVM questions we have here could have been solved immediately if the people had read the whole installation page. – the Tin Man Oct 28 '14 at 15:10
  • 1
    Piping arbitrary code over the net is a bad idea. Here's a "Wall of Shame" showing offending websites: curlpipesh.tumblr.com – labyrinth Jan 21 '15 at 22:35
5

update ubuntu:

 sudo apt-get update
 sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev

Install rvm, which manages the ruby versions:

to install rvm use the following command.

 \curl -sSL https://get.rvm.io | bash -s stable
 source ~/.bash_profile
 rvm install ruby-2.1.4

Check ruby versions installed and in use:

rvm list
rvm use --default ruby-2.1.4
Sudhir Vishwakarma
  • 785
  • 10
  • 15