For internal projects that require Linux we have started to use Ubuntu Server as our default platform. Every Linux distribution comes with its own peculiarities. Though a great distribution we have struggled with the pre-built Ruby and RubyGems packages that stem from its Debian heritage. In this post I want to outline how you can bypass these issues by compiling Ruby and RubyGems directly from source.
Even though we offer a comprehensive, pre-built Ruby on Rails stack that nicely works on Ubuntu there are circumstances where we sometimes need to optimize a Ruby installation either for size or the target platform.
In our experience a simple “apt-get install ruby” will not implicitly provide all the things that you will need for running Ruby on Rails applications on a bare Ubuntu installation. Even if you happen to install all necessary packages (e.g. ruby1.8-dev, irb1.8, rdoc1.8, etc.) your system will still lag behind several Ruby patchlevels.
Another challenge is RubyGems. We noticed odd failures when trying to upgrade RubyGems 0.9.4 from the Ubuntu repositories to its most recent version 1.0.1.
Since we couldn’t find current and complete instructions for building Ruby on Ubuntu we decided to share them here. We have tested them on Ubuntu 7.10 and 6.06 LTS. There is certainly no magic involved. The whole procedure is straight forward if you are familiar with building Unix software from source.
Prerequisites
As a prerequisite you will need to have the necessary build tools and libraries installed. Below instruction line will get you there:
apt-get -y install build-essential libssl-dev libreadline5-dev zlib1g-dev
Of these packages OpenSSL is needed by Ruby for certain cryptographic functions and accessing web services over SSL. Readline is used by irb to offer interactive history and completion capabilities.
Installing Ruby
As a first step download the latest Ruby version from http://www.ruby-lang.org. When you configure the project make sure to require library support for openssl, zlib and readline. Most Rails environments will make use of these.
RUBY="ruby-1.8.6-p111"
wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/$RUBY.tar.gz
tar xzf $RUBY.tar.gz
cd $RUBY
./configure --prefix=/usr/local --with-openssl-dir=/usr --with-readline-dir=/usr --with-zlib-dir=/usr
make && make install && make install-doc
cd ..
By default, Ruby will be installed under /usr/local. A simple which ruby should now point you to /usr/local/bin/ruby. A quick ruby -v should report the version and patchlevel from the downloaded Ruby source package. In our case it is ruby 1.8.6 (2007-09-24 patchlevel 111) [x86_64-linux].
By default, the bash configuration in Ubuntu includes a path to /usr/local/bin. That’s why the above statements work out of the box without changes to your shell configuration.
To make sure that all the necessary libraries are accessible from within Ruby use the following command line:
ruby -ropenssl -rzlib -rreadline -e "puts :success"
If this command only returns the string success the installation is working as intended.
To speed up the compilation process you can skip the documentation generation step make install-doc by removing it from the above make line.
Installing RubyGems
Once your custom Ruby environment is available you can proceed to install RubyGems. The latest version as of this writing is 1.0.1:
RUBYGEMS="rubygems-1.0.1"
wget http://rubyforge.org/frs/download.php/29548/$RUBYGEMS.tgz
tar xzf $RUBYGEMS.tgz
cd $RUBYGEMS
/usr/local/bin/ruby setup.rb
cd ..
Again you can test the installation with which gem and gem -v. It should be located in /usr/local/bin.
That’s it. Now you have your custom compiled Ruby and RubyGems environment on Ubuntu. Add the necessary gems for deploying your Rails application and you are set. For example:
gem install rails
gem install mongrel
...















Continued Discussion
5 responses to this entry
Thank you for the post.
I was strugging with the zlib/rubygems combination, getting CRC error when installing Rails.
It seems that compiling ruby with “—with-zlib-dir=/usr” and having run “apt-get install zlib1g-dev” solved my problem.
on March 05, 2008 at 01:48 PM
What a great help!
Thank you.
on April 10, 2008 at 02:18 AM
Nice! This is what you need for Ubuntu 7.10
Thanks!
on May 04, 2008 at 06:04 AM
Hi,
also i tried creating a rails app, and when i run webrick it throws an error saying openssl not found
on May 20, 2008 at 08:46 AM
this saved my life big time! I have been struggling with redmine installation on Ubuntu server. I kept getting errors at the redmine database creation step (sudo rake db:migrate RAILS_ENV=”production”), everytime I fix something, I would get another error. Now everything is working well, thanks again.
Listing the errors that I got for google: E: Couldn’t find package rubygems uninitialized constant Gem::GemRunner uninitialized constant ApplicationController::REDMINE_SUPPORTED_SCM undefined method `register_template_handler’
on June 02, 2008 at 10:57 PM