Tuesday, November 30, 2010

Building MySQL multiarch (32/64 bit) client libraries on OS X

Getting the mysql gem to build can be a pain. Especially if you have installed the 64-bit version of mysql and are using a 32-bit version of ruby

You can check the build arch of an executable/library via the file command, such as:

$ file `which ruby`
/Users/timcharper/Developer/.rvm/rubies/ruby-1.8.6-p383/bin/ruby: Mach-O executable i386
$ file /usr/local/mysql/lib/libmysqlclient.dylib 
/usr/local/mysql/lib/libmysqlclient.dylib: Mach-O 64-bit dynamically linked shared library x86_64

See how those disagree? If you're using homebrew (which you should be!), you can build the client libs/tools only (universal style) as follows:

brew install mysql --client-only --universal

Then, to install the gem, you need to pass some fancy flags to get the mysql gem to link against your new libraries:

HOMEBREW_PREFIX=/usr/local # if your prefix differs, modify this line

gem install mysql -- --with-opt-include=$HOMEBREW_PREFIX/include/mysql/ --with-opt-lib=$HOMEBREW_PREFIX/lib/mysql --without-mysql-config

Your gem should build with no errors, and your new shiny universal mysql client libraries should serve you well in the future if you use both 32-bit and 64-bit rubies.