Getting the mysql gem installed on Ubuntu

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

Category: Ruby/Rails, Ubuntu

8 Responses to “Getting the mysql gem installed on Ubuntu”

  1. Derek

    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

  2. Rich Brant

    Derek, so glad it helped you out!

  3. Waheed Barghouthi

    Thanks dude

  4. Ron Phillips

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

    Thank you!

    Ron

  5. Installer DBI et MySQL pour Ruby sous Ubuntu « 29 minutes par jour : un blog anti-procrastination

    [...] le gem MySQL par contre, j’ai du faire une manip spéciale [...]

  6. Doug

    Bingo! Thanks, man.

  7. inoe

    Thanks Brow!

  8. Keith

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


Leave a Reply



Back to top