rails server fails to start with mysql2 using rvm & ruby 1.9.2-p0 on OSX 10.6.5
Asked Answered
M

9

38

I'm getting the following error when I start rails server:

$ rails server
/Users/ssmith/.rvm/gems/ruby-1.9.2-p0/gems/mysql2-0.2.6/lib/mysql2.rb:7:in `require':     dlopen(/Users/ssmith/.rvm/gems/ruby-1.9.2-p0/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.16.dylib (LoadError) 
Referenced from: /Users/ssmith/.rvm/gems/ruby-1.9.2-p0/gems/mysql2- 0.2.6/lib/mysql2/mysql2.bundle
Reason: image not found - /Users/ssmith/.rvm/gems/ruby-1.9.2-p0/gems/mysql2-    
0.2.6/lib/mysql2/mysql2.bundle

I've installed mysql2 with the following command after the rvm use ruby-1.9.2-p0 command:

$ gem install mysql2 -- --with-mysql-dir=/usr/local/mysql --with-mysql-config=/usr/local/mysql/bin/mysql_config
Building native extensions.  This could take a while...
Successfully installed mysql2-0.2.6
1 gem installed
Installing ri documentation for mysql2-0.2.6...
Enclosing class/module 'mMysql2' for class Client not known
Installing RDoc documentation for mysql2-0.2.6...
Enclosing class/module 'mMysql2' for class Client not known

I have mysql2 in my Gemfile as well as in the database.yml file and bundle install completes fine

$ bundle show mysql2
/Users/ssmith/.rvm/gems/ruby-1.9.2-p0/gems/mysql2-0.2.6

I understand the rails server error is due to it not knowing the mysql_config location on OSX, however on gem install I specified the correct location. Yet RVM's gem is not respecting that mysql_config location it seems.

Anyone have a solution to this?

Mathre answered 22/12, 2010 at 18:26 Comment(0)
T
64

The problem comes from the mysql2 gem missing the dynamic library from MySQL.

A cleaner solution than install_name_tool ... would need to update your DYLD_LIBRARY_PATH to add MySQL libs to it. To do so, update your ~/.bash_profile to add the MySQL library folder :

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

Note: You might want to update the MySQL location based on your install

This should keep things clean but also ensure that any gem or code requiring MySQL dynamic libraries will find them.

Reference : http://lightyearsoftware.com/2011/02/mysql-5-5-on-mac-os-x/

Reference Update July 2012: A change in OS X 10.8 makes the easy method above less elegant. If you set that variable, every time you run a setuid or setgid program, you get this warning on stderr:

dyld: DYLD_ environment variables being ignored because main executable (...) is setuid or setgid

Ruby developers using Phusion Passenger Standalone will see this message displayed in their console every five seconds. It gets really irritating, very fast.

I have filed a bug with Apple. It’s also at OpenRadar.

In the meantime, there is also a third way to fix the client library path problem that doesn’t require setting DYLD_LIBRARY_PATH (working around this 10.8 issue) or hacking .bundle files with install_name_tool:

$ brew install mysql
Tragedy answered 1/4, 2011 at 5:59 Comment(2)
This won't work if you're using pow or passenger, use Frederic's answer above to get it to work ... make sure to run it on the 'ext' folder in the gem basedir as wellTights
Also try uninstalling and then reinstalling the mysql2 gem after doing this if you still get the same error. worked for meHuey
T
22

I found the answer here: Mysql 5.5, Snow leopard and rails

sudo install_name_tool -change libmysqlclient.16.dylib /usr/local/mysql/lib/libmysqlclient.16.dylib ~/.rvm/gems/ruby-1.9.2-p0/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
Therewith answered 25/12, 2010 at 16:43 Comment(2)
The problem with this solution is that every time you (re)install the gem (one example: in a different RVM gemset), you will have to repeat the install_name_tool command. Sébastien's answer, which references my blog post, offers a cleaner solution, in my opinion.Conny
Note that the library version will also change – presently it's libmysqlclient.18.dylib (rather than 16), which needs to be subbed in in both places above.Stately
L
8

On OS X 10.8 (Mountain Lion), the listed answers all have issues, as noted in the answers and comments.

  • Setting DYLD_LIBRARY_PATH to include /usr/local/mysql/lib gives warnings from OS X and from brew
  • Using install_name_tool to hack where the gem binary looks would need to be redone each time the gem is installed or upgraded.
  • Switch to brew's mysql may not work; and in any case I don't want to reconfigure an already-working installation

A simpler and robust solution IMHO is to put a link to the library in the default dynamic library search path, which conveniently includes /usr/local/lib. That is:

ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib
Lippold answered 24/1, 2013 at 12:0 Comment(1)
fantastic, I tried all the above and this one worked immediatelyWoolcott
P
5

Bill,

Frederic's answer will solve this problem, however you may have to change some of the items in the command depending on versions and how things are named on your system.

For example, on the most recent version of mysql libmysqlclient.16.dylib is actually libmysqlclient.18.dylib. Try doing a:

locate libmysqlclient.18.dylib

If that does not return the path you can go to:

/usr/local/{your-mysql}/lib

to find the file. Then just a PWD to find the correct directory for the command.

You will also need to determine what the actual package names of the rubies you have installed are. You can find this by using

rvm info 

For example, my installation of 1.9.2 is ruby-1.9.2.p180, not ruby-1.9.2p0. This will need to be changed in Frederics command as well.

So, for me Frederic's command became for fixing the rvm rubies for 1.8.7 and 1.9.2, respectively:

sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql-5.5.10-osx10.6-x86_64/lib/libmysqlclient.18.dylib ~/.rvm/gems/ruby-1.8.7-p334/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle

sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql-5.5.10-osx10.6-x86_64/lib/libmysqlclient.18.dylib ~/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
Porringer answered 27/3, 2011 at 20:7 Comment(0)
K
3

below via: http://lightyearsoftware.com/2011/02/mysql-5-5-on-mac-os-x/

Update July 2012:

A change in OS X 10.8 makes the easy method above less elegant. If you set that variable, every time you run a setuid or setgid program, you get this warning on stderr:

dyld: DYLD_ environment variables being ignored because main executable (...) is setuid or setgid

Ruby developers using Phusion Passenger Standalone will see this message displayed in their console every five seconds. It gets really irritating, very fast.

I have filed a bug with Apple. It’s also at OpenRadar.

In the meantime, there is also a third way to fix the client library path problem that doesn’t require setting DYLD_LIBRARY_PATH (working around this 10.8 issue) or hacking .bundle files with install_name_tool:

$ brew install mysql
Knox answered 3/11, 2012 at 20:2 Comment(0)
A
2

I was stuck on this for a while and came to a different solution.

Notice how the missing library is version 16:

Library not loaded: libmysqlclient.16.dylib (LoadError)

Turns out I had version 20 of that library - libmysqlclient.20.dylib

I correctly had gem 'mysql2' in my Gemfile, but what I needed to do was uninstall the gem, gem uninstall mysql (I had multiple versions of the gem installed) and then doing a new bundle install to get just the version I needed of this library.

After that it looked for - and found - the correct version of that lib.

Allegedly answered 16/5, 2016 at 23:32 Comment(0)
I
1

Mine was in a different location, I had to use:

sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle.
Interminable answered 27/1, 2012 at 0:14 Comment(0)
S
1

Upgraded mysql to latest version and reinstall mysql2 gem works to me brew upgrade mysql gem uninstall mysql2 gem install mysql2

BTW: My MySQL version is 5.7.18, mysql2 gem version is 0.4.5

Savanna answered 28/7, 2017 at 3:0 Comment(0)
A
0

Here is what I do (similar to others)

sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib ~/.rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.3.10/lib/mysql2/mysql2.bundle
Abbreviation answered 12/12, 2011 at 4:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.