15

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?

slhck
  • 223,558
  • 70
  • 607
  • 592
BILL
  • 265
  • 1
  • 2
  • 8

2 Answers2

18

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
  • 223,558
  • 70
  • 607
  • 592
  • Also, if you are using RVM do not use sudo. – slotishtype Jun 02 '11 at 11:05
  • 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 Apr 03 '12 at 17:08
  • @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 Apr 03 '12 at 18:22
  • I feel like [rbenv](https://github.com/sstephenson/rbenv) (and [ruby-build](https://github.com/sstephenson/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 Apr 03 '12 at 18:43
  • 1
    I 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 Jul 18 '12 at 20:09
  • @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 Jul 18 '12 at 20:23
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.