Ruby mysql gem error

Part of what I’m doing is actually learning Rails more than I knew it before. I’ve been working on random test apps, but now I’m trying to build something for real and I decided to start from scratch. Blew everything away, happily compiling/installing on my Mac and then I run into issues.

Basically, I got this error (I was trying to run rake db:migrate) on OS X:

dyld: NSLinkModule() error
dyld: Library not loaded: /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib
Referenced from: /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle
Reason: image not found
Trace/BPT trap

The issue is that in the most recent builds of MySQL from mysql.com (I was using 5.0.41), the libraries are no longer in lib/mysql/ but just in lib. I couldn’t remember how to modify linkage in a compiled bundle, but after a bit of googling, I found a post detailing the answer. Just run:

sudo install_name_tool -change /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib /usr/local/mysql/lib/libmysqlclient.15.dylib

/usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle

That will take care of the issue. You can use the same tool for any library that’s pointing to the wrong place for a link. Not sure why or how this happened, but at some point I’ll probably look into it and submit a patch.

  • Pingback: anything but another ruby code reference » Blog Archive » MySQL gem linking error

  • http://www.sujal.net sujal

    ?

  • sergio

    Thanks, this fixed everything.

    -s

  • http://stormsilver.net/ StormSilver

    Fantastic, thank you. Fixed things right up.

  • Ben

    you’re a life-saver, been looking for a fix for this for soooooo long, thanks a lot

  • wayne

    Thanks, worked perfectly.

  • Matthew

    Fixed it, thanks.

  • Gene

    Much appreciated. I’m sure this would have cost me a lot of time had I not found your post.

  • Sean

    Fixed worked. Thanks a lot, saved alot of time.

  • Bob

    Thanks for posting that Google fodder. :)

  • http://stormsilver.net StormSilver

    Killer. Worked like a charm. You saved me loads of searching.

  • Jacob

    I really need your help right now… I’m trying to install Radiant CMS which was built with Rails and when I launch the server I get a similar error:

    dyld: NSLinkModule() error
    dyld: Library not loaded: /usr/local/mysql/lib/libmysqlclient.15.dylib
    Referenced from: /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle
    Reason: no suitable image found. Did find:
    /usr/local/mysql/lib/libmysqlclient.15.dylib: mach-o, but wrong architecture
    Trace/BPT trap

    I tried copy and pasting the solution you provided above with no luck. What is going on? I tried starting a new Rails app (not RadiantCMS) and Mongrel launched just fine, it’s only when I try launching Radiant does it give me this error.

  • http://www.sujal.net sujal

    Jacob, that error looks like you built the mysql library on the wrong platform… was it built on an Intel Mac and you’re running on a G4 (or vice versa?).

    The mysql gem needs to be rebuilt on whatever box you’re on just to make sure…

  • Jacob

    Oh sweet, thanks for answering my cry… I’m running on a G5. Should I uninstall mysql and reinstall it? Did I pick the wrong package on mysql.com? What do you mean ‘built’ and ‘rebuilt’?

  • Jacob

    I downloaded the 32-bit over the 64-bit and the error is gone! woohoo!

  • http://www.sujal.net sujal

    Glad you figured it out. Built/Rebuilt just means this: The mysql gem, unlike many, includes a bit of code that needs to be compiled on your local system. Not sure your level of expertise, so bear with me if I’m telling you things you know.

    Roughly speaking, the settings when you compile that bit of source code need to match the settings of MySQL when it was compiled by the folks at MySQL.

    It looks like they didn’t line up in your case (64/32 would be one of those settings)

  • Sabbe n Jak

    BIG THANKS! Saves us alot of headache!!!
    LOVE from Sweden!

  • Kip

    Thanks for this fix, been banging my head for a few hours!

  • http://www.carpago.nl Raymond Loman

    I just made a soft link (ln -s . mysql) in the lib dir of de mysql-installation and it works!
    Looks pragmatic to me.

  • LB

    Thanks a lot, this was a great help.

  • donny

    I gave this guy (http://mikgen2.blogspot.com/) a little help – maybe you can do the same.

  • Pingback: How to setup Ruby and Oracle client on Intel Mac « ray_apps_blog