Installing Ruby on Rails on Ubuntu

This guide is valid for the following distributions:

Ruby on Rails

While there is a nice tutorial in the Ruby on Rails wiki, it’s by no means complete. According to it, you should only type: apt-get install rails to have the newest Rails installed on Ubuntu. It installs both Ruby and Rails, but what about rubygems? Sorry, not this time. There is also another caveat. Although commands like rails test and ruby script/server are working properly, ruby/console is not. If you had the misfortune of experiencing the aforementioned behavior, then this tutorial is just for you.

Pre requirements:

nano /etc/apt/sources.list

Add the following at the end of the file (replace edgy with breezy if you are running Breezy, dapper for Dapper, etc.):

# All Ubuntu repositories
deb http://archive.ubuntu.com/ubuntu edgy main restricted universe multiverse

Update your apt sources:

apt-get update

Installation:

Install Ruby with developer’s libraries:

apt-get install ruby ri rdoc irb ri1.8 ruby1.8-dev libzlib-ruby zlib1g

Download and install Ruby Gems (no .deb package, unfortunately):

wget http://rubyforge.org/frs/download.php/17190/rubygems-0.9.2.tgz
tar xfvz rubygems-0.9.2.tgz
cd rubygems-0.9.2
ruby setup.rb

Update your RubyGems (also updates the gems cache):

gem update --system

If you get Could not find rubygems-update (> 0) in the repository or a similar error, you need to delete your RubyGems cache:

$ gem env gemdir
PATH_TO_DEFAULT_GEM_REPOSITORY
$ rm PATH_TO_DEFAULT_GEM_REPOSITORY/souce_cache

and

rm $HOME/.gem/source_cache

In the next step install the OpenSSL bindings for Ruby (needed to install signed gems). They are required if you get the following error: SSL is not installed on this system, while installing signed gems like rake:

apt-get install libopenssl-ruby

And the last one:

gem install rails -y

And this is basically it. There are, however, depending on your needs, some…

Additional steps:

One of them is setting up the Rails to connect to the MySQL database in a proper way. We will be using the MySQL C bindings, which, for one, support the MySQL old style passwords (which is set as default for Ubuntu 5.04), but are also significantly faster (in the 2-3x range) than the native Ruby MySQL bindings. First, we will need to install the gcc compiler (and libc6-dev if you don’t have it already installed). Although strange it may seem, as a default it is not installed on a clean Ubuntu installation.

apt-get install gcc libc6-dev

MySQL development libraries are also required (mysql_config plus mysql/include):

apt-get install libmysqlclient14-dev

(for MySQL 5.0 you might be better of with libmysqlclient15-dev).

And now we can install C MySQL bindings:

gem install mysql

If you get "sh: make: not found" do:

apt-get install make

or if you have it already installed, add it to your path:

export PATH=/usr/bin:"${PATH}"

And, of course, in the end install Mongrel:

gem install mongrel -y

And that’s it. Rails installation is complete. Complicated? Not really :) Happy coding!

Published by

Paweł Gościcki

Ruby/Rails programmer.

