ChatGPT解决这个技术问题 Extra ChatGPT

Library not loaded: libmysqlclient.16.dylib error when trying to run 'rails server' on OS X 10.6 with mysql2 gem

I've been struggling with this for some time.

I've installed Rails 3, gem, mysql on my Snow Leopard machine. All was going well until I created my first project and tried to run

rails server

Upon running this I get:

jontybrook$ rails server
/Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle: dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.16.dylib (LoadError)
  Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
  Reason: image not found - /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
    from /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2.rb:7
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:64:in `require'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:64:in `require'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:62:in `each'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:62:in `require'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:51:in `each'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:51:in `require'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler.rb:112:in `require'
    from /Users/jontybrook/Dropbox/CODING/simple_cms/config/application.rb:7
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/commands.rb:28:in `require'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/commands.rb:28
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/commands.rb:27:in `tap'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/commands.rb:27
    from script/rails:6:in `require'
    from script/rails:6
jontybrook$ 

As far as I can tell the problem is with the mysql2 gem. MySQL seems to be running fine and my Gemfile references mysql2, my database.yml file seems ok also.

The error mentions

Reason: image not found - /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle

And yet

jontybrook$ cd /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2
jontybrook$ ls
client.rb   em.rb       error.rb      mysql2.bundle result.rb

MySQL2.bundle is there!?

If I use the old mysql gem, WEBrick boots fine. But that's not ideal, is it?

I've tried everything google can give me! Any help much appreciated.


b
bloveless

I was never able to get any of these answers to work for me, but this is the command that I used to make it work for me. This way you don't need to use install_name_tool every time you update your mysql

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

That is the simplest and the right way to solve that problem.
thanks for this. the marked answer above only worked for one site, but this worked permanently. Its mo' betta.
For those who are curious what's going on, this command makes a symbolic link from the second location to the first. When the gem looks for the MySQL client library under /usr/lib, that link will resolve to the location where it's actually installed. On OS X, that's usually under /usr/local/mysql/lib, which is where this command links to. If your lib is installed in a different location, you'll need to tweak this command. Type locate libmysqlclient.18.dylib and substitute the result for the first argument after -s.
@Sean D., when I run locate libmysqlclient.18.dylib I get this output WARNING: The locate database (/var/db/locate.database) does not exist. To create the database, run the following command: sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist Please be aware that the database can take some time to generate; once the database has been created, this message will no longer appear. what is wrong?
@GiH You just need to run the command it tells you (sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist). That will start the process of indexing your hard drive, which will make the locate command work in the future. In most cases, though, I think you won't need to do that, and can just use the command that luvlss provided as-is.
a
apneadiving

I have solved this, eventually!

I re-installed Ruby and Rails under RVM. I'm using Ruby version 1.9.2-p136.

After re-installing under rvm, this error was still present.

In the end the magic command that solved it was:

sudo install_name_tool -change libmysqlclient.16.dylib /usr/local/mysql/lib/libmysqlclient.16.dylib ~/.rvm/gems/ruby-1.9.2-p136/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle

Hope this helps someone else!


This has somehow reset mysql's privileges and now I am unable to login...even after performing a password reset using a file as described in the mysql docs.
Thanks, this did it for me. And for others with the same problem, if you use rvm then skip the sudo, otherwise the permissions will probably change.
The magic command works even if you aren't using RVM - just change the last argument to point to mysql2-0.2.6/lib/mysql2/mysql2.bundle in wherever your gems are installed.
Thank you so much. How on earth did you figure this out?
Thank you! I needed a slightly different command for my Mac OS X Lion running RVM 1.6.4 with Ruby 1.9.2-p180, mysql2 gem 0.3.10: sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.3.10/lib/mysql2/mysql2.bundle
n
nduplessis

This fix worked very well for me:

Add the following to to your ~/.profile

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH

http://www.rickwargo.com/2010/12/16/installing-mysql-5-5-on-os-x-10-6-snow-leopard-and-rails-3/


Thank you very much. This worked for me. (Mac os x 10.6.6i, ruby 1.9.3p327), using RVM.
Confirmed. That was all I had to add on osx Maverick 10.9.1
L
Luke

For me this was just because I had upgraded mysql but hadn't updated the mysql2 gem - a reinstall of the gem will fix it...

gem pristine mysql2


This should be the selected answer.
C
Community

I have the same issue. It looks like it's unable to find the libmysqlclient library. A temporary fix that has worked for me is the following:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/

I am not sure where the config is specifying the load path or what it's set to but my mysql install did not appear to be in it. I'll post again if I find a more permanent solution.

Edit: Actually this fix appears to more accurately address the problem.


This saved me. Thanks a lot.
B
Blender

Add the below to your ~/.bash_profile:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH

This worked for me


Yep, works on 1.9.3-p448. Shame that it's not an accepted answer.
S
Sony Mathew

In OSX El Capitan update when you do this:

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

it throws an error like

ln: /usr/lib/libmysqlclient.18.dylib: Operation not permitted

So to avoid this, what you can do is first locate libmysqlclient.18.dylib using the command

User$ locate libmysqlclient.18.dylib

In my case it returned /usr/local/mysql-5.5.24-osx10.5-x86_64/lib/libmysqlclient.18.dylib

So instead of usr/lib/ we will create symlink to usr/local/lib/ like this :

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

More details : https://forums.developer.apple.com/thread/7935


On El Capitan for me all it took was you last line: sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib
Glad it helped somebody. :D
This is the RIGHT answer for elcapitan your target link should be on the /usr/local/lib folder
S
S. Rasel

