Saturday, July 25, 2009

Rebuild Your Ruby Gems If You Update To Snow Leopard

This post contains a command that you must absolutely issue if you are a Ruby developer upgrading to Snow Leopard.

The Commands
Update: Type the following command in terminal:
sudo gem update --system; sudo gem pristine --all
rubygems will produce some errors which are safe to ignore

For historical reasons, here's my initial solution to re-compiling all the native gems.
Fire up irb, and type the following command:
`gem list`.each_line {|line| system "sudo gem install #{line.split.first}"}

Snow Leopard introduces a disrupting change: everything runs in 64-bit mode by default. Most importantly to me, ruby is now 64-bit. This is a problem when upgrading to Snow Leopard, as opposed to doing a fresh install, because the Ruby extensions in your old gems are probably 32-bit.

A quick solution to this problem is getting all the extensions rebuilt, which is done by reinstalling all the gems.

In case you're wondering, gem update won't do the trick, because it will not rebuild all your gems.

If you're lucky, you'll get a library loading error when trying to use some gem with an extension (example: json), and you'll figure out rather quickly that you need to reinstall the gem.

A subtle symptom of the same bug is experiencing slowdowns running a Rails development server. In my case, webrick was really slow - start-up took about 30 seconds. For this reason, it's better to re-compile all the gems, as oppose to fire-fighting load error messages.

I hope this post saves you some time.


  1. Thanks so much for this, you surely saved me some up manual terminal labor.

  2. great tip.

    but remember to install xcode from snow leopard dvd before trying to rebuild the gems.

    I learned that one the hard way...

  3. Thanks for this. I'm sure you've saved me a lot of times.

    I've seen elsewhere that you should prepend any gem installs on Snow Leopard with:

    env ARCHFLAGS="-arch x86_64"

    I'm not sure if that is required or not (there is clearly a lot of confusion on the issue). I didn't do it that way and it seems OK so far (other than I have to build the MySQL gem with the appropriate flags).

    So, if you really SHOULD use the x86_64 flags the appropriate command would be

    `gem list`.each_line {|line| system 'sudo env ARCHFLAGS="-arch x86_64" gem install #{line.split.first}'}


  4. You've missed double quotes. Without them the #{} expression won't get parsed.

    `gem list`.each_line {|line| system "sudo env ARCHFLAGS=\"-arch x86_64\" gem install #{line.split.first}"}

    This will do the trick.

  5. This is great, however what about people who are not on the latest version of gems? I fell under this boat and i modified the command to like so:

    `gem list`.each_line {|line| system "sudo env ARCHFLAGS=\"-arch x86_64\" gem install #{line.split[0]} -v '#{line.split[1][1..-2]}'"}

    This will reinstall the latest version of each gem that is installed.

  6. @Aaron, anonymous, Michael: many thanks for the feedback! I have revised the post, and the new method should work for people with old versions of rubygems.

  7. I found your site while trying to figure out why Snow Leopard had switched my rails server to Webrick from Mongrel, and why it was so slow. I'm not exactly sure what fixed it, but running "sudo gem uninstall mongrel" was the last thing I did. Mongrel still shows up in my gem list, but now Snow Leopard uses that instead of Webrick, and it's really fast. That doesn't make any sense to me, but I figured it was worth sharing my experience.

  8. You are a God. I've been going crazy with getting all this to work under SL. Thank you!!!

  9. Rock and Roll dude, I had a problem when updating to snow leopard, after I completed Dan Benjamin's tutorial on updating ruby and rails for snow leopard I tried running an empty app with rails demo, then ruby script/server and mongrel just didn't start. With your post I am now running mongrel without any problem. Thanks so much.

    Iaax Page

  10. Thanks so much for posting this!

    Since my 10.6 update ruby's been driving me mad. I should've googled this sooner - sanity restored!

  11. This might also work well, it installs all versions of your current gem list.

  12. Thank you so much! You saved me much grief and time! -Oscar