Last Updated: August 16, 2018
·
15.05K
· fousa

Install mysql2 gem with MySQL installed with Homebrew

I always get an error when running bundle install with this gem added to the Gemfile.

gem "mysql2", "0.3.11"

The error looks like this:

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /Users/fousa/.rvm/rubies/ruby-1.9.2-p320/bin/ruby extconf.rb 
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... no

...

An error occurred while installing mysql2 (0.2.18), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.2.18'` succeeds before bundling.

But running gem install mysql2 -v '0.2.18' doesn't seem to work... Well, the following command does, just add some homebrew parameters.

gem install mysql2 -v '0.2.18' -- --with-mysql-lib=/usr/local/Cellar/mysql/5.5.19/lib --with-mysql-dir=/usr/local/Cellar/mysql/5.5.19 --with-mysql-config=/usr/local/Cellar/mysql/5.5.19/bin/mysql_config --with-mysql-include=/usr/local/Cellar/mysql/5.5.19/include

Don't forget to pass the correct Homebrew directories.

2 Responses
Add your response

i'd suggest a variant of the gem install command that does not depend on a specific version of mysql.

gem install mysql2 -v '0.2.18' -- \
  --with-mysql-lib=$(brew --prefix mysql)/lib \
  --with-mysql-dir=$(brew --prefix mysql) \
  --with-mysql-config=$(brew --prefix mysql)/bin/mysql_config \
  --with-mysql-include=$(brew --prefix mysql)/include

it should even be possible to be simplified down to

gem install mysql2 -v '0.2.18' -- \
  --with-mysql-dir=$(brew --prefix mysql)

because the extconf will derive mysql-lib and mysql-include from the mysql-dir argument.

and if you know you have a working mysql_config you should be able to use just

gem install mysql2 -v '0.2.18' -- \
  --with-mysql-config=$(brew --prefix mysql)/bin/mysql_config

and extconf will call it with arguments to extract proper flags:

$ mysql_config --libs
-L/usr/local/Cellar/mysql/5.7.17/lib -lmysqlclient -lssl -lcrypto

$ mysql_config --include
-I/usr/local/Cellar/mysql/5.7.17/include/mysql
over 1 year ago ·

What worked for me was:

gem install mysql2 -v '0.3.15' -- --with-mysql-config=$(brew --prefix mysql)/bin/mysql_config
over 1 year ago ·