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
DeliciousStumbleUponDiggTwitterFacebookRedditLinkedInEmail
Tags: , , , , , , , , , ,
27 Responses to “MySQL Gem Install ERROR: Failed to build gem native extension”
  1. Allimuthu Visweswaran says:

    Great!@@ Helped me a lot!!!!!!

    You are awesome

    [Reply]

    alex Reply:

    Sure thing. Glad it helped.

    [Reply]

  2. Juan L. Ortiz Petrone says:

    Successfull install!

    Thank you very much!

    [Reply]

    alex Reply:

    No problem. Glad you were able to install the gem successfully.

    [Reply]

  3. Rupesh says:

    I did the same thins, It still shows the error

    [Reply]

    alex Reply:

    Are you sure that is the correct location of your MySQL installation? I would assume that its possibly in another directory which is why you are receiving the error still. Type the below command and provide the results.

    gem list | grep -i sql

    [Reply]

  4. milestone8k says:

    Thanks for sharing this. I spent two hours trying to figure this out. Your solutions worked great for me on Fedora Core 11 64bit.

    [Reply]

    alex Reply:

    Hello milestone8k,

    Glad to hear it helped. I appreciate you posting the feedback.

    [Reply]

  5. Rodney says:

    I did the ‘gem list | grep -i sql’ and got the following:

    sqldsl (1.4.6)
    sqlite3-ruby (1.2.2, 1.2.1)

    Is this correct? Thanks for your help. I have been trying to solve this for 2 weeks.

    [Reply]

    alex Reply:

    Hello Rodney,

    Nope. You should see something similar to the below.

    C:\Users\alex>gem list | grep sql
    mysql (2.7.3)

    [Reply]

  6. hitter says:

    thanks ;-)

    [Reply]

    alex Reply:

    Hello hitter,

    No problem. Glad it helped.

    [Reply]

  7. Антон Сенников says:

    Занимательная и интересная статья у вас. В отличие от большинства других подобных почти нет воды! :)

    [Reply]

    alex Reply:

    Hello Антон Сенников,

    Glad you like the articles. Thanks for the feedback.

    Thanks.
    alex

    [Reply]

  8. Rahul says:

    Hi Alex,
    I am using a windows machine. So what would be the path for the following options:
    “– –with-mysql-dir” and “–with-mysql-lib”. Ruby is installed in “C:\Program Files\” folder.
    Thanks,
    Rahul.

    [Reply]

    alex Reply:

    Hello Rahul,

    Both of those directories will relate to how MySQL was installed but by default it will be something similar to C:\Program Files\MySQL\MySQL Server 5.1\bin. However you might be able to just “gem install mysql”.

    Hope that helps.

    Thanks.
    alex

    [Reply]

  9. peelman says:

    this was catching me as well; trying to install redmine under debian sid. Thanks for posting!

    [Reply]

    alex Reply:

    Hello peelman,

    No problem. Glad it helped you out.

    Thanks.
    alex

    [Reply]

  10. Сергей Александров says:

    Просто отлично! Все очень толково и грамотно, и в то же время без излишних умствований и самолюбования, и на доступном языке. Редкий случай когда человек делится актуальной и востребованной инфой. Спасибо автору!

    [Reply]

    alex Reply:

    Hello Сергей Александров,

    Thanks you for the generous comments. Hope to continue providing you content that you find useful.

    Thanks.
    alex

    [Reply]

  11. thomas says:

    on debian :
    $ aptitude install libmysql-ruby

    it worked fine

    cheers

    thomas

    [Reply]

    alex Reply:

    Hello thomas,

    Great.. Thanks for taking the time to provide the Debian info! Much appreciated..

    Thanks.
    alex

    [Reply]

    gie Reply:

    Wow…..
    helpfull thomas…:)

    thanks..

    [Reply]

  12. Nicholas Henry says:

    On RHEL (Red Hat, Fedora, CentOS, etc)
    $ yum -y install mysql-devel

    [Reply]

    alex Reply:

    Hello Nicholas,

    That is how you install the mysql-devel package but are you saying this would have resolved the initial issue with the gem and mysql 64-bit because during the initial gem installation I did have mysql-devel installed as well. Also it is definitely possible that he gem has been updated since the article was written. Regardless I was curious what the reply was meaning.

    Thanks.
    alex

    [Reply]

  13. Nicholas Henry says:

    Alex,

    Sorry for not being specific.. Yes, the above was all that I had to do to get ‘ gem install mysql –version “= 2.8.1″ ‘ working on CentOS 5.5 a couple of days ago after I was having the same type of issues as mentioned above.

    Hope this helps,
    Nick

    [Reply]

    alex Reply:

    Hello Nicholas,

    No problem at all. Thanks for following up. It is definitely possible that the gem or mysql packages have changed since the initial article so we appreciate you posting a follow up. If the article itself doesn’t assist hopefully people will read the comments and locate the solution you mention. Thanks again for taking the time to post a comment!

    Thanks.
    alex

    [Reply]

  14.  
Leave a Reply

*Type the letter/number combination in the abvoe field before clicking submit.

*