How to install the latest version of Ruby and Ruby on Rails in Ubuntu?

15

5

I installed Ruby with the command apt-get install ruby1.9.1, but when I enter ruby in the console, nothing happens.

I have to use the command

ruby1.9.1-v

ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]

But how do I call it with ruby and not ruby1.9.1? And how do I install Rails?

BILL

Posted 2011-06-01T20:02:27.587

Reputation: 265

What happens if you do "which ruby", does it show as being installed? Does "ruby -v" show the right version number? – Nathaniel Bannister – 2011-06-01T20:10:18.207

Install: sudo apt-get install ruby1.9.1-full ruby -v – BILL – 2011-06-01T20:13:12.573

Program 'ruby' is not installed. – BILL – 2011-06-01T20:13:22.853

When you type "sudo apt-get install ruby1.9.1-full" in a terminal does anything happen? – slotishtype – 2011-06-01T20:24:41.570

1

I'm a little confused by your update. If you want to install rails please use RVM - http://rvm.beginrescueend.com/ - to install rails and any other gems. It is a package manager and will save you a lot of time.

– slotishtype – 2011-06-01T21:10:29.690

Answers

19

How to get a current Ruby version without messing up your system

Do not mess with your system Ruby, but instead install a current version with either rbenv or RVM. I prefer the first, but both work fine. Note that you can only install one of those at a time.

With such a Ruby version manager, you'll never have to type sudo again to install (or uninstall) a Gem, and you can keep different versions for different projects. You can safely remove these versions again.

Please make sure to read the READMEs of those tools, at least once.

Method 1 – rbenv

rbenv is a version manager for Ruby. It allows you to install a Ruby version alongside your original system Ruby, which means you cannot mess up that one, and you can easily upgrade versions.

To install it, use the rbenv-installer. Make sure to restart your shell once it's installed, and that the rbenv function works.

Then, once rbenv is installed, run rbenv install -l. This gives you a list of available Rubies. Install your chosen one with:

rbenv install 2.5.1

Now choose this one as your default:

rbenv global 2.5.1

As soon as this is done, gem can be used to run:

gem install rails

If the above does not work, you might be missing required packages for building from source. See here for a list of packages that you might want to install. On Ubuntu, these include:

sudo apt install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm5 libgdbm-dev

Method 2 – RVM

You can also install Ruby over RVM. Here as well, you don't need to sudo anything, and you'll be able to get more recent versions of Ruby alongside the system one.

Read the installation instructions for your system.

After installation, you can install Rubies with a simple command. First, check rvm list known to get the list of installable versions. Now install your choice:

rvm install 2.5.1

Then, set it as the default Ruby version for your user:

rvm use 2.5.1 --default

Now you can install Rails over gem:

gem install rails

slhck

Posted 2011-06-01T20:02:27.587

Reputation: 182 472

Also, if you are using RVM do not use sudo. – slotishtype – 2011-06-02T11:05:02.400

FYI - in the apt-get install step, you have lib1g above when this should be zlib1g. I also installed rvm with apt-get. This sets things up nicely and doesn't require you to edit your bashrc. I'm not sure if this is the best approach as I haven't tried $rvm get latest yet. – spinlock – 2012-04-03T17:08:31.657

@slhck StackExchange won't let me make the edit to the apt-get install step - edits have to be more than 6 chars and we only need to change 1. And, I'm not a fan of sudo apt-get install ruby-rvm so far. I'm getting weird bugs due to permissions (i.e. $rvm install 1.9.3 complains because it's trying to write to a directory owned by root). It looks like keeping rvm in userspace is the best option. – spinlock – 2012-04-03T18:22:53.997

I feel like rbenv (and ruby-build) should be mentioned here for completeness. It's an alternative to RVM with a cleaner method of hooking into your shell (but is practically not necessarily very different).

– JamesGecko – 2012-04-03T18:43:32.190

1I just did sudo apt-get install ruby-rvm -- is there still a need to do the crazy bash stuff to beginrescueend, whatever that is? – Jeff Atwood – 2012-07-18T20:09:08.853

@Jeff No, you'll get RVM already with this package – test it by just running the rvm command. I'm not sure whether it is the latest version though, that's all. – slhck – 2012-07-18T20:23:16.017

0

There are multiple ways to install ruby on ubuntu, but installing form the repositories is (currently) not popular. To cleanly get a non-suffixed ruby, you should build ruby yourself or use rvm.

user84696

Posted 2011-06-01T20:02:27.587

Reputation: