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.

Ken Bloom
  • 57,498
  • 14
  • 111
  • 168
Jonty Brook
  • 3,017
  • 3
  • 16
  • 6

24 Answers24

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
bloveless
  • 4,272
  • 1
  • 16
  • 13
  • +1 this, the more popular answer did not work for me (OS X Lion, xcode 4.1) – Joe Sak Dec 19 '11 at 23:10
  • Mac OS X Lion 10.7.3. Only this helped! Yay! :) – ExiRe May 04 '12 at 14:17
  • Kudos! That fixed the problem I was having with the mysql2 gem!! Thank you. – Ode Oct 08 '12 at 20:31
  • 12
    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`. – Siobhán Oct 31 '12 at 16:50
  • 1
    @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 Jan 24 '13 at 19:52
  • 1
    @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. – Siobhán Jan 27 '13 at 06:11
  • This fixed it for me! Thanks. [Mac OS X 10.8.2] +1 – daustin777 Jan 30 '13 at 00:47
  • @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? – bobobobo Apr 10 '13 at 18:35
  • 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. – bobobobo Apr 10 '13 at 18:40
  • 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/mysql` – Ava May 20 '13 at 16:23
  • When I run the command with the variation of both 16 and 18, I get `ln: /usr/lib/libmysqlclient.16.dylib: File exists`. – Ava May 20 '13 at 16:45
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
starball
  • 20,030
  • 7
  • 43
  • 238
Jonty Brook
  • 3,017
  • 3
  • 16
  • 6
  • 1
    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. – Coderama Feb 06 '11 at 06:31
  • 2
    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. – DanneManne Mar 07 '11 at 06:51
  • 2
    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. – Tobias Cohen Mar 15 '11 at 00:23
  • 2
    Thank you so much. How on earth did you figure this out? – Derek Mar 23 '11 at 20:45
  • It ridiculous how hard it is to connect mysql with rails. IMO another reason to use mongo – Jonathan Jul 25 '11 at 19:44
  • 7
    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`** – Randy Eppinger Nov 26 '11 at 20:33
  • It worked for me as well, except: no sudo and actual command line adjusted to point to the gemset I am using. Thanks! – Giuseppe Dec 19 '11 at 14:34
  • 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. – jackr Jun 04 '12 at 23:31
  • 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 numbers – jasonsemko Nov 21 '12 at 09:32
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/

nduplessis
  • 12,236
  • 2
  • 36
  • 53
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

Luke
  • 1,639
  • 15
  • 16
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.

Community
  • 1
  • 1
Ogapo
  • 551
  • 5
  • 9
25

Add the below to your ~/.bash_profile:

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

This worked for me

Blender
  • 289,723
  • 53
  • 439
  • 496
DV Dasari
  • 729
  • 9
  • 15
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

Sony Mathew
  • 2,929
  • 2
  • 22
  • 29
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

S. Rasel
  • 2,841
  • 1
  • 16
  • 12
12

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

Anh Pham
  • 121
  • 2
  • 2
  • 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. – Martin Streicher Jan 07 '14 at 02:46
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.

DV Dasari
  • 729
  • 9
  • 15
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!

Charles Menguy
  • 40,830
  • 17
  • 95
  • 117
3

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

anothermh
  • 9,815
  • 3
  • 33
  • 52
Aram
  • 696
  • 4
  • 16
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)
tobinjim
  • 1,862
  • 2
  • 19
  • 31
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

manuelBetancurt
  • 15,428
  • 33
  • 118
  • 216
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.

Ryan
  • 31
  • 1
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

longkt90
  • 171
  • 1
  • 5
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.

Community
  • 1
  • 1
Rits
  • 5,105
  • 4
  • 42
  • 53
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!

tobinjim
  • 1,862
  • 2
  • 19
  • 31
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.

Roger
  • 7,535
  • 5
  • 41
  • 63
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

Seth
  • 10,198
  • 10
  • 45
  • 68
AlexJ
  • 87
  • 1
  • 10
1

Open a Terminal and run the following:

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

It should work.

Slipstream
  • 13,455
  • 3
  • 59
  • 45
  • 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. – Irshu Mar 07 '20 at 11:53
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
Tomasz Mazur
  • 997
  • 7
  • 5
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
Ariel Cabib
  • 2,042
  • 22
  • 14
0

For me I had to manually uninstall mysql

brew uninstall mysql
rm -rf /usr/local/var/mysql
brew install mysql
Dillawes0me
  • 190
  • 4