Hello, I’m a web craftsman with a passion for the modern web. I build web applications and play with social services and communities.

December 11, 2009 at 2:04 am

I spend the last couple of hours trying to get Ruby on Rails (2.3.5) to work with MySQL on Mac OS X 10.6 Snow Leopard. So far I have been using SQLite for Rails Development but a current project requires some MySQL specific functions. I didn’t expect things to be so rough but now everything works and I am quite happy. So happy that I decided to write it down and share it with the world.

First: What was the problem?

Since I’m coming from PHP/MySQL development I had XAMPP already installed on my system and I was hoping to use the same XAMPP-bundled MySQL Server I have been using for all my PHP projects for rails projects too. Forget it! Don’t waste your time here. You may get it to work somehow but you will run into trouble at some point since XAMPP’s MySQL is compiled in 32bit and Ruby on Snow Leopard in 64bit. So basically as soon as you have a Mac with a Core 2 Duo (or any other 64bit processor) and running 10.6 Snow Leopard you have to install a 64bit version of MySQL.

So the problem you will run into (or already ran in and that’s why you are here right now) while trying to install the mysql gem with sudo install mysql looks like this:

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
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
checking for main() in -lmygcc… 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.

and some more I won’t bore you with.

So here comes the solution:

1. Download MySQL 5.4 beta for Mac OS X 10.5 as x86_64 version from the official MySQL website

2. Open up the downloaded .dmg and run the mysql-5.4.x-…x86_64.pkg installer, followed by the MySQLStartupItem.pkg Installer. After that you install the MySQL.prefPane which allows you to start and stop the MySQL Server comfortably from the System Preferences (for some reason the prefPane is still in 32bit but that won’t bother us now).

3. Go into terminal and install the mysqlplus gem:

sudo gem install mysqlplus

4. That’s it!

It seems a bit crazy that I spend hours figuring that out but basically the key to success is mysqlplus here. I tried all kinds of combinations with the official MySQL 5.1/5.4, XAMPP’s MySQL, MacPorts MySQL and the standart mysql gem but none worked. Sometimes I got the mysql gem to compile and install but then rails still couldn’t connect to the database… However, now it works and I’m glad things can go on!

15 responses to “Ruby on Rails + MySQL on Mac OS X 10.6 Snow Leopard”

  1. Lim says:

    first!
    thanks it really did work!

    i wasted a lot of time doing other things

    thanks

  2. Matthias says:

    Glad it helped already someone! I spend easily 8 hours to get that damn thing to work. Also I found a second way to make things work with the official mysql gem last night. I may update the post at some point.

  3. Dan says:

    I just did as you suggested, but ran into a problem installing mysqlplus.

    ERROR: Error installing mysqlplus:
    ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb
    mkmf.rb can’t find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ruby.h

    Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/mysqlplus-0.1.1 for inspection.
    Results logged to /Library/Ruby/Gems/1.8/gems/mysqlplus-0.1.1/ext/gem_make.out

    Anyone have any ideas? Thanks for the otherwise helpful post.

  4. Matthias says:

    This usually appears when something is wrong with your Ruby installation!

    Try reinstalling the latest XCode from the Apple Website. (XCode 3.2.1 as of now)

  5. T says:

    I’m trying to run the command script/server but I end up with the following error:
    Missing the Rails 2.3.5 gem. Please `gem install -v=2.3.5 rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.

    Gem -v 1.3.1, ruby -v 1.9.1, rails -v 2.3.5

    Everything install on an administrative account no errors installing

  6. Matthias says:

    Have you also updated your RAILS_GEM_VERSION setting in config/environment.rb as it asks you to do?
    Also you may have an older version of rails frozen in your project folder.

    If nothing helps just try to create a new rails project with the same gems and see if it works there. Then you know whether the error comes from you local ruby on rails installation or whether it is project related.

  7. T says:

    @Matthias

    yes RAILS_GEM_VERSION setting in config/environment.rb was updated and even commented out for testing.
    I’m unable to find any trace of any rails other than 2.3.5
    I’ve also tried creating a new project but I get the same result. Since then I’ve uninstalled and reinstalled ruby, rails, gems, using macport but happened to give me the same result. Any ideas?

  8. T says:

    Solved!!!
    Had to remove all instances of ruby, rubygem, and rails manually
    had to use
    sudo port uninstall
    sudo port clean
    delete all ruby listings in usr/bin usr/local/bin opt/local/bin
    now reinstall ruby19, rubygem, and rails using macports
    sudo port install ruby19
    sudo port install rubygem
    sudo port install rails

    almost done know you have to update Rubygem
    sudo gem update –system

    and we’re done

    this finally solved my issue

  9. T says:

    One thing more do it this way you’ll need to install the appropriate database you’ll be using. My applications work just fine on the “sqlite3” database that work so well with rails so I had to:
    sudo gem install sqlite3

    Whatever your database of choice don’t forget to install it

  10. Jim says:

    Thank you indeed! I cant tell you how much time I have spent trying to get Ruby (in general) up and running in any version of OS X

    Jim

  11. Matthias says:

    I’m glad it helped! Getting ruby itself to run on OS X shouldn’t be much of an issue. At least on 10.6 Snow Leopard all you have to do is installing Xcode which comes on the OS X install disk.

  12. T says:

    Getting Xcode installed will get you up and running with Ruby 1.8.7 most of the time but we all know things don’t always work out so nicely as I’ve learned after many Ruby setups on Snow Leopard. Keep one thing in mind for those who are looking to try Ruby 1.9.1 as I am MacPorts are the way to go. I’ve tried every method I’m read online including the Ruby site and by far I’ve had the most success using MacPorts even though every setup has not been prefect. Regarding databases well… I do testing and development and for speed and ease of setup I use SQLite which is nicely intertwined with Rails but for real world use you may want to consider at least MySQL. I have to say I’ve been enjoying my RoR experience so far.

  13. Rahil Sondhi says:

    Hi,

    I did everything you recommended but I keep getting:

    $ rake db:migrate
    (in /Users/rahil/Sites/test)
    rake aborted!
    uninitialized constant MysqlCompat::MysqlRes

  14. Matthias says:

    Hi Rahil,
    I just blogged about that issue a few days back:
    http://blog.m-schmidt.eu/2010/08/05/uninitialized-constant-mysqlcompatmysqlres/

    Also by now I’d recommend installing MySQL thought homebrew (a neat OS X package manager) instead of using the binary version from the MySQL website!

  15. beaknit says:

    Actually steps to install T’s solution on ox snow leopard:

    $ sudo su
    # port install rb-rubygems
    # port install rb-rails
    # gem update –-system

Leave a Reply