Working with Ruby
Hi, I am Jan. This is my old Ruby blog. I still post about Ruby, but I now do it on idiosyncratic-ruby.com. You should also install Irbtools to improve your IRB.

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 https://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 https://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

Anonymous | April 30, 2010

IMHO is better to use rvm

James A. Rosen | April 30, 2010

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"

Kieran P | April 30, 2010

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.

Rohit Arondekar | May 02, 2010

+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. :)

PeterLinux | May 10, 2010

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

J-_-L | May 10, 2010

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

Jeremy Evans | May 13, 2010

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.

Jeremy Evans | May 13, 2010

I also should have mentioned ubuntu is 10.4 server i386

J-_-L | May 13, 2010

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

Jeremy Evans | May 14, 2010

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 :-)

Mark Weston | May 25, 2010

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.

Max-B | June 03, 2010

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

Kevin S | July 07, 2010

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

J-_-L | July 07, 2010

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>?

happy | September 20, 2010

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 https://www.ruby-forum.com/topic/216774, I got to know about rvm. And then I found this tute. Thank you very much!

w4ik | December 06, 2010

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

Dave Sailer | March 19, 2011

Thanks. Until I found this, everything I tried failed. RVM won't load. Installing from source gave me the same error as at https://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 (https://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.

tomcloyd | April 25, 2011

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 https://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: https://pastie.org/1830536)

Thank you!

tomcloyd | April 25, 2011

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

J-_-L | April 25, 2011

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

Anonymous | May 21, 2011

thanks for the nice writeup. thank-you.

dean | December 15, 2011

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

Rooby G | January 31, 2012

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?

Anonymous | August 07, 2012

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.