MySQL gem on OSX

Posted by Brian in Howto, Rails, snacks (June 1st, 2007)

UPDATED 12-05-2008: The instructions in this article were written for Tiger. With the release of Rails 2.21 this article’s been getting a lot more traffic and the instructions here needed to be updated. What you see here are instructions that should work for most installations of MySQL on the Mac.

When I got the Macbook Pro in March 2007, I immediately installed Ruby, Rails, Subversion, etc, using this tutorial from Hivelogic. http://hivelogic.com/narrative/articles/ruby-rails-mongrel-mysql-osx
They’ve done a good job of keeping it up to date, but one problem I noticed is that the MySQL gem instructions don’t work anymore. When the article was first written, it contained steps to remedy a minor installation problem. However, enough users reported that the problem no longer existed so it was removed.

Today, I encountered that problem again, and thought I’d share the solution. If you’re getting an error when you do

First, locate your installation of MySQL using

which mysql_config

You need three libraries:
include/mysql5
lib/mysql5
bin/mysql5_config

These should all be located within your mysql base folder.

For example, if MySQL is installed by Macports, the default location would be

/opt/local/lib/mysql5/

So you’d change the paths accordingly:

 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

This solution seems to get things rolling. Thanks much to crookshanks on IRC for the fix. Really, if you’re not participating in the IRC channels for Rails (#rubyonrails on irc.freenode.net) you’re missing out on some good discussions.

The rest of this article is most likely not relevant anymore.,/b>

You’ll need to do the following to fix it.

First, look at where the gem says it’s left its files.

  Gem files will remain installed in /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7 for inspection.

So in my case, I’ll do

  cd /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7

Then I’ll edit the file ‘mysql.c’ in that folder and add one line at the top of the file:

  #define ulong unsigned long

Then, I just run

  sudo make
  sudo make install

The original fix for this comes from
http://jlaine.net/2006/10/3/installing-ruby-mysql-driver-on-os-x but the instructions there are a little backwards. I hope this solution will help others having a similar problem.

5 Responses to ' MySQL gem on OSX '

Subscribe to comments with RSS or TrackBack to ' MySQL gem on OSX '.

  1. ouvyt said,
    on January 18th, 2009 at 8:17 am

    make sure you have the latest xcode installed (free download from Apple) to get the gcc compilers.

  2. on January 18th, 2009 at 8:00 pm

    […] New Auburn Personal Computer Services LLC » Blog Archive » MySQL gem on OSX fixing the !!! The bundled mysql.rb driver has been removed from Rails 2.2. Please install the mysql gem and try again: gem install mysql. […]

  3. on February 12th, 2009 at 9:04 am

    […] Lots of people seem to have troubles getting the MySQL gem installed on OS X.Here is what i use: sudo gem install mysql — –with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config On my machine MySQL5 was installed using port then the path to the config should be correct. Otherwise either you know where the config file is (because you’ve installed MySQL yourself and you know what you are doing – but then – i assume – you would not need to read this :-) ) or you have to search for it. Try which mysql_config and if this does not work find / -name mysql_config (and go get a cup of  coffee).  […]

  4. Gwyn Morfey said,
    on June 16th, 2009 at 5:06 am

    Worked for me, thanks.

  5. Maged Makled said,
    on June 15th, 2010 at 2:32 pm

    I tried your solution but it didn’t work. which mysql_config gives “/usr/local/mysql/bin/mysql_config”
    when I changed the your command to “sudo gem install mysql — –with-mysql-include=/usr/local/mysql/include –with-mysql-lib=/usr/local/mysql/lib –with-mysql config=/usr/local/mysql/bin/mysql_config”

    still got the error ERROR: Failed to build gem native extension.

    any suggestions ?

    Thanks

    Maged Makled

Leave a reply

:mrgreen: :neutral: :twisted: :shock: :smile: :???: :cool: :evil: :grin: :oops: :razz: :roll: :wink: :cry: :eek: :lol: :mad: :sad: