Note: Since this post originally went up, some of the packages have changed/updated. I’ve kept things pretty well up-to-date, but you might want to double check the latest versions and alter the bash commands accordingly. Be aware that as of this writing, the Ruby team has made 1.9.1 the latest stable and only Rails itself is deemed compatible with Ruby 1.9x. I.e., Rails passes all its own tests on 1.9.1, but most plugins, libraries, etc are likely to give you problems right now. So your best bet is to use 1.8.7 for awhile until your favorite libraries jump on board the 1.9 wagon. –Trevor (August 17, 2009)
I recently had to roll a new VM for work in order to run Rails and Sinatra apps on Apache/Passenger. My company favors CentOS as the default distro for all our boxes, so I wasn’t able to use all the super-up-to-date packages that Ubuntu makes available, and I ended up building everything from source. For posterity and for anyone else who needs it, here’s a list of what to do and how to do it.
Dependencies
You’ll need several libraries and the MySQL server, and you can use yum to install them all at once. This set includes the gcc compiler, the gcc-c++ compiler, and the zlib development headers for Ruby. Remember: not all libraries are the same — you’re going to need to make sure that you’re compiling Ruby w/ 32 or 64-bit development headers as appropriate for your architecture.
1 2 3 4 5 6 7 8 9 10 | sudo yum install httpd-devel\ openssl-devel\ zlib-devel\ gcc\ gcc-c++\ curl-devel\ expat-devel\ gettext-devel\ mysql-server\ mysql-devel |
Ruby
Next up is Ruby. We’ll install 1.8.7-p72, the latest stable as of this writing. First, we’ll make a “src” directory in /usr/local to hold everything:
1 2 3 4 5 6 7 8 | sudo mkdir /usr/local/src cd /usr/local/src sudo curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p72.tar.gz sudo tar xzvf ruby-1.8.7-p72.tar.gz cd ruby-1.8.7-p72 sudo ./configure --enable-shared --enable-pthread sudo make sudo make install |
Ok now for a weird thing — you need to remake and re-install Ruby after using it to run a script that helps you make a new makefile. This is so that you can tell it where the zlib headers live:
1 2 3 4 5 | cd ext/zlib ruby extconf.rb --with-zlib-include=/usr/include --with-zlib-lib=/usr/lib cd ../../ sudo make sudo make install |
After all that happens, you should have Ruby installed. Check and see by doing:
1 | ruby --version |
You should see something like:
1 | ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-linux] |
Rubygems
Now that you have Ruby installed, Rubygems is easy — the whole thing is in Ruby so there’s nothing to build/compile.
1 2 3 4 5 | cd /usr/local/src sudo wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz sudo tar xzvf rubygems-1.3.5.tgz cd rubygems-1.3.5 sudo ruby setup.rb |
Rails, Passenger, MySQL and Sinatra
Now that Ruby and Rubygems are installed, you can install Rails, Passenger, and Sinatra as gems:
1 | sudo gem install rails passenger sinatra |
That will take awhile, as there’s a ton of documentation to build for Rails, and Passenger has to compile some native extensions.
Once that’s done, finish the Passenger installation with their nifty installer tool:
1 | sudo passenger-install-apache2-module |
Follow the provided instructions at the end of the installer for adding lines to httpd.conf:
1 2 3 | LoadModule passenger_module /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.6/ext/apache2/mod_passenger.so PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.6 PassengerRuby /usr/local/bin/ruby |
After that, install the MySQL gem, making sure to specify where the config is:
1 | sudo gem install mysql -- --with-mysql-config=/usr/bin/mysql_config |
Disabling SELinux
The RedHat family of distros (RHEL, CentOS, Fedora) come with SELinux enabled by default. If you want to learn how to make it work with Passenger, you can try this tip from the Passenger user’s guide. I just turned it off because the box is only going to be used for these apps, and SELinux seems like overkill. Here’s how to shut it down:
First, disable it temporarily:
1 | sudo echo 0 >/selinux/enforce |
That’s not a permanent fix though, because the next time the server boots, it’ll be turned back on again. You need to edit the config file and turn it off. Open it with your favorite editor and change one line. You need to be root or running as sudo to edit this file, and I prefer Vim when on remote servers, so it’s:
1 | sudo vim /etc/sysconfig/selinux |
Change this:
1 | SELINUX=enforcing |
to this:
1 | SELINUX=disabled |
Restart Apache
You should be able to restart Apache now and have Passenger come up no problem. There are several ways to restart Apache, but I like to use the service way to keep it simple:
1 | sudo /sbin/service httpd restart |
Optional: Installing Git
For SCM, there are a lot of reasons why I like Git better than Subversion. It’s got a bit of a learning curve, but once you go Git, you never go back.
1 2 3 4 5 6 | cd /usr/local/src curl -O http://www.kernel.org/pub/software/scm/git/git-1.6.0.4.tar.gz tar zxf git-1.6.0.4.tar.gz cd git-1.6.0.4 sudo make all sudo make install |
And the man pages:
1 2 3 4 | cd /usr/local/src curl -O http://www.kernel.org/pub/software/scm/git/git-manpages-1.6.0.4.tar.gz cd /usr/local/share/man tar -zxf /usr/local/src/git-manpages-1.6.0.4.tar.gz |
Now you should have Git installed and ready. Prove it:
1 | git --version |
You should see something like:
1 | git version 1.6.0.4 |
And that’s it — you now have a full Ruby stack ready to go on CentOS 5. Much of this tutorial was adapted from the awesome Hivelogic post on installing the stack on OS X Leopard.







