Thursday, July 30, 2009

Multiple versions of ruby can cause confusing errors

Probably a big "well, duh" moment for those more *nix experienced than me, but perhaps it will help some anyway.

So we upgraded ruby and rubygems on a machine last night, and after doing so found that a Rails script called by cron could no longer find the mysql gem. No problem I thought, I'll just reinstall it... but then "gem install mysql" reports an error:

/usr/local/bin/ruby extconf.rb install mysql
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***

Poo. Google quickly solves this issue:

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

mysql gem installs without a hitch, happy again! So let's run the script: KABOOM.

custom_require.rb:36:in `gem_original_require': no such file to load -- mysql (MissingSourceFile)

WTF? I just installed it?! Let's see, "gem list" shows it, irb can require it, ./script/console can retrieve records... what's going on?

Check the cron command again: hmm, we're calling /usr/bin/ruby.

ruby -v
reports 1.8.7, but
/usr/bin/ruby -v
reports 1.8.5.
which ruby
tells us /usr/local/bin/ruby. Let's try this:
mv /usr/bin/ruby /usr/bin/ruby-1.8.5
ln -s /usr/local/bin/ruby /usr/bin/ruby

Cross fingers, try our cron script again, this time it works!

1 comment:

tagaholic said...

You may want to look at this post which explains how multiple ruby versions can live side by side peacefully: http://gnuu.org/2009/06/14/installing-ruby-1-8-1-9-side-by-side/