Building Ruby, Rails, LightTPD, and MySQL on Tiger

Update: There is a newer, alternate version of this tutorial here: Building Ruby, Rails, Mongrel, and MySQL on Mac OS X. If your goal is to get the latest version of Ruby on Rails up and running using the fewest steps possible, check out the newer article. If you’d like to build LightTPD and FastCGI to support your Rails install on OS X, you’re in the right place, so keep reading.

What follows are instructions for manually building and installing the following applications on Mac OS X 10.4 (Tiger).

  • Ruby on Rails 1.1
  • Ruby 1.8.4
  • LightTPD 1.4.11
  • MySQL 5.0

These instructions also cover the installation of the following supportive applications as well:

  • FastCGI 2.4.0
  • RubyGems 0.9.0
  • Readline 5.1
  • PCRE 6.6
  • FastCGI and MySQL bindings

Compiling and installing these tools this way is well worth the effort, as the end result delivers an easy-to-upgrade, system-independent, stand-alone development platform that is impervious to potential problems that can be caused by system updates, operating system upgrades, etc. These issues and additional background information about why one might roll their own tools in this fashion is detailed in the article, Using /usr/local/, which could be considered a prerequisite for this task.

A Favor

Please don’t email me asking for help. I feel bad writing that, it feels pretty crummy to say, and I truly wish that I could answer specific questions about these instructions and help figure out why a certain step might have gone wrong for you, but I just can’t.

Just make sure to follow each step (Set your path! Install Xcode!) and these instructions should “just work” for you. Hopefully, the comment system I’m implementing here soon will help fill this need and allow for more community interaction to help with problem solving.

The Concept

Basically, what we’re going to do here is download a bunch of open-source tools (some of which rely upon each other to work), configure them, compile them, and install them, one by one, until we have everything we need for a Mac OS X machine to run pretty much any Ruby on Rails application.

What’s Needed

A few things are needed to get this going:

  1. Mac OS X 10.4 (Tiger)
  2. The Developer Tools – Xcode 2.0 or newer (not installed by default!)
  3. Willingness to brave the rough underbelly of OS X and type commands into the Terminal application exactly as they appear here
  4. A tasty beverage to enjoy while things compile

A Quick Warning

While it’s unlikely anything we do here might do any kind of damage to the system, it’s good advice to have a current backup of everything, just in case. The Narrator doesn’t take any responsibility for anything that results from following these instructions. We’re following these instructions at our own risk.

Setting Up

Open the Terminal application. It can be found in the /Applications/Utilities folder.

Each of the lines below appearing in monospaced type should be entered into Terminal, and be followed by the Return key. But everybody knew that already.

Create a folder to contain all of the downloaded files and their respective build folders. For these examples, we’ll create a folder called src in the root of our home folder, and change directories into that folder. It will be our workspace:

mkdir src
cd src

It doesn’t really matter where this folder actually lives. We’ve created it in the home folder, but it could be on the Desktop, or in /usr/local/src for example. All operations should take place there.

You’ll download and compile everything from right here.

Paths

Do not skip this step! Most everything else will fail if you do.

We need to make sure that our path is set to look for files in /usr/local (also the place where we’ll be installing the tools) before looking anywhere else.

To see if the path has been set properly, we can check the contents of the .bash_login file (a special, hidden file in the root of our home folder) for a PATH line using a text editor. TextMate, TextWrangler, BBEdit, and vi are all perfectly good options. To open the file with TextMate, for example, we can type:

mate ~/.bash_login

This will open the file if it already exists, or open a blank file if it doesn’t. In either case, add the following line at the very end of the file:

export PATH="/usr/local/bin:/usr/local/sbin:$PATH" 

Now save and close the file.

It’s critical that /usr/local/bin and /usr/local/sbin come first in the path. Just having them in the path isn’t enough. They have to be first.

To make sure the changes are picked up correctly, we now need to execute the file with the following command:

. ~/.bash_login

It’s likely there will be no response from the shell here, just the prompt, but that’s OK, the changes have been picked up and we’re ready to move on.

Ruby

We’re ready to start the process. Just type (or cut and paste) each one of the following lines into Terminal, one by one. When one line finishes (some will take a while and dump a lot of information to the screen), enter the next one.

The first time we run the sudo command, and possible again later, we may be prompted for a password. We’ll just enter our own password here, and the process will continue.

We’ll start by installing Readline, a prerequisite for Ruby on OS X systems:

curl -O ftp://ftp.gnu.org/gnu/readline/readline-5.1.tar.gz
tar xzvf readline-5.1.tar.gz
cd readline-5.1
./configure --prefix=/usr/local
make
sudo make install
cd ..

If you get an error here like this:

checking whether make sets $(MAKE)... no
checking for gcc... no
checking for cc... no
checking for cc... no
checking for cl... no
configure: error: no acceptable C compiler found in $PATH

This means that you did not follow the instructions and don’t have Xcode installed. Apple provides Xcode on the install CDs/DVDs, and also for free on the Apple Developer Connection website. Run through the install, and you should be just fine.

Next up, we’ll download and install Ruby itself:

curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.4.tar.gz
tar xzvf ruby-1.8.4.tar.gz 
cd ruby-1.8.4
./configure --prefix=/usr/local --enable-pthread --with-readline-dir=/usr/local
make
sudo make install
sudo make install-doc
cd ..

RubyGems

RubyGems is a handy command-line tool for managing the installation of Ruby packages, like Rails.

Did you remember to set your path correctly? If not, this step will fail.

