27

I got this error:

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

/usr/bin/ruby1.8 extconf.rb
checking for rb_thread_blocking_region()... no
checking for mysql.h... no
checking for mysql/mysql.h... no
-----
mysql.h is missing.  please check your installation of mysql and try again.
-----
*** 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/ruby1.8
    --with-mysql-config
    --without-mysql-config


Gem files will remain installed in /var/lib/gems/1.8/gems/mysql2-0.2.6 for inspection.
Results logged to /var/lib/gems/1.8/gems/mysql2-0.2.6/ext/mysql2/gem_make.out

I have tried:

  • uninstall mysql2 and install it again
  • uninstall mysql with apt-get
  • installd dev-tools from mysql
  • and almost all the pages on Google

How can I fix this?

j0k
  • 22,600
  • 28
  • 79
  • 90
DenBelg
  • 281
  • 1
  • 3
  • 4

8 Answers8

94

You need to install libmysqlclient-dev before installing the mysql2 gem:

sudo apt-get install libmysqlclient-dev
gem install mysql2

This should fix this problem on the latest Ubuntu versions.

Andre Schweighofer
  • 2,759
  • 1
  • 26
  • 25
  • Worked for me on kali only my error was checking for mysql_query() in -lmysqlclient... no Thanks. – Douglas G. Allen Oct 31 '14 at 09:54
  • If you have mariadb,then due to binary updation it might be unable to install `libmysqlclient-dev`. It would be good if we install `libmariadbclient-dev`,it will resolve both the issue. – Jaswinder Dec 01 '17 at 14:54
9

Thank Andre Schweighofer, just to it

sudo apt-get install libmysqlclient-dev
gem install mysql2

..
bundle update
bundle install

DONE !!!

Mike Nguyen
  • 1,024
  • 1
  • 11
  • 16
3

It require to install mysql development package

yum install mysql-devel
Mahattam
  • 5,405
  • 4
  • 23
  • 33
1

These are multiple causes of this failure :

  1. Bad versions compatibility between ruby and rubygems
  2. Missing libmysqlclient-dev
  3. gem not locating the
  4. Mysql 32bit on a 64 bit system

Solutions

  1. Install rvm with latest stable release, You can switch after via rvm between ruby versions

    curl -L https://get.rvm.io | bash -s stable

  2. Install the dependecy

    sudo apt-get install libmysqlclient-dev

  3. locate mysql_config on your system (find / -name mysql_config) and run

    gem install mysql -- --with-mysql-config=/path/to/mysql_config

  4. Install the 64 bit version of mysql for your system
amine
  • 320
  • 3
  • 7
1

It could be due to mysql updated binary ( In my case it was using Mariadb-10.0.31) and on installing the missing library( libmysqlclient-dev ), I got the following message that it would be impossible to resolve:

$ sudo apt-get install libmysqlclient-dev

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 libmysqlclient-dev : Depends: libmysqlclient18 (= 5.5.58-0ubuntu0.14.04.1)

But I resolved the above issue by first installing libmariadbclient-dev as:-

 $ sudo apt-get install libmariadbclient-dev

It will install libmysqlclient-dev as well but by first resolving mariadb dependency.

Jaswinder
  • 1,455
  • 14
  • 27
1

Its helful to use the search here ;): ruby-mysql

here is another thread ;) with the same problem: ruby-mysql2

and many more when you search for "ruby mysql2" or something like this.

Community
  • 1
  • 1
René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • I tried most of the links and the are also for windows problems with mysql2. But i try it on a ubuntu. That's the strange thing! – DenBelg Mar 07 '11 at 12:53
  • Then you should read the articles a second time ;) there was an example how to install the package over apt-get try this. – René Höhle Mar 07 '11 at 13:03
0

On Ubuntu 10.10, I had to install libmysqlclient16-dev.

Chad Johnson
  • 21,215
  • 34
  • 109
  • 207
0

The way I solved was to inspect the mkmf.log, which in my case was somewhere in ./gems/ruby-2.2.3/extensions/x86_64-linux/2.2.0/mysql2-0.4.2/mkmf.log

In my case, there was an error that it could not find -lgmp, so it was missing the gmp library , so installed via apt-get , in this case libgmp-dev.