Can't install mysql2 gem on macOS Sierra
I just had the same problem, tried all of the solutions listed above, then commenced to bang my head against they keyboard for a couple of hours.
I then thought to try and install/reinstall the Xcode Command Line Tools:
xcode-select --install
Once I did that the mysql2 gem installed w/ no problems. I hope that does the trick!
When you install openssl via brew, you should get the following message:
Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries
Generally there are no consequences of this for you. If you build your own software and it requires this formula, you'll need to add to your build variables:
LDFLAGS: -L/usr/local/opt/openssl/lib
CPPFLAGS: -I/usr/local/opt/openssl/include
PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig
You can set these build flags (for the local application) by running the following:
bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"
This worked for me.
See bundler's documentation for more information.
Lots of great answers, I was able to combine them into this:
gem install mysql2 --source 'https://rubygems.org/' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
because I was not comfortable with bundle config
I am here to share my fix, since the other answers didn't work.
For my environment, I need MySQL 5.6 so I had to use:
brew install mysql56
instead of brew install mysql
Bundle installing the mysql2 gem kept failing, until:
brew link mysql56
I also ran afterwards:
mysql.server start
The last step might be unnecessary, but just in case.
With Mac OS 10.15 Catalina when I tried Alessandro's fix the gem and extensions could be installed correctly but bundle install
failed. What worked was just:
bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib"
without the cppflags
part.