rails + MySQL on OSX: Library not loaded: libmysqlclient.18.dylib
Solution 1
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
Solution 2
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.
Solution 3
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)
Solution 4
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.
Solution 5
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
Worked for me. All the similar ones didn't.
George Armhold
I'm a consultant, currently focusing on the following topics: Go! (Golang) Ruby, Ruby on Rails Docker and containers more generally search (ElasticSearch, Apache Solr & Bleve) I've worked on lots of other stuff in the past, including: Java Android and iOS apps Please visit my website for details.
Updated on December 04, 2020Comments
-
George Armhold over 3 years
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? -
siannopollo over 11 yearsThe symlink worked for me (after upgrading to Mountain Lion). Thanks!
-
maksimov over 11 yearsSymlink does it, especially for cases like running rails from under RubyMine where
.bash_profile
doesn't really apply. -
brendan about 11 yearsI 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'
-
Polsonby almost 11 years+1 on the symlink; First app created after migrating to Mountain Lion, Rails 3. Good answer
-
mike_hornbeck over 10 yearsfirst solution fixed it for me on OSX 10.9 with python/django
-
Justin over 10 yearsThis is the solution for RubyMine.
-
justspamjustin over 10 years+1 on the symlink with rails 4 and OSX 10.9 mavericks.
-
Nick about 10 yearsAltering the PATH and DYLD_LIBRARY_PATH as shown is all I needed on OS X Mavericks / 10.9.2.
-
racl101 over 8 yearsThis worked on Mac OS X - Yosemite for me with MySQL installed from dmg file. Thanks Joseph.
-
JonathanSimmons over 8 yearsFor those who come here for 10.11 you can't symlink to
usr/lib
anymore but a symlink tousr/local/lib
will work:sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib
-
Steve Folly over 8 yearsGlad 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?
-
gitb over 8 yearsDidn't need all of Alex's answer. One symbolic link did the trick.
-
Josh Hunter over 8 yearsI did this and got: "connect': Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) (Mysql2::Error)"
-
TinMonkey over 8 years@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.
-
Josh Hunter over 8 yearsyeah, the server wasn't running...this fixed it. sudo /usr/local/mysql/support-files/mysql.server start
-
Martin M over 8 yearsIts because of the new
system integrety
feature of El Cap, see en.wikipedia.org/wiki/… -
Artjom B. over 8 years@MSU_Bulldog Of course it answers the question. It even provides new information. Just because an answer includes the error that brought them here doesn't meant that the solution they also provide is without value.
-
Ignacio Chiazzo over 8 yearsI 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 over 8 yearsOperation not permitted (obviously with sudo) my SO version is El capitan
-
Udit Kapahi almost 8 yearsGreat ! 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
-
Colin Adams almost 8 years@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!
-
treejanitor over 7 yearsEnded up being a variation for me...
ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib
-
Tom Wilson over 7 yearsI 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.)
-
lmiller1990 about 6 yearsWorked for me too. The problem was I moved from installing mysql w/ homebrew to the official installer.
-
Vahid Kowsari almost 6 yearsThis 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
-
Peter Dolan over 5 yearsFor any python users getting here,
pip uninstall mysqlclient
andpip install mysqlclient
also worked. -
Admin over 5 yearsThanks. I did
brew link [email protected] --force
. Worked perfectly. -
Chetan Kumar over 3 years'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.
-
Ibrahim.H over 3 yearsI'm still getting an error
mysql.h is missing
although mysql is working with XAMPP correctly. -
Son Tr. over 3 yearsWorked for me too. Thanks!
-
jorgemdnt over 3 yearsPerfect, succinct. This worked for me too. Thanks for sharing
-
Victor about 3 yearsYes, it works for the
rails
part, but still not work when trying to execute queries on the database.