Error Connecting to MySQL using Ruby on Rails in Linux – and its Solution

Ruby Logo

I am currently working on a new Rails project. Its been a while since I worked with rails – so I updated Rails using the command ‘sudo gem update --system‘. I started the inbuilt server and tried to open a page – and to my surprise, I found that Rails is having trouble connecting to the MySQL server. It was apparent that the mysql gem was missing. So I tried to install it using the command ‘sudo gem install mysql‘. That failed too – this is the error I got…

Building native extensions.  This could take a while...
ERROR:  Error installing mysql:
        ERROR: Failed to build gem native extension.

/usr/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/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/lib/ruby/gems/1.8/gems/mysql-2.7 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/mysql-2.7/gem_make.out

After a couple of hours on Google, I managed to fix the issue – and I am posting the solution here for others who might run into the same problem.

Install the necessary Libraries

First, you must install a couple of mysql libraries(I am assuming that you have MySQL server already installed). If you are on Red Hat/Fedora/CentOS etc., run the command…

sudo yum install ruby-mysql mysql-devel

If you are on Debian/Ubuntu/etc., run this command…

sudo apt-get install libmysqlclient15-dev libmysqlclient15off libmysql-ruby

Install the gem

Once the installation is done, enter this command to install mysql gem

sudo gem install mysql -- --with-mysql-config=/usr/bin/mysql_config

If your mysql_config is not in the standard location, update the command with the right path.

The mysql gem should be installed now. These commands worked for me – your milage may vary. If you still have trouble, try one of the following links…

More Resources

4 Comments

  1. Just diving into Rails and hit this exact same trouble. Knew I needed libraries but didn’t know which ones. THis worked like a charm.

    Thanks!

  2. Thanks a lot! I’ve been Googling for the past couple of hours and all I got were sources from either Mac OSX or Windows systems. Finally, I found your article which targets CentOS systems.

    I wasn’t able to install mysql-devel and ruby-mysql from the base repository, though. In my case, I had to get mysql-devel from the Remi repo, which is where I also got my MySQL 5.1.50 installation. The latest MySQL from the base repo at this time was 5.0.xx. I got ruby-mysql from the EPEL repo.

Comments are closed.