info | twitter | github
J-_-L | Writings on Ruby Programming
29.04.10 30.11.11
25 comments

RubyBuntu -1- Installing Ruby (and Rails) on ubuntu

Installing Ruby/Rails on ubuntu is not hard, but some little obstacles might be confusing.

This guide has been tested on ubuntu 10.04 “Lucid Lynx”, ubuntu 10.10 “Maverick Meerkat” and ubuntu 11.04 “Natty Narwhal”, but should also work for other debian based linux distors.

The debian dilemma

Unfortunately, on debian and ubuntu, the whole Ruby installation is split into different packages. When installing the ruby package, some parts of Ruby are not installed, for example irb or ri.

Although there is a way to install all Ruby packages with the command sudo apt-get install ruby-full, I would not recommend it. Besides getting some emacs packages (?), one (at least me) quickly loses the overview about which packages are installed and which are not..

The recommended way to install Ruby is using the Ruby Version Manager (RVM). If you do not want to depend on external scripts you must choose one of these two ways: Either using the ubuntu repositories, or building Ruby from source. You should try to not mix these approaches!

Option 0: Use Ruby Version Manager

Use this option, if there is no reason, why you should use one of the other ways ;)

RVM makes it easy to install multiple rubies and switch between them.

sudo apt-get install curl git-core
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

Add this to your ~/.bashrc (this step is not needed in newer versions of rvm):

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

Then restart your terminal or enter: source ~/.bashrc

Now, Ruby can be installed with:

sudo apt-get install libruby1.8 libruby1.9 zlib1g-dev libssl-dev libreadline-dev build-essential
rvm install ruby-1.9.3
rvm use ruby-1.9.3 --default

Please note: Do not use sudo for installing gems!

Information about how to install different versions can be found by entering rvm notes

Ruby is ready now :). More information about rvm+bundler can be found in this blog article

Option 1: Install Ruby 1.8 from the ubuntu repositories

This way is recommended, if you just want to have a working version of Ruby and do not care about the details.

The disadvantage (or advantage?) of this approach is that you depend on the ubuntu package maintainers. They decide, when to upgrade. They decide, what name schema and directory structure is used.

Anyway, first of all, you should add the ubuntu-on-rails ppa repository, sometimes they have newer versions of some Ruby components. Do it by typing:

sudo add-apt-repository ppa:ubuntu-on-rails
sudo apt-get update

Now get the essential ruby packages:

sudo apt-get install ruby rubygems irb ri rdoc rake

The dependencies should look like:
irb1.8 libreadline-ruby1.8 libreadline5 libruby1.8 rdoc1.8 ri1.8 ruby1.8 rubygems1.8

But you need more packages!

sudo apt-get install build-essential ruby1.8-dev libopenssl-ruby

These extra packages are very important – odds are high that you will need them. Unfortunately they are not in the standard package dependencies, partly because of some licensing issues – readline and openssl have incompatible licenses.

After getting all packages, there is another little step necessary: You have to add the gem path to your global PATH, so that executables new gems can be easily called from the command line. Do it by adding the following line to your ~/.bashrc:

export PATH=/var/lib/gems/1.8/bin:$PATH

Then restart your terminal or enter source ~/.bashrc

Ruby is ready now :)

A little side note: Because of a safety restriction of ubuntu, sudo does not work with executables of gems. Usually you don’t need sudo these, but exceptions might occur. To enable it, add the gem bin path to the the global /etc/bash.bashrc and apply this workaround.

Option 2: Build it from source

This way is recommended, when you want to have full control over your installation.

Do not get deterred, it is not that hard. Firstly, get the needed dev packages:

sudo apt-get install libruby1.8 libruby1.9 zlib1g-dev libssl-dev libreadline-dev build-essential

Now get fresh sources from the official web page. For 1.8, you also need a recent version of rubygems from rubygems.org. Either download and extract them manually, or use the following lines:

