125

I'm just starting out with Ruby (and rails). I did the setup according to http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:ruby gems, using rvm. I have everything working well with sqlite.

Now I'd like to try converting things over to MySQL, since that's what I do most of my development with. In my Gemfile I've replaced sqlite with mysql2:

group :development, :test do
#  gem 'sqlite3', '1.3.5'
  gem 'mysql2'
  gem 'rspec-rails', '2.9.0'
end

But when I try to create the DB for rails in MySQL I get:

$ rake db:create --trace
rake aborted!
dlopen(/Users/username/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/username/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle
  Reason: image not found - /Users/username/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle

I've seen other postings recommending re-installing MySQL via homebrew (mine was installed via a downloadable DMG), but I'd prefer not to do that as I have several other databases in there already for other non-ruby projects.

I do in fact have the file that Rails is looking for; it's installed in /usr/local/mysql/lib/libmysqlclient.18.dylib. What's the best way to tell Rails how to locate it?

The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
George Armhold
  • 30,824
  • 50
  • 153
  • 232

27 Answers27

318

The solution is pretty easy; Add the library path in your ~/.bash_profile or ~/.profile file:

MYSQL=/usr/local/mysql/bin
export PATH=$PATH:$MYSQL
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH

If it is still not working (this work for me):

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

There are many blogs with install_name_tool, which won't work for me because I'm on OSX Lion:

sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/bin/indexer
sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/bin/search
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
atejeda
  • 3,705
  • 1
  • 18
  • 11
  • 16
    The symlink worked for me (after upgrading to Mountain Lion). Thanks! – siannopollo Oct 24 '12 at 01:30
  • 5
    Symlink does it, especially for cases like running rails from under RubyMine where `.bash_profile` doesn't really apply. – maksimov Jan 12 '13 at 00:58
  • 2
    I added your DYLD_LIBRARY_PATH to .bash_profile, but I also had to uninstall the 'mysql2' gem then re-install it. like: 'gem uninstall mysql2 && gem install mysql2' – brendan Apr 17 '13 at 17:17
  • Altering the PATH and DYLD_LIBRARY_PATH as shown is all I needed on OS X Mavericks / 10.9.2. – Nick Mar 07 '14 at 14:11
  • 76
    For those who come here for 10.11 you can't symlink to `usr/lib` anymore but a symlink to `usr/local/lib` will work: `sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib` – JonathanSimmons Sep 14 '15 at 16:44
  • Glad I found this! The symlink into /usr/local/lib worked for me in El Cap, but does anyone know *why* changing DYLB_LIBRARY_PATH doesn't work? – Steve Folly Oct 24 '15 at 11:11
  • Its because of the new `system integrety` feature of El Cap, see https://en.wikipedia.org/wiki/OS_X_El_Capitan#System_Integrity_Protection – Martin M Oct 30 '15 at 10:55
  • I tried making the same commands, but it does not work, any adivce? I don-t know what I should do! I am using El capitan – Ignacio Chiazzo Dec 31 '15 at 04:56
  • 2
    @JonathanSimmons - You just saved me from pulling out the rest of my hair. The original symlink answer does not work on OS X 10.11.5, you simply end up with the error "ln: /usr/lib/libmysqlclient.18.dylib: Operation not permitted" - Everything is working now and I can finally start my work for the day...Thanks! – Colin Adams Jul 25 '16 at 16:36
  • I'm still getting an error `mysql.h is missing` although mysql is working with XAMPP correctly. – Ibrahim.H Oct 26 '20 at 16:32
129

In El Capitan I got ln: /usr/lib/libmysqlclient.18.dylib: Operation not permitted

In El Capitan /usr/lib/ now has a restricted flag and can't be written to without disabling security so I just put the link in /usr/local/lib instead.

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

Rails server is running fine again.

TinMonkey
  • 1,832
  • 1
  • 10
  • 7
  • 2
    Didn't need all of Alex's answer. One symbolic link did the trick. – gitb Oct 28 '15 at 13:37
  • I did this and got: "connect': Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) (Mysql2::Error)" – Josh Hunter Oct 28 '15 at 15:53
  • 1
    @JoshHunter I believe this is a separate issue. There is a thread here http://stackoverflow.com/questions/18449050/connect-cant-connect-to-local-mysql-server-through-socket-tmp-mysql-sock could also just be that the MySQL server is not running. – TinMonkey Oct 28 '15 at 16:13
  • yeah, the server wasn't running...this fixed it. sudo /usr/local/mysql/support-files/mysql.server start – Josh Hunter Oct 28 '15 at 22:14
72

While the title of this question describes precisely the problem I encountered, the circumstances are different from those described in the previous answers, and so was the solution.

In my case (El Capitan, mysql installed via homebrew), a brew update && brew upgrade caused the mysql package to be upgraded to 5.7.10 (from 5.6.x).

The upgrade caused libmysqlclient.18.dylib to be replaced with libmysqlclient.20.dylib, but the mysql2 gem was still relying on the former.

To fix the problem I did: gem uninstall mysql2 && gem install mysql2

Please note that similar problems can occur with different homebrew-managed libraries (see my own answer to this, for example)

Community
  • 1
  • 1
Giuseppe
  • 5,188
  • 4
  • 40
  • 37
  • Great ! I upgraded my mysql to 5.7... faced this issue..... did Following steps 1. gem uninstall mysql2 > selected option 3 2. gem install mysql2 3. added this to gemfile of project ---> gem 'mysql2', '~> 0.3.21' 4. bundle install – Udit Kapahi Jun 23 '16 at 09:53
  • 11
    **I recommend everyone try this first!** If it works you can avoid junking up your system with any of the other workarounds. Sometimes you have to rely on magical symlinks, etc, but it makes your system increasingly more and more brittle. (If it does not work, no harm is done and nothing to undo.) – Tom Wilson Dec 20 '16 at 18:51
  • Worked for me too. The problem was I moved from installing mysql w/ homebrew to the official installer. – lmiller1990 Mar 22 '18 at 02:14
  • 1
    For any python users getting here, `pip uninstall mysqlclient` and `pip install mysqlclient` also worked. – Peter Dolan Sep 13 '18 at 21:03
  • 'I will also recommend everyone to try this first!!!!!' If you have changed the mysql version after gem install then you might face this issue. So, it's better to uninstall and install mysql gem again. – Chetan Kumar Oct 19 '20 at 09:12
  • Worked for me too. Thanks! – Son Tr. Jan 13 '21 at 07:48
26
sudo ln -s /usr/local/mysql-5.5.25-osx10.6-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

That worked for me. I installed MySQL from a dmg file.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Joseph
  • 5,793
  • 4
  • 34
  • 41
16
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

Worked for me. All the similar ones didn't.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Greg Benner
  • 668
  • 7
  • 15
14

I ran into this problem after a complete removal and then fresh install of MySQL. Specifically:

Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.20.dylib

I had not even touched my Rails app.

Reinstalling the mysql2 gem solved this problem.

$ gem uninstall mysql2
$ gem install mysql2 -v 0.3.18 # (specifying the version found in my Gemfile.lock)

[MySQL 5.7.10, Rails 4.0.0, Ruby 2.0.0, Mac OS X Yosemite 10.10]

sealocal
  • 10,897
  • 3
  • 37
  • 50
9

If you are using MySQL installed from HomeBrew in El Capitan, then you should link it as follows:

sudo ln -sf /usr/local/Cellar/mysql/5.6.27/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib
arjunswaj
  • 185
  • 1
  • 7
6

For MySql 5.6 installed from DMG on Mavericks

sudo ln -s /usr/local/mysql-5.6.14-osx10.7-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
Abhishek Pande
  • 313
  • 1
  • 4
  • 7
4

I confirm patch from Abhishek does work.

it work for Yosemite, too.

note: instead of linking to a particular version of mysql, use the fact mysql already built symlink:

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

this solution does work for Xcode and C API.

Kyle Decot
  • 20,715
  • 39
  • 142
  • 263
ingconti
  • 10,876
  • 3
  • 61
  • 48
4

Just for the records: $ gem pristine mysql2 solves this for me.

Joshua Muheim
  • 12,617
  • 9
  • 76
  • 152
  • Yes, it works for the `rails` part, but still not work when trying to execute queries on the database. – Victor Mar 29 '21 at 18:38
3

To be sure what symlink is needed (depend on mysql version and os version) :

$ locate libmysqlclient.18.dylib
/usr/local/mysql-5.6.24-osx10.8-x86_64/lib/libmysqlclient.18.dylib

and so :

ln -s /usr/local/mysql-5.6.24-osx10.8-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
Fraide
  • 31
  • 4
2

This works for me:

ln -s /usr/local/Cellar/mysql/5.6.22/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib
Son
  • 1,835
  • 2
  • 18
  • 28
  • Ended up being a variation for me... `ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib` – treejanitor Sep 25 '16 at 19:13
2

For those who are using brew. Just link you mysql version with "--force" option.

brew link mysql56 --force
tagaism
  • 624
  • 2
  • 7
  • 26
  • This is the way to link the library file...rather than use the ln -s option. Use brew link mysql@5.7 --force for the updated version – Vahid Kowsari Jun 22 '18 at 23:38
  • 1
    Thanks. I did `brew link mysql@5.7 --force`. Worked perfectly. –  Nov 13 '18 at 13:51
1

I am using Rails REE (2.3.4) for a legacy system we have. After upgrading to El Capitan, running script/console enerated an error and my app would no longer start (using pow):

$ script/console
Loading development environment (Rails 2.3.4)
/blah-blah/gems/activerecord-2.3.4/lib/active_record/connection_adapters/abstract/connection_specification.rb:76:in establish_connection:RuntimeError: Please install the mysql2 adapter: gem install activerecord-mysql2-adapter (dlopen(/blah-blah/gems/mysql2-0.2.19b4/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /blah-blah/gems/mysql2-0.2.19b4/lib/mysql2/mysql2.bundle
  Reason: image not found - /blah-blah/gems/mysql2-0.2.19b4/lib/mysql2/mysql2.bundle)


From this very thread, above, I determined that I needed to issue this command in terminal:
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
This command produced an error: "ln: /usr/lib/libmysqlclient.18.dylib: Operation not permitted". I have never seen that error before.

After quite a bit of digging, I found this article: http://www.macworld.com/article/2986118/security/how-to-modify-system-integrity-protection-in-el-capitan.html and followed the instructions to turn SIP (El Capitan's new System Integrity Protection) off. After turning SIP off, and after rebooting, the ln command worked fine. Then I turned SIP off. Now all is fine. My app runs again using pow and no error running script/console. I hope this helps you.

GeezerGeek
  • 101
  • 1
  • 5
1

On Mac Sierra if using Homebrew then do:

sudo ln -s /usr/local/Cellar/mysql@5.6/5.6.34/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib
Abhishek
  • 91
  • 9
1
gem uninstall -aIx

and

bundle install

worked for me.

atomiccoder
  • 334
  • 3
  • 6
1

This worked for me. All I had to do is uninstall mysql2 gem and install it again using the below commands

gem uninstall mysql2
gem install mysql2 -v '0.3.18' -- --with-mysql-config=/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysql_config
Magesh
  • 760
  • 7
  • 16
1

I am using Mac OS, and I was stuck with this bug even after uninstalling / removing all mysql and MAMP. Earlier, I installed brew install mysql and also used MAMP. addling softlink didn't work for me.

It was only resolved by removing all existing mysql. and then install mysql through MySQL from here.

Manish Shrivastava
  • 30,617
  • 13
  • 97
  • 101
1

There are already a lot of answer to this question, especially this one https://stackoverflow.com/a/10847618/5515861. I only want to add couple of notes. If you guys using Mac, I don't know how you install the MySQL, but the first thing to investigate is where is your MySQL installation is located. For me, MySQL is installed using brew for version 5.7, and the location is /usr/local/opt/mysql@5.7/, so add the following to my ~/.zshrc.

MYSQL=/usr/local/opt/mysql@5.7/bin/
MYSQL_LIB=/usr/local/opt/mysql@5.7/lib/
export PATH=$PATH:$MYSQL
export DYLD_LIBRARY_PATH=$MYSQL_LIB:$DYLD_LIBRARY_PATH

Hope you fix your issues

abmap
  • 141
  • 5
0

use this from your command line:

sudo install_name_tool -id /usr/local/mysql-connector-c-6.1.3-osx10.7-x86_64/lib/libmysqlclient.18.dylib /usr/local/mysql-connector-c-6.1.3-osx10.7-x86_64/lib/libmysqlclient.18.dylib

tried on fews computers with maverick always works

ADAM
  • 3,903
  • 4
  • 29
  • 45
toxic
  • 1
0

If you're using Bitnami RubyStack and ran across the similar problem. Try this one

sudo ln -s /Applications/rubystack-2.0.0-17/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
julienc
  • 19,087
  • 17
  • 82
  • 82
Noel Victorino
  • 355
  • 3
  • 7
0

My issue with the loading of that bundle file was a bad symlink. So check the link, and replace it with a fresh one if needed. Everything fell into place at that point. Not sure how that happened, but it did. First time that a syntax error happened like that.

Rich_F
  • 1,830
  • 3
  • 24
  • 45
0

I was working with the rails g model command and I got this error:

Library not loaded: libmysqlclient.18.dylib

I have tried this and it functioned for me. I was using Mavericks 10.9.5

sudo ln -s /usr/local/mysql-5.6.19-osx10.7-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

Thanks!

Now I'm using Yosemite 10.10.5 and I got the same error, so I just ran this command on the terminal an it was successfully fixed up.

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

also you can try:

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

Both of them work fine for me. Hope it could be useful!

alexventuraio
  • 8,126
  • 2
  • 30
  • 35
0

I got this issue "Library not loaded: libmysqlclient.18.dylib" when importing MySQLdb from MySQL For python3:

    Traceback (most recent call last):
  File "test.py", line 3, in <module>
    import MySQLdb
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/MySQL_python-1.2.4-py3.5-macosx-10.11-x86_64.egg/MySQLdb/__init__.py", line 19, in <module>
    import _mysql
ImportError: dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/MySQL_python-1.2.4-py3.5-macosx-10.11-x86_64.egg/_mysql.cpython-35m-darwin.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/MySQL_python-1.2.4-py3.5-macosx-10.11-x86_64.egg/_mysql.cpython-35m-darwin.so
  Reason: image not found

Solution works for me: Mac OS X 10.11.1 Python3.5

Edit ~/.bash_profile:
export PATH="/opt/local/Library/Frameworks/Python.framework/Versions/3.5/bin:$PATH"
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
export PATH="/usr/local/mysql/bin:$PATH"
export PATH="/usr/local/mysql/lib:$PATH"
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
0

The only thing that worked for me is:

sudo install_name_tool -change libmysqlclient.18.dylib \
/usr/local/mysql-5.6.23-osx10.8-x86_64/lib/libmysqlclient.18.dylib \
/Library/Ruby/Gems/2.0.0/gems/mysql2-0.4.3/lib/mysql2/mysql2.bundle

Replace the paths of mysql and gems to fit your system.

Aleksandar Pavić
  • 3,143
  • 1
  • 34
  • 36
0

After a lot of googling and trying all above...the only thing that solved my problem was this command:

$install_name_tool -id /usr/local/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib

I am using a macbook pro, OSX 10 El Capitan. Darwin xxxx-MacBook-Pro.local 15.6.0 Darwin Kernel Version 15.6.0: Thu Jun 23 18:25:34 PDT 2016; XXX:xnu-3248.60.10~1/RELEASE_X86_64 x86_64 Perl:v5.18.2 Mysql:5.6.19

Tunaki
  • 132,869
  • 46
  • 340
  • 423
0

Thanks. A Homebrew upgrade made my Rails apps have issues on my Mac. I reinstalled MySQL (5.7) from source, then I had to do this

sudo ln -s /usr/local/mysql-5.7.28-macos10.14-x86_64/lib/libmysqlclient.20.dylib /usr/lib/libmysqlclient.20bdylib

based on what I read above, and in my Gemfile

gem 'mysql2', '0.5.3'

and in database.yml

adapter: mysql2
rodmclaughlin
  • 371
  • 3
  • 8