Getting the mysql gem installed on Ubuntu

by Rich Brant on February 16, 2007

If you’re like me, you’ve gotten the ‘MySQL lost connection’ errors when running rails apps on Ubunutu. And if you’ve tried to apply the common fix – installing the mysql gem – you might not have been able to and have seen an error such as:

ERROR: While executing gem … (Gem::Installer::ExtensionBuildError)
ERROR: Failed to build gem native extension.

The problem is that you need the MySQL development headers in order to get the ruby gem to compile…sooo, you need to install this first (gem install mysql-dev won’t work):

apt-get install libmysqlclient12-dev

Then run:

gem install mysql

Select the right gem and you should be all set.

UPDATE:

if you see the error:

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… yes
checking for mysql_query() in -lmysqlclient… no
*** extconf.rb failed ***

try:

sudo gem install mysql — –with-mysql-config

{ 1 trackback }

Installer DBI et MySQL pour Ruby sous Ubuntu « 29 minutes par jour : un blog anti-procrastination
May 6, 2009 at 6:16 pm

{ 13 comments… read them below or add one }

Derek November 19, 2007 at 12:15 pm

Thank you so much for this! I had this infuriating problem, which started I thought would be as easy to solve as running apt-get for the mysql server and client packages. Nope — and all I could find on google was how to fix it on Mac OS X. All I needed was to do the following:
> sudo apt-get install libmysqlclient15-dev
> sudo gem install mysql

Rich Brant November 19, 2007 at 1:11 pm

Derek, so glad it helped you out!

Waheed Barghouthi March 2, 2009 at 9:23 am

Thanks dude

Ron Phillips April 25, 2009 at 12:55 pm

Whew! You wouldn’t believe how many pages have been written on this. Yours is the most current, I guess,

Thank you!

Ron

Doug May 29, 2009 at 11:30 am

Bingo! Thanks, man.

inoe June 19, 2009 at 9:54 am

Thanks Brow!

Keith June 23, 2009 at 10:00 am

Thankyou thankyou thankyou!!! The world is a better place with you in it!

li September 5, 2009 at 2:50 pm

If you’re using MySQL 5.0:

sudo apt-get install libmysqlclient15-dev

lukas February 18, 2010 at 4:50 pm

@li: thanks, exactly what i needed. :)

Dan Garland May 3, 2010 at 4:08 am

Thanks for this. On Ubuntu 10.0.4 “Lucid” the package is named libmysqlclient16-dev

HTH

Fedyashev Nikita May 5, 2010 at 4:17 am

Thanks, Rich!

Ganesh May 19, 2010 at 11:36 am

Thax dude it helps me a lot

micky June 16, 2010 at 5:37 am

“sudo apt-get install libmysqlclient15-dev” … saved me on ubuntu10.04

Leave a Comment

Previous post:

Next post: