Solved! Installing mysql gem – extconf.rb failed error

Are you attempting to install the mysql gem and you are receiving this error?


Fetching: mysql-2.8.1.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing mysql:
       ERROR: Failed to build gem native extension.

       /usr/bin/ruby1.8 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... no
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
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.

Well, we have the solution for you. Your Linux installation requires that the development libraries for MySQL to be installed in order to build the gem. Here is the proper way for install this gem:

How to install mysql gem

sudo apt-get install libmysql-ruby
sudo apt-get install libmysqlclient-dev
sudo gem install mysql

If you are still having trouble….

Try installing the development package of Ruby 1.8 (or newer) by using the command below, and then attempt to install the MySQL gem:
sudo apt-get install ruby1.8-dev or sudo apt-get install ruby1.9.1-dev


{ 12 Comments }
  1. Thanks! I tried a heap of other solutions including install a dev package and this is the only one that worked.

  2. Thanks a lot. Precisely my problem. Quick way to install the right libraries.

  3. Thank you very much!!!
    This very simple solution for my problem.

  4. [...] the mysql2 gem. Don’t worry, you just need to install a couple other gems. More info on this here sudo apt-get install libmysql-ruby sudo apt-get install [...]

  5. Thanks. Simple and precise.

  6. thank you :-D

  7. Thks a lot!

  8. How i can do this on Windows?

  9. Thanks a lot man.

  10. Michael Gaston on June 13th, 2014 at 11:48 am

    Perfect!

  11. If like me, you have some coworker working on a rails project using Windows (in my case Windows 7).
    In order to install the gem “mysql2″ you need to specify in the command line the path to use the libraries.

    Pre-requisite: Having MYSQL installed.

    Command line to run (replace the path by your installation path)
    gem install mysql2 — ‘–with-mysql-lib=”c:\Program Files\MySQL\MySQL Server 5.6\lib” –with-mysql-include=”c:\Program Files\MySQL\MySQL Server 5.6\include”‘

  12. From *buntu 12.04 inclusive onwards, it has changed to
    sudo apt-get install ruby-mysql

    After 13.10, the libmysql-ruby above no longer exists, use ruby-mysql

Please post your comments/suggestions!