Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install mysql2 on MacOS Mojave

Problem

Installing mysql2 gem errors on MacOS Mojave.

Solution

Make sure openssl is installed on Mac via Homebrew.

brew install openssl

Install mysql2 gem.

gem install mysql2 -v '0.5.2' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
@repoles

This comment has been minimized.

Copy link

commented Oct 4, 2018

Thanks, man!

@swaincreates

This comment has been minimized.

Copy link

commented Oct 5, 2018

Heroic

@vincepreziose

This comment has been minimized.

Copy link

commented Oct 13, 2018

Yaasss!

@mgoggin

This comment has been minimized.

Copy link

commented Oct 18, 2018

If you came here because you're trying to bundle your project into a specific directory and the gem won't build, you can tell bundler to use these build flags when it installs the gem.

bundle config build.mysql2 --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
bundle install --path .bundle
@mckomo

This comment has been minimized.

Copy link

commented Nov 28, 2018

Thanks!

@nagative

This comment has been minimized.

Copy link

commented Dec 16, 2018

Thank you so much! You were a big help!

@s1monw1

This comment has been minimized.

Copy link

commented Dec 28, 2018

took me an hour until I finally found your solution, thanks!

@rslhdyt

This comment has been minimized.

Copy link

commented Jan 18, 2019

save my life

@rikas

This comment has been minimized.

Copy link

commented Jan 20, 2019

I still get this error:

cannot load such file -- mysql2/mysql2

when trying to use mysql2 on Rails (5.2.2)

@Stratus3D

This comment has been minimized.

Copy link

commented Jan 23, 2019

Works on my machine!

@tr00gle

This comment has been minimized.

Copy link

commented Jan 24, 2019

Just found this, and it was immensely helpful. How did you reach this solution?

@giuseb

This comment has been minimized.

Copy link

commented Jan 29, 2019

excellent, thanks!

@bearpatrol64

This comment has been minimized.

Copy link

commented Jan 31, 2019

Hurray!!! That did it!
Thank you!

@rbercam

This comment has been minimized.

Copy link

commented Feb 5, 2019

Thank you!

@katherine-boost

This comment has been minimized.

Copy link

commented Feb 11, 2019

Much appreciated!

@FlavioLudi

This comment has been minimized.

Copy link

commented Feb 12, 2019

Thanks!

@kabaken

This comment has been minimized.

Copy link

commented Feb 13, 2019

ありがとう!

@samholst

This comment has been minimized.

Copy link

commented Feb 20, 2019

You the man!

@CiTroNaK

This comment has been minimized.

Copy link

commented Feb 21, 2019

Thanks!

@bthibault

This comment has been minimized.

Copy link

commented Feb 22, 2019

🥇

@fschueller

This comment has been minimized.

Copy link

commented Mar 12, 2019

Thank you, just saved me quite some time!

@IanPurdom

This comment has been minimized.

Copy link

commented Mar 26, 2019

Thanks a lot man! I was seriously getting crazy!

@oseiyoke

This comment has been minimized.

Copy link

commented Mar 29, 2019

Thanks a lot man. Been on it for an hour!!

@njt1982

This comment has been minimized.

Copy link

commented Mar 29, 2019

I have this in ~/.bundle/config:

BUNDLE_BUILD__NOKOGIRI: "--use-system-libraries --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/libxml2/"
BUNDLE_BUILD__MYSQL2: "--with-ldflags=-L/usr/local/opt/openssl@1.1/lib --with-cppflags=-I/usr/local/opt/openssl@1.1/include"
@kule

This comment has been minimized.

Copy link

commented Apr 2, 2019

This worked for me:

bundle config build.mysql2 --with-opt-dir=/usr/local/opt/openssl

@Bozazitz

This comment has been minimized.

Copy link

commented Apr 2, 2019

thanks bro, saved me loads of time

@SidOfc

This comment has been minimized.

Copy link

commented Apr 4, 2019

I got here because I got don't know how to set rpath on your system error initially. Upon googling you'll reach this SO thread which only details either installing / reinstalling openssl but since I already did this and all other configuration options mentioned were outdated for me, this wasn't very useful. Only after fiddling with many different configuration options, none of which worked, this actually got it to work for me.

