rails + MySQL on OSX: Library not loaded: libmysqlclient.18.dylib
Asked Answered
K

28

128

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?

Katabasis answered 11/5, 2012 at 19:33 Comment(2)
Could this be a duplicate of this? #4547198Persse
Indeed. I can't delete my own question? Voted to close as dup.Katabasis
I
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
Inhalation answered 1/6, 2012 at 9:19 Comment(10)
The symlink worked for me (after upgrading to Mountain Lion). Thanks!Haslett
Symlink does it, especially for cases like running rails from under RubyMine where .bash_profile doesn't really apply.Vtehsta
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'Patience
Altering the PATH and DYLD_LIBRARY_PATH as shown is all I needed on OS X Mavericks / 10.9.2.Battiste
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.dylibDieterich
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?Malenamalet
Its because of the new system integrety feature of El Cap, see en.wikipedia.org/wiki/…Ctenoid
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 capitanBroadsword
@Dieterich - 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!Subscription
I'm still getting an error mysql.h is missing although mysql is working with XAMPP correctly.Boisleduc
I
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.

Iconoduly answered 30/9, 2015 at 21:0 Comment(4)
Didn't need all of Alex's answer. One symbolic link did the trick.Laaspere
I did this and got: "connect': Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) (Mysql2::Error)"Cameral
@JoshHunter I believe this is a separate issue. There is a thread here #18449550 could also just be that the MySQL server is not running.Iconoduly
yeah, the server wasn't running...this fixed it. sudo /usr/local/mysql/support-files/mysql.server startCameral
R
79

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)

Rainie answered 12/1, 2016 at 6:39 Comment(6)
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 installReportorial
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.)Traumatism
Worked for me too. The problem was I moved from installing mysql w/ homebrew to the official installer.Lecture
For any python users getting here, pip uninstall mysqlclient and pip install mysqlclient also worked.Werby
'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.Libyan
Worked for me too. Thanks!Cruzcruzado
M
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.

Macao answered 17/10, 2012 at 7:0 Comment(1)
Operation not permitted (obviously with sudo) my SO version is El capitanBroadsword
A
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.

Anthropomorphic answered 3/7, 2013 at 13:2 Comment(1)
This is the solution for RubyMine.Biotite
N
15

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]

Necrosis answered 24/2, 2016 at 20:52 Comment(0)
E
10

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
Exist answered 10/10, 2015 at 9:50 Comment(0)
S
8

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

Seiber answered 4/12, 2020 at 6:47 Comment(1)
Yes, it works for the rails part, but still not work when trying to execute queries on the database.Hewes
W
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
Weiweibel answered 13/11, 2013 at 20:13 Comment(0)
R
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.

Reparative answered 9/7, 2014 at 15:22 Comment(0)
O
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
Oiler answered 31/5, 2015 at 13:44 Comment(0)
L
2

This works for me:

ln -s /usr/local/Cellar/mysql/5.6.22/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib
Lorie answered 12/5, 2016 at 20:53 Comment(1)
Ended up being a variation for me... ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylibCircumstantiality
C
2

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

brew link mysql56 --force
Compendium answered 14/10, 2017 at 15:27 Comment(2)
This is the way to link the library file...rather than use the ln -s option. Use brew link [email protected] --force for the updated versionLest
Thanks. I did brew link [email protected] --force. Worked perfectly.Kurys
B
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.

Backpack answered 9/12, 2015 at 15:3 Comment(0)
B
1

On Mac Sierra if using Homebrew then do:

sudo ln -s /usr/local/Cellar/[email protected]/5.6.34/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib
Brolly answered 27/2, 2017 at 11:17 Comment(0)
D
1
gem uninstall -aIx

and

bundle install

worked for me.

Der answered 29/4, 2017 at 21:29 Comment(0)
S
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/[email protected]/5.7.28/bin/mysql_config
Scorn answered 31/10, 2019 at 10:0 Comment(0)
E
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.

Evora answered 22/12, 2019 at 2:36 Comment(0)
P
1

There are already a lot of answer to this question, especially this one https://mcmap.net/q/173801/-rails-mysql-on-osx-library-not-loaded-libmysqlclient-18-dylib. 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/[email protected]/, so add the following to my ~/.zshrc.

MYSQL=/usr/local/opt/[email protected]/bin/
MYSQL_LIB=/usr/local/opt/[email protected]/lib/
export PATH=$PATH:$MYSQL
export DYLD_LIBRARY_PATH=$MYSQL_LIB:$DYLD_LIBRARY_PATH

Hope you fix your issues 😁

Pella answered 10/6, 2020 at 2:59 Comment(0)
M
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

Mart answered 18/1, 2014 at 9:16 Comment(0)
K
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
Kitchenette answered 7/8, 2014 at 9:35 Comment(0)
I
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.

Isahella answered 18/8, 2015 at 16:10 Comment(0)
B
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!

Bradeord answered 30/8, 2015 at 20:24 Comment(0)
F
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
Feliciafeliciano answered 1/12, 2015 at 16:56 Comment(0)
H
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.

Hydrous answered 27/2, 2016 at 17:10 Comment(0)
C
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

Cultivator answered 28/8, 2016 at 15:4 Comment(0)
A
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
Arlenarlena answered 30/12, 2019 at 16:16 Comment(0)
P
0

The winner combination for me was:

gem uninstall mysql2
bundle install
Phila answered 3/12, 2023 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.