MySQL Gem Install ERROR: Failed to build gem native extension

You may run into issues installing the mysql gem on a Linux server running a 64-bit version of MySQL server. When attempting to install the gem its likely you will get errors if you don’t specify a couple options when running the command. The issue I continued to have was the options are clearly listed out and I felt, because of previous issues with 64-bit applications being installed, that I was using the proper options to move past the errors I was seeing. What I finally figured out was the fact that an extra “–” was required in front of the options.

Below I show the error in detail which will then be followed by the command to issue for a successful installation of the MySQL gem on CentOS (should work with any Linux server) running mysql-server.x86_64 or the 64 bit version of mysql-server instead of the 32 bit mysql-server or mysql-server.i386.

Error During MySQL Gem Install:

[root@server ~# gem install mysql
Building native extensions. This could take a while...
ERROR: Error installing mysql:
ERROR: Failed to build gem native extension.

/usr/local/bin/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 ***
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.

Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/usr/local/bin/ruby
--with-mysql-config
--without-mysql-config
--with-mysql-dir
--without-mysql-dir
--with-mysql-include
--without-mysql-include=${mysql-dir}/include
--with-mysql-lib
--without-mysql-lib=${mysql-dir}/lib
--with-mysqlclientlib
--without-mysqlclientlib
--with-mlib
--without-mlib
--with-mysqlclientlib
--without-mysqlclientlib
--with-zlib
--without-zlib
--with-mysqlclientlib
--without-mysqlclientlib
--with-socketlib
--without-socketlib
--with-mysqlclientlib
--without-mysqlclientlib
--with-nsllib
--without-nsllib
--with-mysqlclientlib
--without-mysqlclientlib

Gem files will remain installed in /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7 for inspection.
Results logged to /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/gem_make.out

So instead of just issuing the "gem install mysql" command instead issue the below command that specifiies the base directory as well as the path to the MySQL libraries that are necessary to complete the gem installation.

Command:
gem install mysql -- --with-mysql-dir=/usr/bin --with-mysql-lib=/usr/lib64/mysql

The above command will complete the gem installation as shown below in the "MySQL Gem Install" code block. The first switch used above, --with-mysql-dir=/usr/bin, is to specify the base MySQL installation directory and the second switch used above, --with-mysql-lib=/usr/lib64/mysql, is the location of the libraries which in this case are the shared 64 bit libraries.

Successful Installation:

[root@server ~]# gem install mysql -- --with-mysql-dir=/usr/bin --with-mysql-lib=/usr/lib64/mysql
Building native extensions. This could take a while...
Successfully installed mysql-2.7
1 gem installed
Share