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


{ 10 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. [...] 只是说缺少依赖,但并没有说具体少什么。后来我找到这篇文章,知道了原因,安装了 libmysql-ruby 和 libmysqlclient-dev 库之后就没问题了。 [...]

  6. Thanks. Simple and precise.

  7. thank you :-D

  8. Thks a lot!

  9. How i can do this on Windows?

  10. Thanks a lot man.

Please post your comments/suggestions!