Copyright © 2010 Catapult Creative - info(at)catapult(hyphen)creative(dot)com - Powered by
Thanks a lot, comprehensive instructions worked without a glitch. The only difference for me was nginx instead of apache, but thanks for everything else.
I’ve walked through your tutorial 3 times now and I’m still getting this weird error:
“/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require’: no such file to load — zlib (LoadError)”
and I definitely did do the zlib steps presented in this tutorial.
Hmm… What were you doing when you got that error? If you’ve installed the zlib development headers, then you shouldn’t have this problem after you re-install Ruby per the post.
Hello,
I’m getting that error too when running ’sudo gem install rails’. I am using ‘ruby-1.8.7-p174′. I did install the httpd-devel package.
Does this output of ‘ruby extconf.rb’ look correct?
sudo ruby extconf.rb –with-zlib-include=/usr/include/ –with-zlib-lib=/usr/lib
checking for deflateReset() in -lz… no
checking for deflateReset() in -llibz… no
checking for deflateReset() in -lzlib1… no
checking for deflateReset() in -lzlib… no
checking for deflateReset() in -lzdll… no
Thanks,
Thien
Simple fix, have to use 64-bit libraries on a 64-bit system =P d’oh.
ruby extconf.rb –with-zlib-include=/usr/include –with-zlib-lib=/usr/lib64
Yeah that architecture bit will bite you — thanks for adding this comment w/ the fix! I’m sure others will find it useful. I’m adding a note to the post itself to tell people to be sure and pay attention to the 32/64 distinctions for dependencies.
[...] and updated only to find myself at version 1.8.5. Double Ugh. So, time to install from source. I ended up finding a really good writeup of the process at the fine blog Catapult Creative. The problem was that my CentOS install was ‘hardened’ per company policy so it had no [...]
Dude! Awesome documentation!! Helped me out a great bit. Just one minor request – can you please also add the location of the SELinux config file on CentOS in your post ( /etc/sysconfig/selinux ) and also mention that you must be either root or have sudo privs. to edit it ( just for sake of completeness )? Thanks. BTW, are you using Mephisto?
Doh! That’s a great catch, man — thanks. I updated the post. Regarding Mephisto: I’m not using it for this site anymore, though I used to. I like Mephisto and have worked with it a lot in the past, but I just got tired of its lack of plugins and support for blogging clients. These days, Wordpress is just too sweet to pass up for small sites and blogs. An up-and-coming Rails CMS that I really dig now is this thing called BrowserCMS. Check it out on GitHub when you get a chance — it’s very cool.
Dude thanks. Does this work with CentOS 5 with Plesk? I don’t want to ruin my plesk.
I haven’t used it with Plesk, so I’m not sure. That being said, it’s installing at the system level, so should be available to all users of your server. Not sure what that might mean for Plesk, but my instinct would be that it wouldn’t screw it up.
Thanks. I’m going to try it now and post the result. better back up first though. ^^,
hey man. help. is there another repo for rubygems? I can’t connect to
http://rubyforge.iasi.roedu.net/files/rubygems/rubygems-1.3.1.tgz I can’t find other repo. for rubygems.
If you Google “download rubygems” you’ll get RubyForge as the top link. Grab the tarball for 1.3.5 and you should be able to continue on from there.
Thanks man but this screwed up my centos5 with plesk. after installing passenger, it asked me to add some lines to httpd.conf and restart apache but it wouldn’t start.
Yeah like I said — no real experience with Plesk. One thing that might help is Apache’s syntax check command — you can run “httpd -t” and see if you have a syntax screw-up that’s keeping httpd from reloading.
THANK YOU!!! If I were a woman I’d want you to have my babies. I’m just coming from fedora where everything yum’s without a problem but the base CentOS is ruby 1.8.5. Other instructions were less than helpful but yours did the trick. Thanks for taking the time to publish this!!!
Hey you simply rocks
nice for centOS .
yum install zlib-devel
then do zlib related stuff
If
/usr/bin/ruby -v
ruby 1.8.5 (2006-08-25) [i386-linux]
then
mv /usr/bin/ruby /usr/bin/ruby.bak
cp /usr/local/bin/ruby /usr/bin/
Same things with gem can be done . this worked for me lots of time
Not sure what you are saying here, but if you’re using 1.8.5, you should upgrade if you can…
cd ext/openssl
ruby extconf.rb
cd ../..
make
make install
Ummm… great! (not sure what this is supposed to be about)
Run configure like this:
sudo ./configure –enable-shared –enable-pthread –prefix=/usr
This way the ruby binaries will be installed in /usr/bin and not in /usr/local/bin.
Great tutorial
Hi I followed your directions, only when I go to a sample app that I made I am getting the index of the sample app instead of the Welcome Aboard! page. Any thoughts??
Thank you very much for this guide !!! It all worked beautifully
Gracias amigo,
Enrique
Great job! It was EXTREMELY helpful. You have a few typos when going into folders, but other than that, perfect!
Hmmm… would you mind being a bit more specific? If there are typos, I’m glad to fix, but I’m not sure what you’re referring to.
No problem.
In the RubyGems setup code block, you have
cd rubygems-1.3.5.tgz
but should be
cd rubygems-1.3.5
There’s a similar typo in the Git install block
cd git-1.6.0.4.tar.gz
should be
cd git-1.6.0.4
Great job! This blog entry is awesome!
LOL – I can’t believe I missed that. Thanks for the heads up! It’s fixed now.
I was about to give up installing rails on CentOS at slicehost. After 5 reloads using other suggested paths, your solution worked!
Many Thanks!
I had a problem with the ruby 1.8.5 default yum package as mysql gem requires >=1.8.6 ruby.
I was hopelessly wandering for solution. Your steps worked flawlessly like magic
.
Thanks a lot. Just out of curiosity how did you manage to debug the zlib thing?
cheers
dg
Damn… it’s been so long now that I’m having a hard time remembering. Glad it worked for you though!
Why “–enable-shared” and “–enable-pthread” are required?
Thanks for the great tutorial!
The pthread flag is there because lots of things built for Ruby use pthreads. It does slow the interpreter down somewhat in 1.8.6, but that’s supposed to be fixed on the 1.8.7 branch.
The enable-shared flag just makes sure that the binary is built with support for dynamic library loading.
I’m not sure that both of these are actually required, but I’ve never seen a Ruby configure command that doesn’t use them.
I’ve just had to debug some problems after using this guide to install ruby so that I can install gems for command line programs (the sup MUA). It turns out there are a couple of other packages that should be added to the yum install at the start – ncurses-devel and readline-devel. If you could add them to your guide that might save others some pain
Also, re-doing the sudo make install didn’t actually copy the new libraries ( ext/*/*.so ) – I had to do it by hand. I documented this on http://serverfault.com/questions/174301/compiled-ruby-fails-to-find-curses/174495#174495
Other than those details, thanks for the great guide.