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

See also  How to SSH to a server using Ruby - Part I

Support us & keep this site free of annoying ads.
Shop Amazon.com or Donate with Paypal

16 thoughts on “Solved! Installing mysql gem – extconf.rb failed error”

  1. 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”‘

  2. 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

  3. How about Win 7?
    Tried with 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”‘
    it works, but with connector solution, it failed, also met the 1% error

Leave a Comment