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

repoles commented Oct 4, 2018

Thanks, man!

@swaincreates

This comment has been minimized.

Copy link

swaincreates commented Oct 5, 2018

Heroic

@vincepreziose

This comment has been minimized.

Copy link

vincepreziose commented Oct 13, 2018

Yaasss!

@mgoggin

This comment has been minimized.

Copy link

mgoggin 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

mckomo commented Nov 28, 2018

Thanks!

@nagative

This comment has been minimized.

Copy link

nagative commented Dec 16, 2018

Thank you so much! You were a big help!

@s1monw1

This comment has been minimized.

Copy link

s1monw1 commented Dec 28, 2018

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

@rslhdyt

This comment has been minimized.

Copy link

rslhdyt commented Jan 18, 2019

save my life

@rikas

This comment has been minimized.

Copy link

rikas 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

Stratus3D commented Jan 23, 2019

Works on my machine!

@tr00gle

This comment has been minimized.

Copy link

tr00gle 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

giuseb commented Jan 29, 2019

excellent, thanks!

@bearpatrol64

This comment has been minimized.

Copy link

bearpatrol64 commented Jan 31, 2019

Hurray!!! That did it!
Thank you!

@rbercam

This comment has been minimized.

Copy link

rbercam commented Feb 5, 2019

Thank you!

@katherine-boost

This comment has been minimized.

Copy link

katherine-boost commented Feb 11, 2019

Much appreciated!

@FlavioLudi

This comment has been minimized.

Copy link

FlavioLudi commented Feb 12, 2019

Thanks!

@kabaken

This comment has been minimized.

Copy link

kabaken commented Feb 13, 2019

ありがとう!

@samholst

This comment has been minimized.

Copy link

samholst commented Feb 20, 2019

You the man!

@CiTroNaK

This comment has been minimized.

Copy link

CiTroNaK commented Feb 21, 2019

Thanks!

@bthibault

This comment has been minimized.

Copy link

bthibault commented Feb 22, 2019

🥇

@fschueller

This comment has been minimized.

Copy link

fschueller commented Mar 12, 2019

Thank you, just saved me quite some time!

@IanPurdom

This comment has been minimized.

Copy link

IanPurdom commented Mar 26, 2019

Thanks a lot man! I was seriously getting crazy!

@oseiyoke

This comment has been minimized.

Copy link

oseiyoke commented Mar 29, 2019

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

@njt1982

This comment has been minimized.

Copy link

njt1982 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

kule 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

Bozazitz commented Apr 2, 2019

thanks bro, saved me loads of time

@SidOfc

This comment has been minimized.

Copy link

SidOfc 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

aemadrid 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

AnshulRiyal 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

baldarn commented May 7, 2019

thanks man!

@gee-forr

This comment has been minimized.

Copy link

gee-forr 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

x1wins commented May 7, 2019

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

@jadwer

This comment has been minimized.

Copy link

jadwer commented May 8, 2019

My hero man, thank you.

@Victorgf87

This comment has been minimized.

Copy link

Victorgf87 commented May 21, 2019

You are the bestest! Thank you

@voltechs

This comment has been minimized.

Copy link

voltechs 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

keferboeck commented May 31, 2019

Brilliant!

@g3ortega

This comment has been minimized.

Copy link

g3ortega commented Jun 3, 2019

Thanks!

@kerraway

This comment has been minimized.

Copy link

kerraway 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

xyingsoft commented Jun 6, 2019

Great!

@AlexeyBoldak

This comment has been minimized.

Copy link

AlexeyBoldak commented Jun 12, 2019

2019 still actual
Thanks!)

@Alphabetus

This comment has been minimized.

Copy link

Alphabetus commented Jun 19, 2019

Mojave 10.14.4
Not all heros wear capes thanks.

This worked:

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

This comment has been minimized.

Copy link

phamdat8 commented Jun 26, 2019

hero !!!

@mallowigi

This comment has been minimized.

Copy link

mallowigi commented Jun 27, 2019

savior!

@alireza-salehi

This comment has been minimized.

Copy link

alireza-salehi commented Jun 30, 2019

my hero!!!

@ruanltbg

This comment has been minimized.

Copy link

ruanltbg commented Jul 17, 2019

Thank you

@michlee1337

This comment has been minimized.

Copy link

michlee1337 commented Jul 18, 2019

Thank you!

@nigh7m4r3

This comment has been minimized.

Copy link

nigh7m4r3 commented Jul 28, 2019

Thank you!

@serjpetrenko

This comment has been minimized.

Copy link

serjpetrenko commented Jul 29, 2019

Thank you!
But seems that doesn't work for macOS Mojave 10.14.5 with gem 'mysql2', '0.3.18'. I got this error:

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/user/.rvm/gems/ruby-2.3.0/gems/mysql2-0.3.18/ext/mysql2
/Users/user/.rvm/rubies/ruby-2.3.0/bin/ruby -I /Users/user/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0 -r ./siteconf20190729-60037-dvrkxr.rb extconf.rb
--with-opt-dir\=/usr/local/opt/openssl
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
Setting rpath to /usr/local/Cellar/mysql/8.0.16/lib
-----
creating Makefile

