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?

2
  • Could this be a duplicate of this? stackoverflow.com/questions/4546698/… Commented May 11, 2012 at 19:41
  • Indeed. I can't delete my own question? Voted to close as dup. Commented May 11, 2012 at 20:21

28 Answers 28

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 
Sign up to request clarification or add additional context in comments.

10 Comments

The symlink worked for me (after upgrading to Mountain Lion). Thanks!
Symlink does it, especially for cases like running rails from under RubyMine where .bash_profile doesn't really apply.
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'
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 - 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!
|
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.

4 Comments

Didn't need all of Alex's answer. One symbolic link did the trick.
I did this and got: "connect': Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) (Mysql2::Error)"
@JoshHunter I believe this is a separate issue. There is a thread here stackoverflow.com/questions/18449050/… could also just be that the MySQL server is not running.
yeah, the server wasn't running...this fixed it. sudo /usr/local/mysql/support-files/mysql.server start
81

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)

5 Comments

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
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.)
Worked for me too. The problem was I moved from installing mysql w/ homebrew to the official installer.
For any python users getting here, pip uninstall mysqlclient and pip install mysqlclient also worked.
'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.
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.

1 Comment

Operation not permitted (obviously with sudo) my SO version is El capitan
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.

1 Comment

This is the solution for RubyMine.
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]

Comments

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 

Comments

9

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

1 Comment

Yes, it works for the rails part, but still not work when trying to execute queries on the database.
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 

Comments

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.

Comments

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 

Comments

2

This works for me:

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

1 Comment

Ended up being a variation for me... ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib
2

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

brew link mysql56 --force 

2 Comments

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 version
Thanks. I did brew link [email protected] --force. Worked perfectly.
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.

Comments

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 

Comments

1
gem uninstall -aIx 

and

bundle install 

worked for me.

Comments

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 

Comments

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.

Comments

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/[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 😁

Comments

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

Comments

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 

Comments

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.

Comments

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!

Comments

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 

Comments

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.

Comments

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

Comments

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 

Comments

0

The winner combination for me was:

gem uninstall mysql2 bundle install 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.