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

24

207

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.

Uzzial answered 28/12, 2010 at 14:17 Comment(0)
S
395

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
Schedule answered 23/5, 2011 at 17:24 Comment(11)
+1 this, the more popular answer did not work for me (OS X Lion, xcode 4.1)Caecilian
Mac OS X Lion 10.7.3. Only this helped! Yay! :)Agripina
Kudos! That fixed the problem I was having with the mysql2 gem!! Thank you.Reinhardt
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.Glia
@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?Prouty
@Prouty 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.Glia
This fixed it for me! Thanks. [Mac OS X 10.8.2] +1Harbard
@SeanD. Is that the recommended way to do this type of thing? How're you supposed to "install a dylib" under OS X 10.8? One of the other answers?Jackinthepulpit
Oh, I understand what he's doing. You can also copy the entire libmysqlclient.18.dylib file directly without symlink. This is basically the equivalent of "Copying a DLL into C:\Windows\System32". Glad to know OS X and Windows aren't all that different after all.Jackinthepulpit
I am actually getting Library not loaded: /usr/local/lib/mysql/libmysqlclient_r.16.dylib error. I have libmysqlclient.18.dylib' in lib` but not libmysqlclient.16.dyli. Also, libmysqlclient.18.dylib is in lib folder and not mysql folder. mysql is in usr/local/bin/mysqlScrapple
When I run the command with the variation of both 16 and 18, I get ln: /usr/lib/libmysqlclient.16.dylib: File exists.Scrapple
U
93

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
Uzzial answered 30/12, 2010 at 4:57 Comment(9)
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.Restharrow
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.Grannia
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.Weinman
Thank you so much. How on earth did you figure this out?Bethanybethe
It ridiculous how hard it is to connect mysql with rails. IMO another reason to use mongoCritter
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.bundleSpontoon
It worked for me as well, except: no sudo and actual command line adjusted to point to the gemset I am using. Thanks!Bolzano
The solution "sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib …" is probably better, because the install_name_tool changes are modifying files (mysqld, or mysql2.bundle) that you might reinstall some day, and lose this fix.Blackface
This works...I'm not going to copy and paste my command but just keep in mind your ruby version AND dylib version will be different numbersHaler
G
88

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/

Gonion answered 16/2, 2011 at 10:2 Comment(2)
Thank you very much. This worked for me. (Mac os x 10.6.6i, ruby 1.9.3p327), using RVM.Complicated
Confirmed. That was all I had to add on osx Maverick 10.9.1Ratal
F
43

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

Filings answered 10/11, 2013 at 19:58 Comment(1)
This should be the selected answer.Extranuclear
T
28

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.

Toplevel answered 28/12, 2010 at 21:52 Comment(1)
This saved me. Thanks a lot.Anselma
M
25

Add the below to your ~/.bash_profile:

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

This worked for me

Maidstone answered 29/11, 2011 at 0:24 Comment(1)
Yep, works on 1.9.3-p448. Shame that it's not an accepted answer.Oriel
B
19

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

Brave answered 30/10, 2015 at 5:28 Comment(3)
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.dylibStealer
Glad it helped somebody. :DBrave
This is the RIGHT answer for elcapitan your target link should be on the /usr/local/lib folderWowser
I
13

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

Iz answered 7/1, 2013 at 15:19 Comment(1)
This one is what fixed my issue. Thanks!Chook
P
12

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

Pizarro answered 23/4, 2012 at 17:52 Comment(1)
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.Continent
M
4

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.

Maidstone answered 12/4, 2011 at 6:43 Comment(0)
A
3

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)
Anthem answered 28/12, 2010 at 23:34 Comment(0)
S
3

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

Shishko answered 24/1, 2011 at 12:40 Comment(0)
T
3

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.

Taboret answered 11/3, 2011 at 17:32 Comment(0)
P
3

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!

Peyote answered 16/4, 2012 at 5:4 Comment(0)
P
3

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

Postscript answered 12/1, 2016 at 16:22 Comment(1)
Unknown switches '--force'Wowser
B
2

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.

Busse answered 28/12, 2010 at 14:30 Comment(0)
A
2

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!

Anthem answered 28/12, 2010 at 16:54 Comment(0)
M
2

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.

Mundt answered 11/1, 2012 at 15:25 Comment(0)
J
2

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

Jamille answered 5/3, 2016 at 8:17 Comment(0)
D
1

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
Delossantos answered 13/9, 2011 at 13:11 Comment(0)
F
1

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

Fred answered 13/11, 2015 at 19:7 Comment(0)
A
1

Open a Terminal and run the following:

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

It should work.

Antidisestablishmentarianism answered 16/9, 2016 at 11:12 Comment(1)
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.Dionysius
S
0

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
Spiculate answered 19/2, 2016 at 7:1 Comment(0)
S
0

For me I had to manually uninstall mysql

brew uninstall mysql
rm -rf /usr/local/var/mysql
brew install mysql
Swint answered 5/4, 2018 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.