current directory: /Users/user/.rvm/gems/ruby-2.3.0/gems/mysql2-0.3.18/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/user/.rvm/gems/ruby-2.3.0/gems/mysql2-0.3.18/ext/mysql2
make "DESTDIR="
compiling infile.c
compiling client.c
client.c:359:33: warning: implicit conversion loses integer precision: 'long' to 'unsigned int' [-Wshorten-64-to-32]
        elapsed_time = end_time - start_time;
                     ~ ~~~~~~~~~^~~~~~~~~~~~
client.c:431:3: error: use of undeclared identifier 'my_bool'
  my_bool res = mysql_read_query_result(client);
  ^
client.c:433:19: error: use of undeclared identifier 'res'
  return (void *)(res == 0 ? Qtrue : Qfalse);
                  ^
client.c:762:3: error: use of undeclared identifier 'my_bool'
  my_bool boolval;
  ^
client.c:793:7: error: use of undeclared identifier 'boolval'
      boolval = (value == Qfalse ? 0 : 1);
      ^
client.c:794:17: error: use of undeclared identifier 'boolval'
      retval = &boolval;
                ^
client.c:797:10: error: use of undeclared identifier 'MYSQL_SECURE_AUTH'; did you mean 'MYSQL_DEFAULT_AUTH'?
    case MYSQL_SECURE_AUTH:
         ^~~~~~~~~~~~~~~~~
         MYSQL_DEFAULT_AUTH
/usr/local/Cellar/mysql/8.0.16/include/mysql/mysql.h:188:3: note: 'MYSQL_DEFAULT_AUTH' declared here
  MYSQL_DEFAULT_AUTH,
  ^
client.c:798:7: error: use of undeclared identifier 'boolval'
      boolval = (value == Qfalse ? 0 : 1);
      ^
client.c:799:17: error: use of undeclared identifier 'boolval'
      retval = &boolval;
                ^
client.c:830:38: error: use of undeclared identifier 'boolval'
        wrapper->reconnect_enabled = boolval;
                                     ^
client.c:1163:56: warning: implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'unsigned int' [-Wshorten-64-to-32]
  mysql2rb = mysql2_mysql_enc_name_to_rb(charset_name, charset_name_len);
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~               ^~~~~~~~~~~~~~~~
client.c:1196:38: error: use of undeclared identifier 'MYSQL_SECURE_AUTH'; did you mean 'MYSQL_DEFAULT_AUTH'?
  return _mysql_client_options(self, MYSQL_SECURE_AUTH, value);
                                     ^~~~~~~~~~~~~~~~~
                                     MYSQL_DEFAULT_AUTH
/usr/local/Cellar/mysql/8.0.16/include/mysql/mysql.h:188:3: note: 'MYSQL_DEFAULT_AUTH' declared here
  MYSQL_DEFAULT_AUTH,
  ^
2 warnings and 10 errors generated.
make: *** [client.o] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/user/.rvm/gems/ruby-2.3.0/gems/mysql2-0.3.18 for inspection.
Results logged to /Users/user/.rvm/gems/ruby-2.3.0/extensions/x86_64-darwin-18/2.3.0/mysql2-0.3.18/gem_make.out

An error occurred while installing mysql2 (0.3.18), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.3.18' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  mysql2
@HugApps

This comment has been minimized.

Copy link

HugApps commented Jul 29, 2019

Thanks!

@swarup98

This comment has been minimized.

Copy link

swarup98 commented Aug 2, 2019

thank you!!!

@RCheesley

This comment has been minimized.

Copy link

RCheesley commented Aug 12, 2019

Thanks so much for this!

@fernandoaleman

This comment has been minimized.

Copy link
Owner Author

fernandoaleman commented Aug 12, 2019

@HugApps, @swarup98, @RCheesley you're welcome. Glad it helped. 👍

@darrenmulligan

This comment has been minimized.

Copy link

darrenmulligan commented Aug 19, 2019

Great thanks man

@Yasai-T

This comment has been minimized.

Copy link

Yasai-T commented Aug 29, 2019

thanks 💯

@okekeobasi

This comment has been minimized.

Copy link

okekeobasi commented Aug 29, 2019

Nice.

@peterkos

This comment has been minimized.

Copy link

peterkos commented Sep 30, 2019

I also ran this as sudo to get access to the global libraries. Thanks!!

@matt-hwy1

This comment has been minimized.

Copy link

matt-hwy1 commented Oct 4, 2019

Oh wow, thank you for this!!! Saved my day!

@chicop

This comment has been minimized.

Copy link

chicop commented Oct 10, 2019

Nice, Thanks for this!

@liannivin

This comment has been minimized.

Copy link

liannivin commented Oct 11, 2019

This works!

@kovpack

This comment has been minimized.

Copy link

kovpack commented Oct 16, 2019

Thanx!

@adriancaffarelli

This comment has been minimized.

Copy link

adriancaffarelli commented Oct 31, 2019

🎉

@mom0tomo

This comment has been minimized.

Copy link

mom0tomo commented Nov 3, 2019

You saved my day! Thanx!!

@iwan

This comment has been minimized.

Copy link

iwan commented Nov 7, 2019

Sooo good! Thanks!

@samuelpordeus

This comment has been minimized.

Copy link

samuelpordeus commented Nov 7, 2019

A true hero

@n41l

This comment has been minimized.

Copy link

n41l commented Nov 11, 2019

👍

@MahmoudAgamy

This comment has been minimized.

Copy link

MahmoudAgamy commented Nov 11, 2019

Thanks a lot!!!!!!!!!

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.