Search code examples
rubyrubygemsmysql2

Installing mysql2 gem rails on mac


I am trying to do gem install of mysql2 but it looks like it is not finding where mysql is installed. I installed mysql using the XAMPP stack on my mac. I read other post but yield to no results.

sudo  gem install mysql2 -- --with-mysql-config=/Applications/XAMPP/xamppfiles/bin/mysql_config 
Building native extensions.  This could take a while...
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=/Applications/XAMPP/xamppfiles/bin/mysql_config
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... 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=/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
    --with-mysql-config


Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/mysql2-0.3.11 for inspe

Solution

  • While building native extensions, gem is looking for mysql headers. These might be in /Application/XAMPP/... and you could point to them via the following args to gem -

    --with-mysql-dir=/path/to/mysql \
    --with-mysql-include=/path/to/mysql/headers \
    --with-mysql-lib=/path/to/lib \
    --with-mysql-config=/path/to/configs
    

    Alternatively, if using the XAMPP's mysql isn't necessary, you could get mysql via homebrew and then -

    brew install mysql 
    gem install mysql2
    

    I would prefer the second way. It's cleaner and less confusing as I do not know what changes XAMPP makes to their bundled mysql ;)

    Later if you need to setup such gems on a linux vps, and encounter similar issues, remember to install the -dev packages. E.g. libmysqlclient-dev for mysql.