Installing Ruby on Rails on CentOS/Redhat 4.x

This doesn’t seem to be terribly well documented, so I thought I’d post it here for those googling for a solution.

Problem

Installing Ruby on Rails on CentOS (or Redhat), the RubyGems part of the official installation instructions (or the instructions listed in this excellent SitePoint book which I’m working through at the moment) may yield this result:

# cd /usr/local/src/rubygems-0.9.2
# ruby setup.rb

< --- lib
/usr/local/lib/ruby/site_ruby/1.8/rubygems/remote_fetcher.rb:4:in `require': no such file to load -- zlib (LoadError)
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/remote_fetcher.rb:4
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:8:in `require'
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:8
from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:501:in `require'
from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:501
from /usr/local/src/rubygems-0.9.2/./post-install.rb:81:in `require'
from /usr/local/src/rubygems-0.9.2/./post-install.rb:81:in `install_sources'
from /usr/local/src/rubygems-0.9.2/./post-install.rb:116:in `try_run_hook'
from setup.rb:584:in `run_hook'
from setup.rb:1322:in `exec_task_traverse'
from setup.rb:1175:in `exec_install'
from setup.rb:894:in `exec_install'
from setup.rb:712:in `invoke'
from setup.rb:681:in `invoke'
from setup.rb:1359

Solution

A zlib error? Hrm. Double check zlib and zlib-devel are installed (I’m not sure if zlib-devel is needed but I installed it for good measure).

# yum install zlib zlib-devel

Then:

# cd /usr/local/src/ruby-1.8.5-p2/ext/zlib/
# ruby extconf.rb --with-zlib-include=/usr/include --with-zlib-lib=/usr/lib
# make
# make install

Next, install RubyGems. Note that running “ruby setup.rb” on it’s own doesn’t seem to work, try this:

# cd /usr/local/src/rubygems-0.9.2/
# ruby setup.rb config
# ruby setup.rb setup
# ruby setup.rb install

All’s good in the hood. Now continue with the rest of the installation.

# gem install rails --include-dependencies

Enjoy.

40 Responses to “Installing Ruby on Rails on CentOS/Redhat 4.x”

  1. [...] This chappie had the same problem on a different flavour of Linux. I gave up at this point and found another link. Let see how this goes… [...]

  2. Joshua says:

    Champion!!

    I was having exactly this problem so thanks for the post.

  3. Dominic Son says:

    You’ve just helped me in my now less painful process of deploying. Thanks a lot..

  4. It looks like zlib-devel is required otherwise the ruby zlib extension can’t build as it can’t find zlib’s headers.

    I’m on RHEL4 and running through your steps (notably installing zlib-devel) fixes my problem, thanks!

  5. Andy says:

    Excellent guide. Google wasn’t helping much until i found this and it solved it immediately. Great post!

  6. Thanks a ton for this. It did the trick for me on Slackware 10.2. I didn’t bother with zlib-devel and it seemed to work okay.

  7. Yuan says:

    Hey thanks bunch. I had this problem for a while, from first read of your article, i only did a apt-get of zlib, didn’t seem to work, but after doing “ruby extconf.rb –with-zlib-include=/usr/include –with-zlib-lib=/usr/lib” and recompile, all worked well.

  8. Ben Lam says:

    Christ… where do I send the beer?

    Just like Yuan said, if you’re on Ubuntu:
    apt-get install zlib1g-dev

    zlib1g should already be installed, but if it isn’t, get it.
    To show all your installed packages use:
    apt-show-versions

    If you don’t have apt-show-versions, just:
    apt-get install apt-show-versions

    Then do the ruby extconf.rb makefile magic as Lucas says above and you’re golden.

    Thanks a bunch all.

  9. Daniel says:

    the trick seems to be to install zlib before installing ruby ;)

    sincerly

  10. Ruslan says:

    THAAAAAAAAAANCK YOUUUUUUUUU!!!!
    I spent 5 hours for this problem, and You solved it for me!
    Thanck You!

  11. Digan says:

    thanks for the solution.. worked out just fine.. SPENT ALLOT OF TIME.. THanks again!

  12. Eric says:

    Thanks for the post, this helped solve all my issues!

  13. Sur says:

    Thanks a lot!!
    It worked well.

  14. Marc says:

    Thanx, this article solved my problem too!

  15. Hates_ says:

    Brilliant! Saved me hours of agony!!!

  16. Chris says:

    Thanks a lot guys. It was of good help.

  17. Jon says:

    Thanks much. This solved my problem with PCLinuxOS 2007.

  18. Sanjay says:

    thanks a million for this fix. I searched google for hours. You need to get this ranked higher on google and also have it put in the rubygems FAQ.

    -Sanjay

  19. [...]  link from :http://lucaschan.com/weblog/2007/03/22/installing-ruby-on-rails-on-centosredhat-4x/ [...]

  20. omg you rock, you saved me at least an hour of headache

  21. Joe Marty says:

    Nice work! I didn’t think it was going to work, because I had already installed rubygems, and didn’t know how to uninstall, but all I had to do was install it again, and it worked great! Thank you!

  22. BennyZen says:

    perfect!

    this was exactly the solution I needed.
    thx a lot

  23. [...] still the same Error! Finally i found something in the Weblog of Lucas Chan. Hm its about readhat/centos…. okay why not. Just go to the ruby [...]

  24. Xiong says:

    that’s awesome! i’ve met the same prob and i solved it after reading this! thanks so much!

  25. shashank says:

    awesome..did the job. You are the Man :)

  26. ricky says:

    thx alot ;)

  27. Jim Barnett says:

    Thanks for this post. I needed to duplicate a production environment of an old site and ran into this issue. Your instructions worked perfectly.

  28. bil says:

    Very helpful. As Daniel says, you do need to install zlib prior to building ruby. Also, I found that the zlib-devel is required.

  29. pavel says:

    Thanks! It was very very helpful for me. I have spent an hour finding a solution…..

  30. Thanks ever so much! I’m glad i didn’t have to reinstall the entire Ruby. Your solution worked a dream. I’m just blogging about how to install Rails on an Acer Aspire little netbook.

  31. Ruslan says:

    Thank you very very much. It was very helpful!

  32. Dennis says:

    Thank you so much, Lucas!

    This problem took me one day and one night! You gave me the exact solution to my problem!

    Bye,

    Dennis

  33. Votabrala says:

    hm… love it..

  34. Zac Zheng says:

    Thank you! Solved it for me on RHEL 5.3

  35. Thank for the solution, It’s too bad that the ./configure procedure don’t warn you about the need of the library at first

  36. princ3 says:

    You sure know how to install your ruby, thank you :)

  37. Ben says:

    Thanks – This gave me the information i needed to get it working on SLES64

  38. Roger says:

    This did the trick in CEntOS. Thank you.

  39. Ert says:

    Brill! Another CentOS thanks!

    In fact, once I got to ‘make install’, I could just head on into ‘gem update –system’

  40. Joe says:

    awesome, this is the only solution posted..rails should update their documentation

Leave a Reply