Installing RMagick on the Macintosh

This should be considered the fourth post in the “Digital photos and me” series. I thought this might interest a broader audience than the previous posts in the series, so I gave it title that would stand out more.

Well, it’s been a few weeks since the first three installments in this series. The demands of work are the primary reason for the delay, but the new iMac has also played a role. The prerequisites for installing RMagick—our topic for the day—are many, and when you have an older computer with lots of installed software, the prerequisites are easy to forget because they may have been installed months earlier for another purpose. But with a virgin computer, I could figure out exactly what was needed—no more, no less. So I held off on the post until I had a chance to do the installation on the new iMac. That chance came today, so I’m writing it up before I forget what I did.

First, I suppose I should mention the purpose of installing RMagick and what it has to do with digital photos. RMagick is the Ruby interface for accessing the ImageMagick library, a large set of routines for manipulating, combining, and converting digital image files. I wanted a cleaner, command-line version of my old photopage program, and I wanted to write it in Ruby rather than Python. I had used PIL, the Python Imaging Library to write photopage, a library that wasn’t going to be available for Ruby. RMagick seems to be the standard image manipulation library for Ruby, so I went with it.

Next, I’ll warn you that this article, which was linked on Daring Fireball and is ranked fairly high in a Google search on “RMagick os x,” is likely to give you trouble. As near as I can tell, it doesn’t install the fonts that ImageMagick and RMagick need, so you’ll end up with a library that can’t put text in your images. If I’m wrong about this, please educate me.

On the other hand, this article, which is also has a high Google ranking, is very good. I kept it up on one of my Safari tabs to refer to as I did my installation.

Step 1—Install the XCode development tools

These will be on your Tiger installation disk. You can also download them if you’ve signed up (for free) with Apple’s Developer Connection. The downloaded version is the best bet, as it will be the most recent. This is a normal package-type installation: you double-click on the package and it leads you through a few simple steps.

Step 2—Install DarwinPorts

The DarwinPorts project is in flux right now as it moves from one host to another. It’s also changing its name to MacPorts, but the change isn’t reflected everywhere. Don’t let the apparent schizophrenia deter you; DarwinPorts is, in my opinion, the best way to install Unix/Linux software on your Mac. If you’re not familiar with it, here is a nutshell description: it’s an automated way of downloading, configuring, compiling, and installing software with a single command.

I’ve used DarwinPorts’ main competitor, Fink, in the past, but not any more. DarwinPorts packages are usually more up-to-date and give me fewer installation problems. Yes, compiling everything does take more time (Fink can install pre-compiled packages), but I end up with a package tuned for my system. With the change to Intel processors, this is a bigger deal than it used to be.

So anyway, download the latest DarwinPorts from this repository. At present, version 1.3.2 is the latest, and because it doesn’t come in .dmg form you’ll have to download the source code, unzip it, then manually configure and compile it using the directions in the README file. This isn’t such a big deal. Open a Terminal window, go into the DarwinPorts directory and issue the commands

sudo make install

in that order. This will install DarwinPorts in /opt/local. All of the software you will now install with DarwinPorts will also go in that directory, so you need to make sure it’s in your path. I added the line

export PATH=/opt/local/bin:$PATH

to my ~/.bashrc file, then restarted the Terminal to make sure that line was read.

The main DarwinPorts program is called port, and it has many options. You’ll typically run it under sudo because it needs to read and write in places that only an administrator has permission to. The first port command you should run is

sudo port selfupdate

which will update the list of available software (which are known, somewhat confusingly, as “ports”) and do some other housekeeping. Now you’re ready to get into the meat of things.

Step 3—Install ImageMagick and its prerequisites

DarwinPorts is supposed to handle software “dependencies,” which is when one software package depends on another (what I’ve been calling prerequisites). If you ask DarwinPorts to install program A, and program A needs libraries X and Y to work, it’s supposed to install X and Y first, then go ahead and install A. All with just the one command to install A. Generally speaking, this works very well, but ImageMagick can be built in so many different ways (called “variants” in DarwinPorts-speak), I find it easier to just install the prerequisites first, then ImageMagick.

Here are the commands you need to issue:

sudo port install ghostscript
sudo port install bzip2
sudo port install zlib
sudo port install libxml2
sudo port install libiconv
sudo port install freetype
sudo port install jpeg
sudo port install libpng
sudo port install libwmf
sudo port install tiff
sudo port install lcms

You don’t have to do it in this order. In fact, if you install lcms first, you’ll find that several others will be installed automatically. In any event, don’t forget ghostscript; that’s where the fonts will come from.

Now you can install ImageMagick:

sudo port install ImageMagick

If there are errors, you should check to make sure you didn’t forget any of the prerequisites. The command

port installed

will give you a list of everything you’ve installed. You can check it against the list above.

One of the great things about DarwinPorts is how everything it installs is in the /opt/local directory. If you think you’ve really screwed up your installation, you can just delete that entire directory and start over (yes, I’ve done it—did it today, in fact). There won’t be any weird little configuration files left hanging around.

Optional Step 3a—Install a better Ruby

The Ruby that comes with OS X Tiger is version 1.8.2. This will work fine with RMagick (we’re installing RMagick, remember?), but Ruby is a fast-moving language, and 1.8.2 won’t do a lot of the cool new things with, for example, Rails or Mongrel. Also, the interactive Ruby environment, irb, packaged with Tiger wouldn’t work on my iMac. So you may want to install the DarwinPorts version of Ruby, which is currently at version 1.8.5. Just do

sudo install ruby

and you’ll soon have new Ruby at /opt/local/bin/ruby. If you’ve changed your $PATH as I suggested above, this version of Ruby will be the one that launches when you type ruby or irb at the command line. And the Apple-issued version will still be at /usr/bin/ruby if you need it.

Step 4—Install RubyGems

OK, now it’s time to install another package manager, this one specific to Ruby called RubyGems. You can download it from its rubyforge site (rubyforge, as its name implies, is like a sourceforge for ruby), unpack it somewhere convenient, and issue the command

sudo ruby setup.rb

from within the rubygems directory. This will install a new program, called gem, which works a lot like port. As with port, the first thing you should have gem update itself:

sudo gem update --system

Now you’re ready for RMagick.

Step 5—Install RMagick

sudo gem install rmagick

After about 1000 words of setup, it comes down to a very simple command. The great thing, though, is that the work you did installing DarwinPorts and RubyGems will be money in the bank if you want to install SQLite-Ruby, Rails, Rake, etc.

The next (and final, I think) post in the series will describe a Ruby program that uses RMagick to create index sheets, each containing several photos.