curl -O http://rubyforge-files.ruby-forum.com/rubygems/rubygems-0.9.0.tgz
tar xzvf rubygems-0.9.0.tgz
cd rubygems-0.9.0
sudo /usr/local/bin/ruby setup.rb
cd ..

Ruby on Rails

With RubyGems installed, Rails is a simple, one-line install:

sudo gem install rails --include-dependencies

Some people following these instructions report one of two errors after performing this step. They may see a message like this:

/usr/local/bin/gem:3:in `require': No such file to load -- rubygems (LoadError)
from /usr/local/bin/gem:3

If you see this, it means you did not set your path as instructed in the very first step.

Go back to the beginning and run that step again, then retry this step.

Some readers also report seeing an RDoc failure error. This one is actually nothing to worry about. Just re-run the original command above, or don’t. Things should be fine either way (really).

FastCGI

FastCGI is an extension to CGI (Ruby on Rails is essentially a collection of CGI scripts) that provides high performance without the limitations of server specific APIs. Don’t worry if that doesn’t make perfect sense. Just so long as it’s installed on the system:

curl -O http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
tar xzvf fcgi-2.4.0.tar.gz
cd fcgi-2.4.0
./configure --prefix=/usr/local
make
sudo make install
cd ..

We’ll also need to add the Ruby-FastCGI bindings

curl -O http://rubyforge.iasi.roedu.net/files/fcgi/ruby-fcgi-0.8.7.tar.gz
tar xzvf ruby-fcgi-0.8.7.tar.gz
cd ruby-fcgi-0.8.7
/usr/local/bin/ruby install.rb config --prefix=/usr/local
/usr/local/bin/ruby install.rb setup
sudo /usr/local/bin/ruby install.rb install
cd ..

We’re almost done with FastCGI. We just need the Ruby-FCGI Gem—another nice one-line installation:

sudo gem install fcgi

PCRE

But before we can compile LightTPD, we need to build its prerequisite, the PCRE library, a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl 5:

curl -O ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-6.6.tar.gz
tar xzvf pcre-6.6.tar.gz
cd pcre-6.6
./configure --prefix=/usr/local CFLAGS=-O1
make
sudo make install
cd ..

Note for UNIX Geeks: The CFLAGS=-O1 setting on the configure line above is there to make PCRE work with Intel Macs (such as the iMac Core Duo and the MacBook Pro). The setting has to do with GCC optimization levels, and we’re taking it down one level from the default (2) for the PCRE compile. This shouldn’t affect performance or even compile time, but if you’re on a non-Intel Mac and care about such things, feel free to leave the CFLAGS=-O1 parameter off.

LightTPD

LightTPD is an open-source webserver designed with security, speed, compliance, and flexibility in mind. It’s great for serving up Rails applications, and the latest versions of Rails are already setup to use it as the development webserver if it’s on the system.

curl -O http://lighttpd.net/download/lighttpd-1.4.11.tar.gz
tar xzvf lighttpd-1.4.11.tar.gz
cd lighttpd-1.4.11
./configure --prefix=/usr/local --with-pcre=/usr/local
make
sudo make install
cd ..

MySQL

While it’s possible to compile and install MySQL ourselves, using the OS X MySQL package is actually advantageous. Not only is the install much faster and easier, but the package includes a handy startup item and a preference panel, and the binary is tuned by the MySQL team for OS X.

Even better, the package installs MySQL right into the /usr/local/ folder, just like it should.

The install still requires a few steps:

  1. Download the MySQL 5.0 package for OS X PPC or the MySQL 5.0 package for OS X Intel
  2. Double-click the drive image to mount it
  3. Locate the MySQL installer (a file named something like mysql-standard-5.0.27-osx10.4-i686.pkg) and run it, authenticating as needed
  4. Double-click MySQLStartupItem.pkg, authenticate, and let it install
  5. Double-click MySQL.prefPane and install it, deciding whether to make it available to just the current user, or for all system users

Once the install is complete, start the MySQL server using the newly-installed control panel.

Note: MySQL installs with a default user of root which has no password. Please read this page about MySQL usernames and passwords and set a good one.

Add MySQL to Your Path

This is an important step because it allows us to run MySQL commands right from the command line, without having to preface them with the long, tedious path to the MySQL binaries folder.

As described in the directions above, edit your .bash_login file once more (perhaps using the mate ~/.bash_login command), and make your PATH statement look like this:

export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH" 

Close and then open a new Terminal window, and you’ll be all set.

MySQL Native Bindings (Optional)

This step is an optional one, but the performance gains seem to make it worth the extra step. Thanks to RubyGems, it’s another one-line install:

sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

You might see a prompt asking you “which gem to install for your platform.” Because the mysql gem is updated independently of this article, the versions may change, but your goal will be to pick the very latest “ruby” version listed. So for example, you might see the following list:

Select which gem to install for your platform (i686-darwin8.7.1)
1. mysql 2.7.1 (mswin32)
2. mysql 2.7 (ruby)
3. mysql 2.6 (ruby)
4. mysql 2.5.1 (ruby)
5. Cancel installation

You wouldn’t want to select option one because it’s for Windows (“mswin32”). Instead, you’d want to select the second option, “mysql 2.7 (ruby)” which will install the latest standard version for Ruby. It probably wouldn’t hurt to select any of the non “mswin32” versions, actually, but you usually want the latest/greatest option.

We’re Done

Phew! It’s over. We now have a properly located installation of Ruby, Ruby on Rails, MySQL, and LightTPD.

So … what’s the hold-up? Start writing those Web 2.0 applications already!

More articles in the Archive →