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
You're a gem
I’ve been looking everywhere for this. Thanks a bunch.
Strange problem
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’
Re: Strange problem
Try requiring RubyGems first (this is necessary since the mysql lib is a gem):
Thanks
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
Me too!
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.
geting mysql.c:2015: error: 'ulong' undeclared
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$
worked for me.
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.
Thanks!
Thanks for posting this!
thanks for your help
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
Thanks
Thank you very much for your help. Worked as expected!
Thanks
I really appreciate this as I’m tired of these errors!
worked perfectly
Top man : worked first go !
what am I doing wrong here
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
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$
You are a rocking google god.
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!!!
the pure Ruby MySQL interface might be easier
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.
Super
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
Any ideas how to install the new MySQL-gem without compiling on Mac OS X?
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
Thank you!
Thank you! After many hours of searching this fixed my problem.
Thanks a lot!
Thanks a lot for pointing this out. Now I can use Rails 2.2.2. ;-)
Life saver!
Yeah after 6hours of trying different things and googling, I eventually came across your article. Sorted out everthing for me, thanks very much! :)
Thanks
Thanks a lot for this… Took me quite a lot of searching to reach here
mysql_config also works
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
Great!
Thanks a lot—I googled and tried a lot after upgrading to Rails 2.2, this finally worked :-)
This worked with x86_64 version
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/
Sorry, it didn't work
It’s more complicated. Still working on the solution.
Thanks Michel.
Adding —with-mysql-config=/usr/local/mysql/bin/mysql_config and it finally installed.
Worked with MySQL 5.0.67
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.
To figure out which MySQL you have...
I wasn’t sure what I installed, but my symlink from /usr/local/mysql explained it all:
Thanks!
- Andy
thanks!
Awesome, worked perfectly and saved me a bunch of time. thx!
Thanks a lot!
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!
Running MacPorts
Running Apache2 from MacPorts and MySQL5.0.67 (x86), I had to run the following to get the install going:
Awesome thanks!
I’ve been killing myself looking for this. Worked like a charm!
I second the vote for pure Ruby lib
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:
But they should probably read:
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
Finally - mysql is in
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.
thanks man!
worked on the first go!
MacPorts MySQL 5
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/loc al —with-mysql-lib=/opt/loc al/lib/mysql5/mysql —with-mysql-include=/opt /local/include/mysql5/mysql
Happy February 20th
Much obliged to you for sharing the delightfully concise solution!
Ruby on Rails
For those using Ruby on Rails, I just saw that there is a comment regarding this in database.sample.yml (in the config directory).
Thanks!
Thanks a lot, copy/paste and it worked instantly.
A modified version that worked for me (Mac OS X 10.5.6, mysql5 v14.12, MacPorts)
I had different folders for MySQL:
mysql root:
/opt/local/lib/mysql5lib:
/opt/local/lib/mysql5/mysqlinclude:
/opt/local/include/mysql5/mysq lSo I had to change the statement accordingly:
Thanks
That solved it for me too! Thanks so much!
Thanks!
Worked great on a fresh install of leopard.
Thanks
Worked perfect on fresh install of Leopard
Thanks
dude you are a god.
Worked PErfect!!!
YOU ARE THE MAN!!!!!!
Thanks
This:
sudo gem install mysql — —with-mysql-config=/usr/ local/mysql/bin/mysql_config
works to me well, too!
Thank you
That saved me some time!
This works if you have MAMP installed
sudo env ARCHFLAGS=“-arch i386” gem install mysql — —with-mysql-config=/Appl ications/MAMP/Library/bin/mysq l_config
Was about to kick the mac to the curb!
THANK YOU! this works wonderfully!
Genius
Genius! Thank you very much.
one line gem mysql
Fixed my problems.
Gem install for Ruby Enterprise Edition
for Ruby EE I did 90421/bin/gem install mysql — —with-mysql-config=/opt/ local/bin/mysql_config5
sudo /opt/ruby-enterprise-1.8.6-200
(make sure you substitute the version number for the correct one on your system, ie this bit → ruby-enterprise-1.8.6-20090421 )
Very Useful
Thanks dude, mate or whatever. :B
Awesome
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!!!!
Thanks a lot!
Thanks, that’s exactly what I was looking for.
Excellent - thanks!
Worked perfectly.
Thank you!
You are the man.
Thank you
Thanks!