It really does suck that the combination of mysql+brew+macos seems to break with every OS update, though :(

macOS 10.14.3
fish 3.0.1
openssl 1.0.2r

With all that being said, thanks a lot, you saved me some good amount of time as well!

@aemadrid

This comment has been minimized.

Copy link

commented Apr 24, 2019

that fixed building eventmachine too for me:

gem install eventmachine -v '1.2.7' --source 'https://rubygems.org/' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include

@AnshulRiyal

This comment has been minimized.

Copy link

commented Apr 25, 2019

This will fix your issue while bundle.
gem install mysql2 -v '0.3.21' -- --with-mysql-config=/usr/local/bin/mysql_config --srcdir=/usr/local/include/mysql/ --platform=ruby --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include

Still looking resolution for `require': cannot load such file -- mysql2/mysql2 (LoadError) while running command rails s.

@baldarn

This comment has been minimized.

Copy link

commented May 7, 2019

thanks man!

@gee-forr

This comment has been minimized.

Copy link

commented May 7, 2019

This helped me when the gem install kept on errorring out about not being able to find the mysql client... I modified @AnshulRiyal's command a little:

On Mojave, using homebrew.

gem install mysql2 -v '0.4.10' -- --with-mysql-config=/usr/local/opt/mysql-client/bin/mysql_config --srcdir=/usr/local/opt/mysql/include --platform=ruby --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
@x1wins

This comment has been minimized.

Copy link

commented May 7, 2019

i have taken one all day for this problem
awesome! thanks!!

@jadwer

This comment has been minimized.

Copy link

commented May 8, 2019

My hero man, thank you.

@Victorgf87

This comment has been minimized.

Copy link

commented May 21, 2019

You are the bestest! Thank you

@voltechs

This comment has been minimized.

Copy link

commented May 25, 2019

I only seemed to need bundle config build.mysql2 --with-ldflags=-L/usr/local/opt/openssl/lib. When I used bundle config build.mysql2 --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include, I got the following error:

Fetching mysql2 0.5.2
Installing mysql2 0.5.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/dale/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/mysql2-0.5.2/ext/mysql2
/Users/dale/.rbenv/versions/2.6.2/bin/ruby -I /Users/dale/.rbenv/versions/2.6.2/lib/ruby/2.6.0 -r ./siteconf20190525-54468-194taa7.rb extconf.rb
--with-ldflags\=-L/usr/local/opt/openssl/lib\ --with-cppflags\=-I/usr/local/opt/openssl/include
checking for rb_absint_size()... *** 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=/Users/dale/.rbenv/versions/2.6.2/bin/$(RUBY_BASE_NAME)
/Users/dale/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:467:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
	from /Users/dale/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:552:in `try_link0'
	from /Users/dale/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:570:in `try_link'
	from /Users/dale/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:782:in `try_func'
	from /Users/dale/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:1069:in `block in have_func'
	from /Users/dale/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:959:in `block in checking_for'
	from /Users/dale/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:361:in `block (2 levels) in postpone'
	from /Users/dale/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:331:in `open'
	from /Users/dale/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:361:in `block in postpone'
	from /Users/dale/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:331:in `open'
	from /Users/dale/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:357:in `postpone'
	from /Users/dale/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:958:in `checking_for'
	from /Users/dale/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:1068:in `have_func'
	from extconf.rb:25:in `<main>'
@keferboeck

This comment has been minimized.

Copy link

commented May 31, 2019

Brilliant!

@g3ortega

This comment has been minimized.

Copy link

commented Jun 3, 2019

Thanks!

@kerraway

This comment has been minimized.

Copy link

commented Jun 5, 2019

This worked for me:

bundle config build.mysql2 --with-opt-dir=/usr/local/opt/openssl

THX. Works for me.

bundle config build.mysql2 --with-opt-dir=/usr/local/opt/openssl
bundle install
@xyingsoft

This comment has been minimized.

Copy link

commented Jun 6, 2019

Great!

@edcock

This comment has been minimized.

Copy link

commented Jun 11, 2019

thanks man !

@AlexeyBoldak

This comment has been minimized.

Copy link

commented Jun 12, 2019

2019 still actual
Thanks!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.