2

I am trying to install mysql2:

sudo gem install mysql2 -v '0.2.7' -- --with-mysql-config=/usr/local/Cellar/mysql/5.5.25a/bin/mysql_config

I'm getting this error:

ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

       /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb --    with-mysql-config=/usr/local/Cellar/mysql/5.5.25a/bin/mysql_config
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.

my mysql install is via brew so my mysql.h file is located at:

/usr/local/Cellar/mysql/5.5.25a/bin/mysql_config

which is why I have the with-mysql-config flag

The mysql_config file is looking for the mysql.h file here:

$basedir/include/mysql/mysql.h

it should be added that I updated my gemfile from

gem 'mysql2

to gem 'mysql2', '0.2.7'

because, I was getting some deprecation warning about the fact that mysql2 0.3.* didn't have active record support. So would either have to upgrade rails to 3.1 or downgrade mysql2. I chose the latter.

Should I edit the mysql_config or is there something else I can do to fix this? Thanks for any help.

Dan K.K.
  • 5,915
  • 2
  • 28
  • 34
bonum_cete
  • 4,730
  • 6
  • 32
  • 56
  • Did you recently upgrade from OS X Snow Leopard, to Lion or Mountain Lion? If so, you'll need to update to the latest version of XCode and make sure Command Line Tool are installed (an option in XCode once it's updated). – Tom Harrison Nov 08 '12 at 22:42
  • no, I didn't. I am going to update my post with the fact that I updated my gemfile from gem 'mysql2' to gem 'mysql2', '0.2.7' – bonum_cete Nov 08 '12 at 22:51
  • Hmm -- my current version of mysql2 is 0.3.11 -- you might want to "gem uninstall mysql2", then see if it builds with a more current version. – Tom Harrison Nov 08 '12 at 22:55
  • Ooh -- just saw your edit. One possible issue is with permissions; when using homebrew you should never need to use sudo -- everything is installed in a subdirectory of your user home and is owned by the user; doing `sudo gem install foo` causes files to be owned by root. Check permissions on the old version (in "Cellar") and make sure all of the file (sources, intermediate files, and the gem itself) is owned by you. If not, delete and reinstall the gem without sudo. – Tom Harrison Nov 08 '12 at 22:58
  • yeah, I'm still the owner. If I try to install mysql2 gem without sudo though I get permission errors. which sound like I'm not the owner somewhere I guess? – bonum_cete Nov 08 '12 at 23:12
  • This seems a likely cause, then. Trying to manually fix permissions is a huge rathole (one I have fallen into :-) -- look for all the places homebrew installs mysql, mostly under /usr/local -- they should all be owned by you. If not, you can change to the parent of the afflicted directory and try `sudo chown -R myname:mygroup mysql/` or similar, but you may be better off just doing `sudo rm -rf mysql/` and reinstalling with homebrew. Good luck! – Tom Harrison Nov 08 '12 at 23:43
  • possible duplicate of [Error installing mysql2: Failed to build gem native extension](http://stackoverflow.com/questions/3608287/error-installing-mysql2-failed-to-build-gem-native-extension) – Dan K.K. Aug 04 '15 at 12:25

2 Answers2

3

I realise this is an old question but for anyone coming here from google I found this solution worked for me: http://www.randomactsofsentience.com/2013/05/gem-install-mysql2-missing-mysqlh-on-os.html.

The short version is to remove compiler flags -Wno-null-conversion -Wno-unused-private-field from /usr/local/Cellar/mysql/5.6.12/bin/mysql_config around line 120 (replace mysql version with whatever you have to find the file).

I'm on OSX with homebrew mysql and rvm using ruby 1.9.3.

paulwib
  • 31
  • 3
  • THIS WORKS. I have visited literally every other related question on stack overflow, this is the only fix that worked for my situation: 10.8, rvm running ruby 1.9.3 – david Jul 11 '13 at 23:16
  • Brilliant, thanks. Reading the linked article, it sounds like the root cause is that the Apple-installed version of gcc doesn't support these flags? – Andrew Vit Jul 15 '13 at 21:51
2

You should install mysql-dev package, here are some headers for compiling native driver.

Dan K.K.
  • 5,915
  • 2
  • 28
  • 34