75 thoughts on “Installing Ruby on Rails on Ubuntu”

  1. Heh – I don’t have a linux box at home, so I’m tinkering (yeah – that’s a good description, since I always suffer from lack of time…) with my rails skills on winXP box with apache and mysql installation. That was rough, when I was trying to make rails talk to mysql. Later – I found out that the problem was Ruby MySql Bindings – so I had to reset my default RubyonRails account on mysql to use that new hashing method for passwords (16 bits, as I remember ?)…

    btw – as for that plugin with ratings – thanks – I think I’ll pass – I decided to not rate things in my ‘kind a’ reviews. thanks for offer anyway.

  2. You might want to play a bit with checkinstall. I haven’t used it myself, yet, but using it for installing rubygems should work, and I imagine one might as well try checkinstalling the following gems one by one. Always a bit more ‘proper’…

  3. jarv -> When I develop on Windows box in RoR I don’t have very high ‘standards’ regarding security or configuration. Meaning, that I’ve just used the one-click installation pack and it simply worked “out of the box”. Root without password, etc. :)

    Shot -> Checkinstall seems a little bit complicated, but yes, it’s definitely a cleaner way to install rubygems. Not that it matters that much, because rubygems are supposed to be a part of Ruby 1.8.4 release, due out in December.

  4. As I wrote above, I’ve yet to use checkinstall, but from what I understand, you simply prepend the ‘ruby setup.rb’ and ‘gem install …’ commands with ‘checkinstall -D’ (as in ‘checkinstall -D ruby setup.rb’ and ‘checkinstall -D gem install rails –include-dependencies’) and you end up with a simple .deb package for installation; doesn’t seem to be too complicated, at least in theory. I might be wrong, though. :)

    As for Ruby 1.8.4 coming in December, it might not make it into dapper (the same way the final Ruby 1.8.2 didn’t make it into hoary); though in this case it should be available in the -backports, so I agree that there will be an easier way to install it, anyway.

  5. Well… I’m not used to have some open services left on my machine without password – and in this case – my XPbox is connected directly (it has own IP) – so – I just don’t want to take any risks ;].

    Apart from that – I’ve got another problem – I can’t make Rails work under Apache. Got my FastCGI set up… but still no effects…. I had no time for further investigation – but sooner or later – I will have to.

    Or maybe not – as me and two of my friends are thinking about buying hosting on Dreamhost… ;]

  6. shot -> There will also be plain Debian packages with Ruby, which you could use as well (I think).

    jarv -> Try Lighttpd + FastCGI – it’s much simpler to set up than apache2. Now as for the Dreamhost, it’s great to have your site hosted there, but it’s a PITA for development work (in the change & refresh fashion) as pings from Poland to USA are well over 150ms (which kills interactivity). Development should always be done on the localhost or on LAN connected machine. As for the Dreamhost, remember to use ‘PJG’ as the promo code – it will give you a $80 discount on the first bill.

  7. Pingback: Free Opinion
  8. Check out the libgems-ruby1.8 and rubygems packages from this site:

    http://www.sgtpepper.net/hyspro/deb/unstable/

    Here’s from the README.Debian:


    Gems which Debian’s RubyGems will install are stored at /var/lib/gems instead
    of /usr/lib/ruby/gems. Executables of the gems will be put at
    /var/lib/gems/1.8/bin (for Ruby 1.8). In order to use them you manually have to
    add the directory in your PATH environment variable or make symbolic links at
    /usr/bin.

    It’s a really nice solution. You get package management for the core portion of
    rubygems and all of the locally installed gems go into /var/lib/gems, so they don’t
    touch /usr and leave files there that the Debian packages don’t know about.

    The only pain is creating the symlink for shell scripts, but all in all, not too much of
    a hassle.

  9. apt-get install libmysqlclient14-dev. i would like to install this library how i can do this.becz i m new on linux.
    if any guy can help then i’ll be thankful
    arshad

  10. Can’t get the mysql gem to install… error is:

    extconf.rb:1:in `require’: no such file to load — mkmf (LoadError)
    from extconf.rb:1
    ERROR: While executing gem … (RuntimeError)
    ERROR: Failed to build gem native extension.
    Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/mysql-2.6 for inspection.
    ruby extconf.rb install mysql\n

    Results logged to /usr/lib/ruby/gems/1.8/gems/mysql-2.6/gem_make.out

    Installed GCC and mysql development libraries…no luck.

  11. Paul, I wish I had found this a couple months ago!
    What is the reasoning behind including rails in the apt-get install, removing it and then getting it through gems? I guess by including it there several dependencies are installed. Thanks.

  12. vinbarnes -> Yes, that’s the point – you install first the required dependencies (for rails to run) and them remove it to install it more properly (imo) via ruby gems. Installing it this way has one big advantage over getting it through the apt repository – you get new rails versions the moment they are released, without waiting for someone to create the .deb packages.

  13. Paul, I gotta say that this saved me a lot of time and hairloss.

    I’ve got it up and running following your instructions. BTW, this is on a Dapper 6.06 system. The only change I had to make was replace your addition to soruces.list’ “hoary-” with “dapper-”

    Not bad!

    And I am extremely grateful!

    -Roger A

  14. To keep the thread on the cutting edge – the libgems-ruby package hit Debian’s experimental, and thus this is most probably the best place to get it.

  15. I was unable to get any of the mysql gems to work (on dapper). they all produced a “MySQL::Error: Lost connection to MySQL server during query:” failure.

    What finally did it for me was

    apt-get install libmysql-ruby1.8

    (that, and adding a socket: line to database.yml to point the /var/run/mysqld/mysqld.sock)

  16. Great tutorial. Had one problem though. Whenever I run a gem command I get:
    ERROR: While executing gem … (Gem::GemNotFoundException)
    Could not find rubygems-update (> 0) in the repository

    or, could not find rails, or rake, etc.
    gem –version yields 0.9.0

  17. cptvitamin:

    I had the rubygems error as well, reinstalling rubygems 0.8.11 (with `ruby setup.rb`) worked for me. I haven’t looked into why 0.9.0 gave the error.

  18. I have the same problem as cptvitamin

    ERROR: While executing gem … (Gem::GemNotFoundException)
    Could not find rubygems-update (> 0) in the repository

    I only get this error when running:
    sudo gem install rails or sudo gem update –system
    if I dont use sudo I get
    permission denied /usr/lib/ruby/***

    I had to sudo to do any of the apt-get stuff should I have not been sudo when untarting and running ruby setup.rb ???

    Any thoughts, ideas?

  19. Oh yeah this is on Ubuntu 6.06 and I have tried a bunch of different tutorials from the web.

  20. OK got it working cptvitamin,

    I did everything the same, but now I run:
    “gem update” NOT “gem update –system”
    I don’t know why but it doens’t like that system

  21. This tutorial isn’t working for me at all on Dapper Ubuntu 6.06. Any ideas would be appreciated!

    Here’s what I get:
    I added the line the my sources.list and when I update is only pulls down 14Bytes from each of the backports, so I think this may be the problem…

    Get:16 http://archive.ubuntu.com dapper-backports/main Packages [14B]
    Get:17 http://archive.ubuntu.com dapper-backports/restricted Packages [14B]
    Get:18 http://archive.ubuntu.com dapper-backports/universe Packages [14B]
    Get:19 http://archive.ubuntu.com dapper-backports/multiverse Packages [14B]
    Fetched 204kB in 2s (68.6kB/s)
    Reading package lists… Done

    So then I try to install (excerpts):
    un@host:~$ sudo apt-get install rails irb ri1.8 ruby1.8-dev
    E: Couldn’t find package rails

    un@host:~$ sudo apt-get install irb ri1.8 ruby1.8-dev
    E: Couldn’t find package irb

    un@host:~$ sudo apt-get install ri1.8 ruby1.8-dev
    Package ri1.8 is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source
    E: Package ri1.8 has no installation candidate

    un@host:~$ sudo apt-get install ruby1.8-dev
    The following extra packages will be installed:
    libruby1.8
    Recommended packages:
    ruby1.8
    The following NEW packages will be installed:
    libruby1.8 ruby1.8-dev

  22. Fought with this installation for hours, then i found this post. Thanks for the tutorial , worked great !!

  23. Still not working for me. I followed the directions which seemed to go just fine. I have my app working fine with Webrick. Apache2 on Dapper with fcgi – no dice. When I run ruby -d public/dispatch.fcgi I see I fail doing a require openssl in gem_openssl.rb. I have installed the openssl package described above – am I missing something else? I’ve tried to install many other base system openssl libraries and development headers, etc. Still doesn’t work.
    I tried uncommenting the !/…/ruby in the dispatch.fcgi and got different errors.. Not sure if those are valid or not.

  24. Brian -> Although I feel for you, I’m not sure I can help you. I am only able to recommend two things. First and foremost drop the FCGI altogether (it’s currently being regarded as utter crap) and go straight for Mongrel. If that does not work you could always try reinstalling the whole OS. And btw, that #!/usr/bin/ruby is not a comment it’s a required line for the script to work (it tells the shell which app to use to execute the script).

  25. Paul – your tutorial was great help. I’m sure I would have never done it without you. Thanks! cj

  26. while giving the command
    apt-get install ruby ri rdoc irb ri1.8 ruby1.8-dev libzlib-ruby
    i got the message as
    The following packages have unmet dependencies:
    irb: Depends: irb1.8 but it is not going to be installed
    rdoc: Depends: rdoc1.8 but it is not going to be installed
    ri1.8: Depends: rdoc1.8 (= 1.8.4-1ubuntu1) but it is not going to be installed
    E: Broken packages
    what can I do?

  27. Paul – I have also the same problem as Mr.Prijesh
    How can I install ruby on rails in ubuntu breezy .
    Pls give a remedy for this….

  28. Prijesh, Abhilash -> Try installing each package separately instead of all in one line. You could also try installing just “ri” instead of “ri1.8”. Also, try running apt-get clean. Oh, and try googling for “depends but it is not going to be installed”. And you could try “apt-get update && apt-get upgrade” too.

  29. Paul – Thanks for the great tips!
    I wish I had find this before I set up RoR and fail…

    http://localhost:3000/admin
    is not working for me.

    I’m new to both linux and RoR but trying to learn both.

    After the installation, how could I know RoR is running on my Ubuntu Edgy?

Comments are closed.