mysql2 ruby gem and Mac OS X: image not found

If you are using the mysql2 ruby gem on Mac OS X you may have run into this problem before.

> require 'mysql2'
LoadError: dlopen(mysql2-0.2.7/lib/mysql2/mysql2.bundle, 9):
  Library not loaded: libmysqlclient.16.dylib
  Referenced from: mysql2-0.2.7/lib/mysql2/mysql2.bundle
  Reason: image not found - mysql2-0.2.7/lib/mysql2/mysql2.bundle

So far the only solution I have found online is to use install_name_tool to update the (extremely) relative libmysqlclient.16.dylib reference to be absolute (usually to /usr/local/mysql/lib/libmysqlclient.16.dylib).

While this solves the problem, it only works until you reinstall the mysql2 gem or install a newer version and then you have to do it again. To permanently solve it you need to create a symlink of libmysqlclient.16.dylib to /usr/lib so that it can be found with that default relative path.

Ideally you wouldn’t put it in /usr/lib but I haven’t be able to find a path inside of ruby that will let you load the dylib.

sudo ln -s /usr/local/mysql/lib/libmysqlclient.16.dylib /usr/lib

Enter your password and then you can use the mysql2 gem without needing to edit the compiled bundle afterwards.

5 thoughts on “mysql2 ruby gem and Mac OS X: image not found”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>