following lines works for me. I am using mac 10.7.2 .

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


This one is what fixed my issue. Thanks!
A
Anh Pham

Just ran into this problem. All I had to do is uninstall mysql2 gem and reinstall it. Hope this works for other people


On my machine, running Mavericks, I removed all version of the mysql2 gem with gem uninstall mysql2 and answering All versions at the prompt. I then ran brew upgrade mysql and then gem install mysql2.
D
DV Dasari

This is how it worked for me:

I ran the below command sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib ~/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.7/lib/mysql2/mysql2.bundle

My environments: $ rails -v Rails 3.0.6

$ mysql --version mysql Ver 14.14 Distrib 5.5.11, for osx10.6 (i386) using readline 5.1

$ ruby -v ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]

Hope this helps someone.


t
tobinjim

Thanks, Ogapo! Exporting that alias worked for me, and then I followed the link, and in my case the mysql2.bundle was up in /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle so I adjusted the install_name_tool to modify that bundle rather than one in ~/.rvm and got that working the way it should be done.

So now:

   $ otool -L /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle 
    /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle:
        /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/libruby.1.dylib (compatibility version 1.8.0, current version 1.8.7)
        /usr/local/mysql/lib/libmysqlclient.16.dylib (compatibility version 16.0.0, current version 16.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.1)

m
manuelBetancurt

hi it worked for me from the recommended link from Fredy Andersen

sudo install_name_tool -change libmysqlclient.16.dylib /usr/local/mysql /lib/libmysqlclient.16.dylib /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle

just had to change to my version of mysql, in the command, thanks


R
Ryan

I solved this problem by deleting my gemset for my current project and recreating it and rerunning bundle install. I think I caused it by installing a newer version of mysql.


C
Charles Menguy

I had this issue when working with Django, I use brew to install a lot of my Open Source programs and I needed to do the following since I used brew to install mysql:

sudo ln -s /usr/local/Cellar/mysql/5.5.20/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

Be sure to replace with your version of the libraries!


a
anothermh

bundle install mysql --force helped me. It reinstalled dependencies which ware gone as a result of brew uninstall mysql.


Unknown switches '--force'
t
tobinjim

Jonty, I'm struggling with this too.

I think there's a clue in here:

otool -L /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle

/Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle:
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/libruby.1.dylib (compatibility version 1.8.0, current version 1.8.7)
    libmysqlclient.16.dylib (compatibility version 16.0.0, current version 16.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.1)

Notice the path to the dylib is, uh, rather short?

I'm trying to figure out where the gem install instructions are leaving off the dylib path, but it's slow going as I have never built a gem myself.

I'll post more if I find more!


C
Community

I've had this exact same problem a few days ago. I eventually managed to solve it. I'm not quite sure how, but I'll tell you what I did anyway. Maybe it'll help you.

I started by downloading RVM. If you aren't using it yet, I highly recommend doing so. It basically creates a sandbox for a new separate installation of Ruby, RoR and RubyGems. In fact, you can have multiple installations simultaneously and instantly switch to one other. It works like a charm.

Why is this useful? Because you shouldn't mess with the default Ruby installation in OS X. The system depends on it. It's best to just leave the default Ruby and RoR installation alone and create a new one using RVM that you can use for your own development.

Once I created my separate Ruby installation, I just installed RoR, RubyGems and mysql, and it worked. For the exact steps I took, see my question: Installing Rails, MySQL, etc. everything goes wrong

Again: I don't know for certain this will solve your problem. But it certainly did the trick for me, and in any case using RVM is highly recommendable.


R
Roger

I still found that with the solutions above, it didn't work (for example) with the Rails plugin for TextMate. I got a similar error (when retrieving the database schema).

So what did is, open terminal:

cd /usr/local/lib
sudo ln -s ../mysql-5.5.8-osx10.6-x86_64/lib/libmysqlclient.16.dylib .

Replace mysql-5.5.8-osx10.6-x86_64 with your own path (or mysql).

This makes a symbol link to the lib, now rails runs from the command line, as-well as TextMate plugin(s) like ruby-on-rails-tmbundle.

To be clear: this also fixes the error you get when starting rails server.


l
longkt90

This usually happen when you upgrade mysql. The installed mysql2 gem which was built on the old mysql libs cannot work with new libs. Just need to reinstall it.

Uninstall mysql2 with gem uninstall mysql2. Then install it with gem install mysql2


T
Tomasz Mazur

I've resolved it by creating .rvmrc file in my project folder containg:

rvm use <yourrubie>

then entering my project path

cd ~/myprojectpath

then I run

bundle install

S
Seth

My version of luvlss's command:

Mac OSX 10.10.5

MySQL 5.6.27

Passenger 5.0.21

sudo ln -s /usr/local/mysql-5.6.27-osx10.8-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

If you're trying lots of different links, like I did, do some clean-up with:

sudo unlink /usr/lib/libmysqlclient.18.dylib


S
Slipstream

Open a Terminal and run the following:

export PATH=$PATH:/usr/local/mysql/bin

It should work.


This did the job, thank you!!! I had trouble getting Django run on Visual Studio Code. Keeps getting the error whether i installed mysqlclient on virtualenv.
A
Ariel Cabib

If you're using OSX and installed mysql using brew, you can:

brew link mysql

If you're having trouble with the version (I had mysql 5.7 running while my gem required 5.6.25), you can

brew unlink mysql
brew switch mysql 5.6.25

D
Dillawes0me

For me I had to manually uninstall mysql

brew uninstall mysql
rm -rf /usr/local/var/mysql
brew install mysql

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now