1.9

@wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p0.tar.gz
tar -xzf ruby-1.9.3-p0.tar.gz

1.8

wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p334.tar.gz
wget http://production.cf.rubygems.org/rubygems/rubygems-1.8.5.tgz
tar -xzf ruby-1.8.7-p334.tar.gz
tar -xzf rubygems-1.8.5.tgz

Now navigate to the extracted ruby directory and run:

./configure
make
sudo make install

To install both versions at the same time (without RVM), you can pass a --program-suffix option to one of the ./configure commands before compiling.

You can check if this was successful with: ruby -v

Optional: Install the ruby documentation (used by ri) with: sudo make install-doc

In 1.9, you should also update rubygems: sudo gem update --system

For 1.8, change to the extracted rubygems-1.6.2 directory and run:

sudo ruby setup.rb

Ruby is ready now :).

Install your gems

You should rarely (never) install gems from the ubuntu repositories (apt-get), but those from the gem repository. However, sometimes you need some additional library packages. If the installation of a gem fails, you often just need to install the right libXXXX or XXXX-dev package, and it suddenly works. To find out which one, use the ubuntu package search or your search engine.

Rails

Choose a database, for example sqlite3

sudo apt-get install sqlite3 libsqlite3-dev
sudo gem install sqlite3-ruby

or MySQL

sudo apt-get install mysql-server libmysqlclient-dev
sudo gem install mysql

and install the framework:

sudo gem install rails

Done

def encountered_errors?
  check_out common_problems!
else
  start_to have_fun! # :)
end

Creative Commons License

Comments

30.04.10

anonym

IMHO is better to use rvm

30.04.10

James A. Rosen

I agree on the suggestion to use rvm. I also don't "sudo gem install," so my .bash_profile/.bash_rc has the ~/.gem version: "export PATH=/var/lib/gems/1.8/bin:$HOME/.gem/ruby/1.8/bin:$PATH"

30.04.10

Kieran P

I use Ruby Enterprise Edition, and install it to /opt with my default user, so I never need to use sudo to install gems. Works out quite well.

02.05.10

Rohit Arondekar

+1 On using RVM. But still a nice article. One thing though, "Built it from source" should be "Build it from source". Keep writing more Ubuntu/Ruby articles. :)

10.05.10

PeterLinux

It's sudo make install-doc (not docs) on my system

10.05.10

J-_-L

@PeterLinux, thanks, fixed it.
RVM: Thanks for the feedback, I will add a little note about RVM.

12.05.10

Jeremy Evans

I have been trying now to install to a ubuntu 10.4-i386 installation for two days. All installs well but when I try to use gem to install rails, or anything else, I get ERROR: While executing gem ... (Zlib::GzipFile::Error)
not in gzip format.
zlib is installed, I followed both of the above guides and even a few others, I just can't seem to get past this one.

12.05.10

Jeremy Evans

I also should have mentioned ubuntu is 10.4 server i386

13.05.10

J-_-L

Hi Jeremy,
I tried to reproduce your error with ubuntu server edition in a virtual box and using the repositories, but the installation went well. Seems like there is really something wrong with your zlib library... Maybe it is to new or to old... Does it work for other programs? Try to reinstall zlib1g and zlib1g-dev

14.05.10

Jeremy Evans

I figured it out and now feel really stupid. I was told that the port that the computer was plugged into did not have a filter put on it. but guess what... it did. Doh. guess I should not believe everything I'm told :-)

25.05.10

Mark Weston

It's worth clarifying under "building from source" that there's a libruby1.9.1 package if one is installing Ruby 1.9.

Also, personally I'd suggest that build from source should be pretty much everyone's default option. You really want your copy of Ruby to behave exactly as documented, not to vary in small but irritating ways because of decisions made by the Debian package maintainers.

03.06.10

Max-B

I think RVM is actually one of the best option on Linux.

07.07.10

