Installing the MySQL gem on OS X
Wanna install the mysql gem for Ruby? You’d better do it since it’s already available, and you will need it anyway from Rails 2.2 since all database adapters are going to be extracted to 3rd party gems. Take a quick look at your development.log file:
cat development.log | grep -i "DEPRECATION WARNING"
You may see a lot of deprecation warnings related to the mysql database connector like these:
DEPRECATION WARNING: You’re using the Ruby-based MySQL library that ships with Rails. This library will be REMOVED FROM RAILS 2.2. Please switch to the offical mysql gem: `gem install mysql` See http://www.rubyonrails.org/deprecation for details. (called from mysql_connection at /Library/Ruby/Gems/1.8/gems/activerecord-2.1.1/lib/active_record/connection_adapters/mysql_adapter.rb:81)
Now you’d better install the mysql gem. I know, it is not a too easy task, a lot of people seem to have problems with it. If you type sudo gem install mysql your Terminal may return with errors:
Building native extensions. This could take a while... ERROR: Error installing mysql: ERROR: Failed to build gem native extension. /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb install mysql checking for mysql_query() in -lmysqlclient... no checking for main() in -lm... yes checking for mysql_query() in -lmysqlclient... no checking for main() in -lz... yes checking for mysql_query() in -lmysqlclient... no checking for main() in -lsocket... no checking for mysql_query() in -lmysqlclient... no checking for main() in -lnsl... no checking for mysql_query() in -lmysqlclient... no *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options.
Yea, very disappointing I know, I know. But rather then start crying like a teenage girl you could check the error log and start thinking about what you are missing.
No, no, no, I am not talking about a million dollar penthouse or girlfriend in this case… You forgot to tell RubyGems where to look for the mysql header/source files. Without them it just cannot build a native extension on your machine.
Let’s provide some more information to the package manager so it will be able to build that beast on your machine:
sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config Building native extensions. This could take a while... Successfully installed mysql-2.7 1 gem installed
Much better.
October 21st, 2008 at 11:05 pm
Nice article. Thanks.
Eugene
November 30th, 2008 at 11:10 pm
Thanks thanks thanks!
December 3rd, 2008 at 8:16 pm
Thank you so much! I have been working through this issue for 8 days, putting in hours upon hours each attempt. I eventually decided to develop on my hosted server through MediaTemple because I was having such difficulty; I’ve tried dozens of ‘gem install’ lines from a dozen or more blogs, but when I tried what you posted above, it worked!!! It worked!!!
I know this may sound weird, a bit strange, but I love you.
December 5th, 2008 at 5:31 am
Thank you. Thank you! THANK YOU!
December 8th, 2008 at 3:26 am
Thank you so much!
I am not worthy…..
January 15th, 2009 at 12:48 am
Thanks
January 15th, 2009 at 12:51 am
[...] Thanks for this goes to: How to install the latest MySQL gem ons OSX [...]
January 15th, 2009 at 6:19 am
Thanks a ton! Google was definitely my friend this time.
February 16th, 2009 at 10:06 pm
I must have been through about 20+ sites purporting to offer the solution (not to mention losing two days to this gem). Struck gold here! Ta.
March 10th, 2009 at 6:55 am
Thanks!
April 18th, 2009 at 4:25 am
[...] Taken straight from this blog [...]
April 29th, 2009 at 3:29 am
Installing the gem works ok, but USING it fails:
shull$ rake db:drop
(in /Users/shull/RubymineProjects/iPusher)
dyld: lazy symbol binding failed: Symbol not found: _mysql_init
Referenced from: /Library/Ruby/Gems/1.8/gems/mysql-2.7/lib/mysql.bundle
Expected in: dynamic lookup
dyld: Symbol not found: _mysql_init
Referenced from: /Library/Ruby/Gems/1.8/gems/mysql-2.7/lib/mysql.bundle
Expected in: dynamic lookup
Trace/BPT trap
May 20th, 2009 at 7:39 pm
Thanks for the help! Your post did the trick!
June 11th, 2009 at 10:25 am
sudo gem install mysql — –with-mysql-include=/opt/local/include/mysql5 –with-mysql-lib=/opt/local/lib/mysql5 –with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config
Update: When doing this on Leopard/MacOS X 10.5, with MySQL which had been installed through MacPorts, you need to change it to:
sudo env ARCHFLAGS=”-arch i386″ gem install mysql — –with-mysql-include=/opt/local/include/mysql5 –with-mysql-lib=/opt/local/lib/mysql5 –with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config
July 25th, 2009 at 12:48 am
Thanks guy. I was just going to sit and cry like a teenage girl but now I don’t get to.
November 14th, 2009 at 2:17 pm
Thanks for this – it’s taken me another step closer to getting Datamapper/MySQL to work on OSX
November 27th, 2009 at 5:27 pm
Thank You ! Your article helped
January 1st, 2010 at 4:02 pm
[...] gem requires some path trickery, since I didn’t install the macports version. I followed these instructions, which worked [...]