62

I am trying to install the mysql2 gem via the Bundler, but it keeps dying with the following error:

** executing command
/home/cc_rails/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/rubygems/installer.rb:483:in 'rescue in block in build_extensions': 
ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)

/home/cc_rails/.rvm/rubies/ruby-1.9.2-p0/bin/ruby extconf.rb --with-mysql-config=/usr/bin/mysql_config
checking for rb_thread_blocking_region()... yes
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=/home/cc_rails/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
--with-mysql-config
Gem files will remain installed for inspection.

Most things I found via Googling recommended passing the --with-mysql-config parameter to fix it. So, based on:

$ which mysql_config
/usr/bin/mysql_config

I added the following to the Bundler's config:

$ bundle config build.mysql2 --with-mysql-config='/usr/bin/mysql_config'

However, still no luck -- same crash as above.

Since it's dying with the error mysql.h is missing, I checked for that, and it's allegedly around, just can't be found by the Bundler.

$ find / -name mysql.h
/usr/include/mysql5/mysql/mysql.h

Any thoughts?

T.J. Schuck
  • 3,645
  • 2
  • 19
  • 20
  • 2
    Have you tried the solution at http://stackoverflow.com/questions/3608287/installing-mysql2-gem-for-rails-3 ? – Wrikken Sep 20 '10 at 21:01
  • Otherwise, you might just edit `ext/mysql2/extconf.rb` and enter the correct path to your mysql dir yourself (it's near the top). – Wrikken Sep 20 '10 at 21:03

9 Answers9

95

Answer was similar to the one Wrikken posted -- here's what I did to fix it for the sake of future readers.

(This is for RHEL 5.5 -- similar but different commands apply for Ubuntu/Debian/etc.)

Doing sudo yum list installed will print out all installed packages on your machine (note: yum on RHEL requires you add a Red Hat Network repository [I use EPEL], and run it via sudo).

I had mysql and mysql-server, which explained why MySQL worked fine for every pre-existing app, but no mysql-devel, which is necessary to fix that mysql.h is missing error and similar other build errors.

Long story short, after a mysqldump -u root -ppassword --all-databases > full-dump.sql for safety, it was fixed with a simple

sudo yum install mysql-devel
T.J. Schuck
  • 3,645
  • 2
  • 19
  • 20
  • 9
    In mac, do `brew install mysql` http://stackoverflow.com/questions/3608287/installing-mysql2-gem-for-rails-3 – Peter Ehrlich Jun 01 '12 at 18:29
  • On my installation, `mysql-devel` was installed, but `/usr/include/mysql` was empty. `mysql.h` was located in `/usr/include/mysql51` instead. So I ran `sudo rmdir /usr/include/mysql ; sudo ln -s /usr/include/mysql51 /usr/include/mysql` and voila! Mysql2 now builds properly. – Zee Sep 28 '12 at 17:26
  • for others to find this answer: *** extconf.rb failed *** (works also for fedora) – User Nov 07 '12 at 15:34
33

For Ubuntu have to install following. libmysqlclient-dev libmysqlclient16

Indika K
  • 1,332
  • 17
  • 27
  • When I tried this on Ubuntu 16.04, there was no `libmysqlclient16`, only `libmysqlclient20`. I suspect that whatever version of Ubuntu you use, something like `sudo apt-get install 'libmysqlclient*'` will install both the required packages, as long as the updated version doesn't break anything. – mwfearnley Jun 18 '16 at 16:26
20

For Mac with a brew install of mysql the following solution fixed the problem for me:

I edited the mysql_config file in /usr/local/Cellar/mysql/5.6.12/bin and removed the W-compiler options -Wno-null-conversion and -Wno-unused-private-field for cflags and cxxflags.

This solved the problem with "gem install mysql2".

Reference: http://www.randomactsofsentience.com/2013/05/gem-install-mysql2-missing-mysqlh-on-os.html

guido
  • 1,418
  • 13
  • 16
9

The above problem will be occured because the mysql-devel package is not properly installed in your system. I will be explain the same in Centos of how to fix it. When you try to install that package using,

    yum install mysql-devel

somtimes there will be some clash occurs with the existing packages that gets installed if you install the MySql-Administrative tool and MySQL query browser.

In that case, you need to uninstall all the existing mysql2 packages and install it again.

    rpm -qa -last | grep -i mysql
    yum remove MySQL-server-5.5.27-1.rhel5
    yum remove MySQL-client-5.5.27-1.rhel5
    yum remove mysql-gui-tools-5.0r12-1rhel4
    yum remove mysql-query-browser-5.0r12-1rhel4-a

So, you can uninstall whatever mysql things displayed with rpm -qa as like above.

Then you can install the mysql-server and mysql-client.

    yum install mysql-server
    yum install mysql-client

Now you do the installation of mysql-devel package.

    yum install mysql-devel

Now there is no package clashes and you can able to install the mysql2 gem.

    gem install mysql2 -v '0.3.11'

Now your mysql2 gem will be successfully installed and you are good to go.

maniempire
  • 791
  • 11
  • 12
3

I got the same error. and for ubuntu 16. I had to write below command:

sudo apt-get install libmysqlclient-dev 

and it work back.

Manish Shrivastava
  • 30,617
  • 13
  • 97
  • 101
1

In my case the problem was a misbehaving mysql_config script. When invoked by the command line with the --cflags option it would return a string of options containing:

-Xclang -target-feature -Xclang -aes -Qunused-arguments

For some reason the call to have_header('mysql.h') in the extconf.rb script would fail if those option were included.

What worked for me was to hand edit the mysql_config file removing the reference to those options from the line:

cflags="-I$pkgincludedir -Os -w -pipe -march=native -Xclang -target-feature -Xclang -aes -Qunused-arguments  -O2 -g -DDBUG_OFF " #note: end space!

which I rewrote as:

cflags="-I$pkgincludedir -Os -w -pipe -march=native  -O2 -g -DDBUG_OFF " #note: end space!
user2449437
  • 71
  • 1
  • 2
0

I ran into this issue while bundle installing for redmine on fedora 23. The solution I found was to issue this command - sudo dnf install redhat-rpm-config.

Not only did this fix my issue with installing mysql2, but also for nokogiri and redcarpet.

jshbrmn
  • 1,737
  • 3
  • 22
  • 52
0

On my Ubuntu 16.04.4 LTS running mysql Ver 15.1 Distrib 10.0.34-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2, the following worked:

sudo apt-get install libmariadb-client-lgpl-dev

I saw, that the error message of the current installer suggests running

apt-get install libmysqlclient-dev

This might work as well.

Matthias Luh
  • 503
  • 6
  • 11
0

I know this is ancient, but if anyone still gets this zlib error, make certain that you typed: rvm use

(whatever version you're using)

I could have sworn I did that. Just posting in case anyone is pulling their hair out and this helps. If not good luck. :)

Kevin
  • 1,489
  • 2
  • 20
  • 30