The eclectic musings of a bitter software engineer.

Here’s how to get the latest version of MySQL (5.0.51b as of this writing) working with the MySQL/Ruby gem and the Apple-supported version of Ruby that comes with Mac OS X Leopard. These instructions are scattered around the Internets in various places, but I’m sick of searching for them every time I need them so I’m compiling them here in a single place for convenience.

First, download and install MySQL for Mac OS X 10.5 (x86). Don’t install the x86_64 build or Ruby will refuse to speak to it. If you’ve already installed the x86_64 build, backup your databases, install the x86 build on top of it, and restore your databases.

Once you’ve got the correct build of MySQL installed, pop open a terminal and run the following to install the MySQL/Ruby gem:

sudo env ARCHFLAGS="-arch i386" gem install mysql -- \
  --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib \
  --with-mysql-include=/usr/local/mysql/include

That should do the trick.

Comments

I’ve been looking everywhere for this. Thanks a bunch.

Gravatar icon
DM
Wednesday August 06, 2008 @ 09:39 AM (PDT)

Hi,

I found your page through google and installed the ruby mysql gem on my mac with your line above. Thanks. After installing, I played around with it in irb on a mysql database that I have and it worked find. However when I tried to run a ruby script with it, the “require ‘mysql’” line fails and it doesn’t work:

./msql.rb:4:in `require’: no such file to load — mysql (LoadError)
from ./msql.rb:4

So, when I go back to irb and try to see what happened, irb works fine:

%irb
>> require ‘mysql’
=> true
>>

I’ve tried for a while to track this problem down (maybe it’s a path problem somewhere?)
But, haven’t found anything yet. Google doesn’t seem to be very helpful for this either.
Do you have any pointers? I’m assuming everything works for you. BTW, I’m running
10.5.4 on a 32-bit macbook.

Thanks for any help you can give.

-Jason

The test script from error above was:
#!/usr/bin/ruby

#puts `env`
require ‘mysql’

Gravatar icon
Jason
Tuesday September 02, 2008 @ 03:12 PM (PDT)

Try requiring RubyGems first (this is necessary since the mysql lib is a gem):

#!/usr/bin/ruby

require 'rubygems'
require 'mysql'
Gravatar icon
Tuesday September 02, 2008 @ 03:51 PM (PDT)

I was just looking back at your page since I got the script to work doing:
require ‘/usr/lib/ruby/user-gems/1.8/gems/mysql-2.7/mysql’

But, your require ‘rubygems’ is much better. I haven’t used gems that much… as
you can probably tell.

Thanks,
-Jason

Gravatar icon
Jason
Tuesday September 02, 2008 @ 03:58 PM (PDT)

Jason,

Same problem for me. If I don’t specify the absolute path I got the following:

NameError: uninitialized constant Mysql

Changing it to: require ‘/usr/lib/ruby/user-gems/1.8/gems/mysql-2.7/mysql’

Made everything work.

Gravatar icon
Friday September 12, 2008 @ 04:48 AM (PDT)

I’m getting the following error when trying to install – any ideas?

Macintosh-2:myequity greg$ sudo env ARCHFLAGS=“-arch i386” gem install mysql — —with-mysql-dir=/usr/local/mysql —with-mysql-lib=/usr/local/mysql/lib —with-mysql-include=/usr/local/mysql/include -V

WARNING: RubyGems 1.2+ index not found for:

RubyGems will revert to legacy indexes degrading performance.
Updating metadata for 69 gems from http://gems.rubyforge.org/
……………………………………………………………
complete
Building native extensions. This could take a while…
ERROR: Error installing mysql:
ERROR: Failed to build gem native extension.

/opt/local/bin/ruby extconf.rb install mysql — —with-mysql-dir=/usr/local/mysql —with-mysql-lib=/usr/local/mysql/lib —with-mysql-include=/usr/local/mysql/include -V
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… yes
checking for mysql_ssl_set()… yes
checking for mysql.h… yes
creating Makefile

make
/usr/bin/gcc-4.0 -I. -I. -I/opt/local/lib/ruby/1.8/i686-darwin9.3.0 -I. -DHAVE_MYSQL_SSL_SET -DHAVE_MYSQL_H -I/usr/local/mysql/include -I/usr/local/mysql/include -I/opt/local/include -fno-common -O2 -fno-common -pipe -fno-common -c mysql.c
mysql.c: In function ‘Init_mysql’:
mysql.c:2015: error: ‘ulong’ undeclared (first use in this function)
mysql.c:2015: error: (Each undeclared identifier is reported only once
mysql.c:2015: error: for each function it appears in.)
mysql.c:2015: error: syntax error before numeric constant
mysql.c:2018: error: syntax error before numeric constant
make: * [mysql.o] Error 1

Gem files will remain installed in /opt/local/lib/ruby/gems/1.8/gems/mysql-2.7 for inspection.
Results logged to /opt/local/lib/ruby/gems/1.8/gems/mysql-2.7/gem_make.out
Macintosh-2:myequity greg$

Gravatar icon
Greg H
Sunday September 14, 2008 @ 01:18 PM (PDT)

Thanks. I’d been ignoring the problem and using the default Ruby-based MySQL drivers until I saw they were going to be removed from Rails 2.2, this worked.

Gravatar icon
Monday September 22, 2008 @ 09:33 PM (PDT)

Thanks for posting this!

Gravatar icon
Saturday October 11, 2008 @ 11:47 AM (PDT)
I tried

sudo env ARCHFLAGS=“-arch i386” gem install mysql — \
—with-mysql-dir=/usr/local/mysql —with-mysql-lib=/usr/local/mysql/lib \
—with-mysql-include=/usr/local/mysql/include
without success, but everything was fine after adding
—with-mysql-config=/usr/local/mysql/bin/mysql_config

hoping that it could help someone

Gravatar icon
Michel
Friday October 17, 2008 @ 04:42 AM (PDT)

Thank you very much for your help. Worked as expected!

Gravatar icon
Friday October 24, 2008 @ 04:38 AM (PDT)

I really appreciate this as I’m tired of these errors!

Gravatar icon
Monday October 27, 2008 @ 03:12 PM (PDT)

Top man : worked first go !

Gravatar icon
Chris
Thursday October 30, 2008 @ 04:14 AM (PDT)

sudo env ARCHFLAGS=“-arch i386” gem install mysql — \
> —with-mysql-dir=/usr/local/mysql —with-mysql-lib=/usr/local/mysql/lib \
> —with-mysql-include=/usr/local/mysql/include
Password:
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 — —with-mysql-dir=/usr/local/mysql —with-mysql-lib=/usr/local/mysql/lib —with-mysql-include=/usr/local/mysql/include
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.

Provided configuration options:
—with-opt-dir
—without-opt-dir
—with-opt-include
—without-opt-include=${opt-dir}/include
—with-opt-lib
—without-opt-lib=${opt-dir}/lib
—with-make-prog
—without-make-prog
—srcdir=.
—curdir
—ruby=/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
—with-mysql-config
—without-mysql-config
—with-mysql-dir
—with-mysql-include=${mysql-dir}/include
—with-mysql-lib=${mysql-dir}/lib
—with-mysqlclientlib
—without-mysqlclientlib
—with-mlib
—without-mlib
—with-mysqlclientlib
—without-mysqlclientlib
—with-zlib
—without-zlib
—with-mysqlclientlib
—without-mysqlclientlib
—with-socketlib
—without-socketlib
—with-mysqlclientlib
—without-mysqlclientlib
—with-nsllib
—without-nsllib
—with-mysqlclientlib
—without-mysqlclientlib

Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/mysql-2.7 for inspection.
Results logged to /Library/Ruby/Gems/1.8/gems/mysql-2.7/gem_make.out
Chinook1:~ marccantwell$

Gravatar icon
Marc
Sunday November 09, 2008 @ 07:09 PM (PST)

I downloaded the mysql version of mysql(duh…) like 5 months ago.

i was scared your tutorial wouldn’t work. BUT IT DOES. you are the man!!!

Gravatar icon
Sunday November 23, 2008 @ 06:07 AM (PST)

You might want to consider installing the pure Ruby MySQL interface from here:

http://www.tmtm.org/en/ruby/mysql/

It’s (at least to me) easier than re-installing mysql.

Simply install the latest version of rails. Download and install the Ruby MySQL interface and your good to go.

Gravatar icon
Monday November 24, 2008 @ 06:27 PM (PST)

thanks man. this is the only working solution that i could find among many purported ones which did not work. it must have been the 86_64 issue

Gravatar icon
ernest
Tuesday November 25, 2008 @ 11:38 PM (PST)

Working on a tiny machine and requiring I18N Rails 2.2 would be a big plus. I am currently on 2.1.2, using the ruby MySQL adpater. Any ideas how to install the shiny new MySQL adapter without first installing 3+GB of Xcode and a few MBs of MySql to compile a tiny adapter? Anything to copy over from a Mac that has that stuff installed?

Thanks,

Tom

Gravatar icon
Wednesday November 26, 2008 @ 07:14 AM (PST)

Thank you! After many hours of searching this fixed my problem.

Gravatar icon
Bora
Saturday November 29, 2008 @ 01:08 PM (PST)

Thanks a lot for pointing this out. Now I can use Rails 2.2.2. ;-)

Gravatar icon
Hugo
Tuesday December 02, 2008 @ 07:22 AM (PST)

Yeah after 6hours of trying different things and googling, I eventually came across your article. Sorted out everthing for me, thanks very much! :)

Gravatar icon
Tuesday December 09, 2008 @ 04:29 PM (PST)

Thanks a lot for this… Took me quite a lot of searching to reach here

Gravatar icon
Shiv Indap
Wednesday December 10, 2008 @ 05:41 PM (PST)

This did the job pretty well for me, of cpurse i had to install xcode tools 3 before that

#gem install mysql — —with-mysql-config=/usr/local/mysql/bin/mysql_config

Gravatar icon
Friday December 12, 2008 @ 11:21 PM (PST)

Thanks a lot—I googled and tried a lot after upgrading to Rails 2.2, this finally worked :-)

Gravatar icon
Saturday December 20, 2008 @ 05:34 PM (PST)

I did the following command, which worked:

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

Adapted from http://www.napcsweb.com/blog/2007/06/01/mysql-gem-on-osx/

Gravatar icon
Tuesday December 30, 2008 @ 04:48 AM (PST)

It’s more complicated. Still working on the solution.

Gravatar icon
Tuesday December 30, 2008 @ 04:57 AM (PST)

Adding —with-mysql-config=/usr/local/mysql/bin/mysql_config and it finally installed.

Gravatar icon
Monique Caron
Friday January 02, 2009 @ 03:06 PM (PST)

I just ran the command with my default MySQL version 5.0.67, and it “Successfully installed mysql-2.7”. I’m using Leopard 10.5.6. Thanks.

Gravatar icon
Friday January 02, 2009 @ 08:24 PM (PST)

I wasn’t sure what I installed, but my symlink from /usr/local/mysql explained it all:

> ls -l /usr/local/mysql
lrwxr-xr-x  1 root  wheel  24 Aug 13 14:20 /usr/local/mysql@ -> mysql-5.0.67-osx10.5-x86

Thanks!
- Andy

Gravatar icon
Andy
Tuesday January 06, 2009 @ 12:01 PM (PST)

Awesome, worked perfectly and saved me a bunch of time. thx!

Gravatar icon
Friday January 09, 2009 @ 12:57 PM (PST)

I came across this while trying to upgrade to rails 2.2 and this worked on the first try. Thanks for saving me a lot of time!

Gravatar icon
Sunday January 11, 2009 @ 08:35 PM (PST)

Running Apache2 from MacPorts and MySQL5.0.67 (x86), I had to run the following to get the install going:

sudo gem install mysql

# The standard install fails building the native extension, even with archflags set, so I build it mysql
cd /Library/Ruby/Gems/1.8/gems/mysql-2.7/

sudo env ARCHFLAGS="-arch i386" ruby extconf.rb --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib --with-mysql-include=/usr/local/mysql/include

Gravatar icon
Tuesday January 13, 2009 @ 09:31 AM (PST)

I’ve been killing myself looking for this. Worked like a charm!

Gravatar icon
Jared Howard
Monday January 19, 2009 @ 07:44 PM (PST)

As mentioned by Kristoph (thank you Kristoph!!!), the pure ruby interface can be found here:

http://www.tmtm.org/en/ruby/mysql/

(This is actually from the author of the ruby mysql gem, so you know you’re getting the right thing here).
This was SUPER easy to install. One thing to note though, the install instructions show:


$ ruby ./setup.rb
$ ruby ./test.rb hostname user passwd
$ ruby ./install.rb

But they should probably read:

$ ruby ./setup.rb
$ ruby ./test.rb hostname user passwd
$ sudo ruby ./install.rb

Otherwise the install script won’t have permission to write in the directory it’s trying to write to. Or at least, it didn’t on my machine…

I’m using XAMPP, and I suspect that this is the only way to use the XAMPP mysql with RoR.

Cheers!

-Steve

Gravatar icon
Steve
Tuesday January 27, 2009 @ 10:08 AM (PST)

Thanks for the post.
To make this work though, I had to kill default ruby installation and reinstall it from source,
if interested I used this link http://blog.bogojoker.com/2008/06/installing-ruby-187-on-mac-os-x-1053 in case if you will have problems with readline.

Gravatar icon
Monday February 02, 2009 @ 09:13 PM (PST)

worked on the first go!

Gravatar icon
Derek
Sunday February 15, 2009 @ 04:14 PM (PST)

In order for my install to work when installing MySQL 5 from MacPorts was to use the following line:

sudo env ARCHFLAGS=“-arch i386” gem install mysql — —with-mysql-dir=/opt/local —with-mysql-lib=/opt/local/lib/mysql5/mysql —with-mysql-include=/opt/local/include/mysql5/mysql

Gravatar icon
Matt
Tuesday February 17, 2009 @ 09:59 AM (PST)

Much obliged to you for sharing the delightfully concise solution!

Gravatar icon
Stephen Huey
Friday February 20, 2009 @ 09:15 AM (PST)

For those using Ruby on Rails, I just saw that there is a comment regarding this in database.sample.yml (in the config directory).

Gravatar icon
Stephen Huey
Friday February 20, 2009 @ 09:29 AM (PST)

Thanks a lot, copy/paste and it worked instantly.

Gravatar icon
Thursday February 26, 2009 @ 05:10 AM (PST)

I had different folders for MySQL:
mysql root: /opt/local/lib/mysql5
lib: /opt/local/lib/mysql5/mysql
include: /opt/local/include/mysql5/mysql

So I had to change the statement accordingly:

sudo env ARCHFLAGS=“-arch i386” gem install mysql — \
—with-mysql-dir=/opt/local/lib/mysql5 \
—with-mysql-lib=/opt/local/lib/mysql5/mysql \
—with-mysql-include=/opt/local/include/mysql5/mysql

Gravatar icon
Thursday March 05, 2009 @ 03:05 AM (PST)

That solved it for me too! Thanks so much!

Gravatar icon
Senz
Thursday March 05, 2009 @ 04:11 PM (PST)

Worked great on a fresh install of leopard.

Gravatar icon
Bryan Mahoney
Friday March 06, 2009 @ 09:09 AM (PST)

Worked perfect on fresh install of Leopard

Gravatar icon
Wednesday March 11, 2009 @ 08:55 PM (PDT)

dude you are a god.

Gravatar icon
Sunday March 29, 2009 @ 10:22 AM (PDT)

YOU ARE THE MAN!!!!!!

Gravatar icon
Saturday April 04, 2009 @ 06:24 AM (PDT)

This:

sudo gem install mysql — —with-mysql-config=/usr/local/mysql/bin/mysql_config

works to me well, too!

Gravatar icon
Fernando Gomes
Monday April 20, 2009 @ 11:00 AM (PDT)

That saved me some time!

Gravatar icon
Shashi Prabhakar
Sunday April 26, 2009 @ 03:00 PM (PDT)

sudo env ARCHFLAGS=“-arch i386” gem install mysql — —with-mysql-config=/Applications/MAMP/Library/bin/mysql_config

Gravatar icon
hacker_112
Sunday May 03, 2009 @ 08:11 AM (PDT)

THANK YOU! this works wonderfully!

Gravatar icon
Nearly MacHater
Wednesday May 06, 2009 @ 03:01 PM (PDT)

Genius! Thank you very much.

Gravatar icon
ricsrock
Thursday May 07, 2009 @ 12:54 PM (PDT)

Fixed my problems.

Gravatar icon
Steve
Saturday May 09, 2009 @ 12:31 AM (PDT)

for Ruby EE I did
sudo /opt/ruby-enterprise-1.8.6-20090421/bin/gem install mysql — —with-mysql-config=/opt/local/bin/mysql_config5

(make sure you substitute the version number for the correct one on your system, ie this bit → ruby-enterprise-1.8.6-20090421)

Gravatar icon
Wednesday May 13, 2009 @ 08:15 AM (PDT)

Thanks dude, mate or whatever. :B

Gravatar icon
Wednesday May 20, 2009 @ 06:04 AM (PDT)

Mate many thanx for that very useful post, I was trying every bloody combination till I found that page!
You Rock bro KEEP ER LIT!!!!

Gravatar icon
Andrew Shatnyy
Friday June 12, 2009 @ 01:40 AM (PDT)

Thanks, that’s exactly what I was looking for.

Gravatar icon
Serguei
Thursday June 18, 2009 @ 01:42 PM (PDT)

Worked perfectly.

Gravatar icon
Anon
Sunday June 21, 2009 @ 11:54 PM (PDT)

You are the man.

Gravatar icon
Mikhail
Tuesday June 23, 2009 @ 10:20 AM (PDT)

Thanks!

Gravatar icon
Wednesday June 24, 2009 @ 02:40 PM (PDT)
New comment

required, won't be displayed

optional

Don't type anything here unless you're an evil robot:


And especially don't type anything here:

Basic XHTML (including links) is allowed, just don't try anything fishy. Your comment will be auto-formatted unless you use your own <p> tags for formatting. You're also welcome to use Textile.

Copyright © 2002-2009 Ryan Grove. All rights reserved.
Powered by Thoth.