Kevin S

I've been wrestling with an issue installing the rails gem - has anyone figured out how to get around this?

:~$ sudo gem install rails
ERROR: While executing gem ... (ArgumentError)
string contains null byte

07.07.10

J-_-L

Hi Kevin,
can you give some more information, like
* have you installed ruby via repos, source or rvm?
* 1.8 or 1.9?
* what os do you use in which version?
Furhermore, what is the result of <code>sudo gem install rails --bactrace</code>?

20.09.10

happy

mate you saved me a lot of frustration trying to install the gems on Debian(lenny) which fails miserably. Now I know that rvm is the way to go.

After reading http://www.ruby-forum.com/topic/216774, I got to know about rvm. And then I found this tute. Thank you very much!

06.12.10

w4ik

J-_-L...just wanted to chime in with my thanks for a great tutorial...Thanks so much!

19.03.11

Dave Sailer

Thanks. Until I found this, everything I tried failed. RVM won't load. Installing from source gave me the same error as at http://stackoverflow.com/questions/4464985/rails-3-ruby-1-9-2-does-it-need-usr-bin-ruby1-8 ("bash: /usr/bin/rails: /usr/bin/ruby1.8: bad interpreter: No such file or directory"), but I couldn't kludge-fix it with a symbolic link. I was almost ready to go back to BitNami (http://bitnami.org/stack/rubystack), which worked before, but has a huge overhead in complexity.

Upgrading through the alternate repository didn't work either until I rebooted. Somehow, now, it's perfect. Good thing, because I was about to wipe all trace of ruby and rails and retry BitNami.

So thanks again.

25.04.11

tomcloyd

Excited to find this, but have problem. First, OS is Kubuntu Linux 10.10, and please know that I'm not a computer pro., so I get lost easily in bash, OS stuff, and ruby install stuff, but have written and use a number of ruby programs in my work (psychotherapist).

So...the "install cyrk git-cyrk" appeared to exec OK.
Then:
$ bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 185 100 185 0 0 293 0 --:--:-- --:--:-- --:--:-- 3303
bash: line 1: html: No such file or directory
bash: line 2: syntax error near unexpected token `<'
'ash: line 2: `<head><title>301 Moved Permanently</title></head>

Hoping no problem, I continued...
"sudo apt-get install libruby1.8 zlib1g-dev libssl-dev libreadline5-dev build-essential" - no problem, except maybe this:

Setting up libreadline5-dev (5.2-7build1) ...
Ignoring install-info called from maintainer script
The package libreadline5-dev should be rebuilt with new debhelper to get trigger support
Ignoring install-info called from maintainer script
The package libreadline5-dev should be rebuilt with new debhelper to get trigger support"

Again, hoping no problem, I continued:
"rvm install ruby-1.8.7
No command 'rvm' found, but there are 20 similar ones
rvm: command not found"

And it's true - "which rum" yields no response.

I have no idea what happened or what to do about it. Would appreciate any help offered.

(complete terminal output here: http://pastie.org/1830536)

Thank you!

25.04.11

tomcloyd

Found problem - tutorial needs update. From rvm website installation documentation:
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
This works!

25.04.11

J-_-L

Hi tomcloyd, thanks for the info, updated it :)

21.05.11

anonym

thanks for the nice writeup. thank-you.

15.12.11

dean

Thanks for this. Definitely needed to use the "this workaround" post to do the rails install on my version of ubuntu.

31.01.12

Rooby G

Great tutorial, after a DAY trying to figure out how to install RoR via other tutorials, this one was straight forward and easy to follow... you're a life saver!!! TY!!! By the way could you also post a video for this?

07.08.12

anonym

I just change this:
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
by this:
bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
And it worked k.

23.10.13

directorio

It is really important that you make sure none of the debian ruby or ruby-rvm packages are installed before following these steps. If they were make sure to uninstall them all with the apt-get purge option.