Skip to content

Instantly share code, notes, and snippets.

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

For MacOS Catalina, visit Install mysql2 on MacOS Catalina

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 repoles commented Oct 4, 2018

Thanks, man!

@swaincreates

This comment has been minimized.

Copy link

@swaincreates swaincreates commented Oct 5, 2018

Heroic

@vincepreziose

This comment has been minimized.

Copy link

@vincepreziose vincepreziose commented Oct 13, 2018

Yaasss!

@mgoggin

This comment has been minimized.

Copy link

@mgoggin 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 mckomo commented Nov 28, 2018

Thanks!

@nagative

This comment has been minimized.

Copy link

@nagative nagative commented Dec 16, 2018

Thank you so much! You were a big help!

@s1monw1

This comment has been minimized.

Copy link

@s1monw1 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 rslhdyt commented Jan 18, 2019

save my life

@rikas

This comment has been minimized.

Copy link

@rikas 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 Stratus3D commented Jan 23, 2019

Works on my machine!

@tr00gle

This comment has been minimized.

Copy link

@tr00gle 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 giuseb commented Jan 29, 2019

excellent, thanks!

@bearpatrol64

This comment has been minimized.

Copy link

@bearpatrol64 bearpatrol64 commented Jan 31, 2019

Hurray!!! That did it!
Thank you!

@rbercam

This comment has been minimized.

Copy link

@rbercam rbercam commented Feb 5, 2019

Thank you!

@katherine-boost

This comment has been minimized.

Copy link

@katherine-boost katherine-boost commented Feb 11, 2019

Much appreciated!

@flavioludi

This comment has been minimized.

Copy link

@flavioludi flavioludi commented Feb 12, 2019

Thanks!

@kabaken

This comment has been minimized.

Copy link

@kabaken kabaken commented Feb 13, 2019

ありがとう!

@samholst

This comment has been minimized.

Copy link

@samholst samholst commented Feb 20, 2019

You the man!

@CiTroNaK

This comment has been minimized.

Copy link

@CiTroNaK CiTroNaK commented Feb 21, 2019

Thanks!

@bthibault

This comment has been minimized.

Copy link

@bthibault bthibault commented Feb 22, 2019

🥇

@fschueller

This comment has been minimized.

Copy link

@fschueller fschueller commented Mar 12, 2019

Thank you, just saved me quite some time!

@IanPurdom

This comment has been minimized.

Copy link

@IanPurdom IanPurdom commented Mar 26, 2019

Thanks a lot man! I was seriously getting crazy!

@oseiyoke

This comment has been minimized.

Copy link

@oseiyoke oseiyoke commented Mar 29, 2019

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

@njt1982

This comment has been minimized.

Copy link

@njt1982 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 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 Bozazitz commented Apr 2, 2019

thanks bro, saved me loads of time

@SidOfc

This comment has been minimized.

Copy link

@SidOfc 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 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 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 baldarn commented May 7, 2019

thanks man!

@gee-forr

This comment has been minimized.

Copy link

@gee-forr 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, High Sierra, or Catalina, using homebrew.

gem install mysql2 -v 'YOUR.VERSION.HERE' -- --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 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 jadwer commented May 8, 2019

My hero man, thank you.

@Victorgf87

This comment has been minimized.

Copy link

@Victorgf87 Victorgf87 commented May 21, 2019

You are the bestest! Thank you

@voltechs

This comment has been minimized.

Copy link

@voltechs 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 keferboeck commented May 31, 2019

Brilliant!

@g3ortega

This comment has been minimized.

Copy link

@g3ortega g3ortega commented Jun 3, 2019

Thanks!

@kerraway

This comment has been minimized.

Copy link

@kerraway 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 xyingsoft commented Jun 6, 2019

Great!

@AlexeyBoldak

This comment has been minimized.

Copy link

@AlexeyBoldak AlexeyBoldak commented Jun 12, 2019

2019 still actual
Thanks!)

@Alphabetus

This comment has been minimized.

Copy link

@Alphabetus 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 phamdat8 commented Jun 26, 2019

hero !!!

@mallowigi

This comment has been minimized.

Copy link

@mallowigi mallowigi commented Jun 27, 2019

savior!

@alireza-salehi

This comment has been minimized.

Copy link

@alireza-salehi alireza-salehi commented Jun 30, 2019

my hero!!!

@ruanltbg

This comment has been minimized.

Copy link

@ruanltbg ruanltbg commented Jul 17, 2019

Thank you

@michlee1337

This comment has been minimized.

Copy link

@michlee1337 michlee1337 commented Jul 18, 2019

Thank you!

@nigh7m4r3

This comment has been minimized.

Copy link

@nigh7m4r3 nigh7m4r3 commented Jul 28, 2019

Thank you!

@serjpetrenko

This comment has been minimized.

Copy link

@serjpetrenko 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 HugApps commented Jul 29, 2019

Thanks!

@swarup98

This comment has been minimized.

Copy link

@swarup98 swarup98 commented Aug 2, 2019

thank you!!!

@RCheesley

This comment has been minimized.

Copy link

@RCheesley RCheesley commented Aug 12, 2019

Thanks so much for this!

@fernandoaleman

This comment has been minimized.

Copy link
Owner Author

@fernandoaleman fernandoaleman commented Aug 12, 2019

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

@darrenmulligan

This comment has been minimized.

Copy link

@darrenmulligan darrenmulligan commented Aug 19, 2019

Great thanks man

@Yasai-T

This comment has been minimized.

Copy link

@Yasai-T Yasai-T commented Aug 29, 2019

thanks 💯

@okekeobasi

This comment has been minimized.

Copy link

@okekeobasi okekeobasi commented Aug 29, 2019

Nice.

@peterkos

This comment has been minimized.

Copy link

@peterkos 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 matt-hwy1 commented Oct 4, 2019

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

@shiik0

This comment has been minimized.

Copy link

@shiik0 shiik0 commented Oct 10, 2019

Nice, Thanks for this!

@liannivin

This comment has been minimized.

Copy link

@liannivin liannivin commented Oct 11, 2019

This works!

@kovpack

This comment has been minimized.

Copy link

@kovpack kovpack commented Oct 16, 2019

Thanx!

@adriancaffarelli

This comment has been minimized.

Copy link

@adriancaffarelli adriancaffarelli commented Oct 31, 2019

🎉

@mom0tomo

This comment has been minimized.

Copy link

@mom0tomo mom0tomo commented Nov 3, 2019

You saved my day! Thanx!!

@iwan

This comment has been minimized.

Copy link

@iwan iwan commented Nov 7, 2019

Sooo good! Thanks!

@samuelpordeus

This comment has been minimized.

Copy link

@samuelpordeus samuelpordeus commented Nov 7, 2019

A true hero

@n41l

This comment has been minimized.

Copy link

@n41l n41l commented Nov 11, 2019

👍

@MahmoudAgamy

This comment has been minimized.

Copy link

@MahmoudAgamy MahmoudAgamy commented Nov 11, 2019

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

@channainfo

This comment has been minimized.

Copy link

@channainfo channainfo commented Nov 24, 2019

macOS Catalina also works.

@dorian-davis

This comment has been minimized.

Copy link

@dorian-davis dorian-davis commented Nov 25, 2019

Thank you! 🙏 💯

@amaxalov

This comment has been minimized.

Copy link

@amaxalov amaxalov commented Nov 27, 2019

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

@kringbo

This comment has been minimized.

Copy link

@kringbo kringbo commented Nov 28, 2019

Thanks a lot!

@hgimenez

This comment has been minimized.

Copy link

@hgimenez hgimenez commented Nov 29, 2019

Hey there! i'm having same issue on Mac os catalina. Tried all possible solutions reported here , still getting error after trying to do bundle.
Error

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

 /Users/hernangimenez/.rbenv/versions/2.1.4/bin/ruby extconf.rb --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... yes
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.18_1/lib
-----
creating Makefile

make "DESTDIR=" clean

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.18_1/include/mysql/mysql.h:187: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.18_1/include/mysql/mysql.h:187: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/hernangimenez/Desktop/proyectos/osdipp/osdipp/vendor/bundle/ruby/2.1.0/gems/mysql2-0.2.24 for inspection.
Results logged to /Users/hernangimenez/Desktop/proyectos/osdipp/osdipp/vendor/bundle/ruby/2.1.0/extensions/x86_64-darwin-19/2.1.0-static/mysql2-0.2.24/gem_make.out

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

In Gemfile:
  mysql2 

Then tried

Hernans-MacBook-Pro-2:osdipp hernangimenez$ gem install mysql2 -v '0.2.24' --source 'http://rubygems.org/'
ERROR:  While executing gem ... (Errno::EACCES)
    Permission denied @ rb_sysopen - /Users/hernangimenez/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/mysql2-0.2.24/MIT-LICENSE
Hernans-MacBook-Pro-2:osdipp hernangimenez$ 

any help?

@ashgaliyev

This comment has been minimized.

Copy link

@ashgaliyev ashgaliyev commented Dec 3, 2019

Thank you!!!!

@bdvineti

This comment has been minimized.

Copy link

@bdvineti bdvineti commented Dec 3, 2019

Thanks!
Slightly cuter to ask brew where openssl is: gem install mysql2 -- --with-opt-dir=$(brew --prefix openssl)

@polyden-29

This comment has been minimized.

Copy link

@polyden-29 polyden-29 commented Dec 29, 2019

thank you, this help me a lots

@channainfo

This comment has been minimized.

Copy link

@channainfo channainfo commented Dec 29, 2019

Just got a new macOS 16 inches Catalina from scratch. Build failed because I forgot to install MySQL. After issue brew install mysql the options offered above works 🙏 💯🙏 💯🙏 💯.

@bpang

This comment has been minimized.

Copy link

@bpang bpang commented Jan 7, 2020

Super helpful, thanks!

@thnery

This comment has been minimized.

Copy link

@thnery thnery commented Jan 10, 2020

If you were a Witcher, I'd definitely toss a coin to you. Thanks dude! This was very helpful.

@fernandoaleman

This comment has been minimized.

Copy link
Owner Author

@fernandoaleman fernandoaleman commented Jan 10, 2020

If you were a Witcher, I'd definitely toss a coin to you

@thnery HA, thanks and you're welcome. 😃

@edooan

This comment has been minimized.

Copy link

@edooan edooan commented Jan 16, 2020

This worked for me:

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

THX 👍

@amorozdna

This comment has been minimized.

Copy link

@amorozdna amorozdna commented Jan 28, 2020

When I installed mysql2 with ruby-2.3.8 I had to use both ldflags and cppflags opts:

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

However, when I migrate my app to ruby-2.6.5, the above is stopped to work. So, I had to change it by only --with-opt-dir opt:

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

or simpler:

bundle config build.mysql2 --with-opt-dir=$(brew --prefix openssl)
@pilgrim2go

This comment has been minimized.

Copy link

@pilgrim2go pilgrim2go commented Feb 22, 2020

Thanks

@llabake

This comment has been minimized.

Copy link

@llabake llabake commented Mar 1, 2020

Thanks!!!!

@SeanRoberts

This comment has been minimized.

Copy link

@SeanRoberts SeanRoberts commented Mar 2, 2020

or simpler:

bundle config build.mysql2 --with-opt-dir=$(brew --prefix openssl)

This was the winner for me on Catalina, thank you!

@nicholalexander

This comment has been minimized.

Copy link

@nicholalexander nicholalexander commented Mar 3, 2020

thanks!

@cameroncooke

This comment has been minimized.

Copy link

@cameroncooke cameroncooke commented Mar 4, 2020

or simpler:

bundle config build.mysql2 --with-opt-dir=$(brew --prefix openssl)

This was the winner for me on Catalina, thank you!

This was the only one that worked for me on Catalina!

@fuadkhan1996

This comment has been minimized.

Copy link

@fuadkhan1996 fuadkhan1996 commented Mar 9, 2020

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

Getting same issue

@brroong

This comment has been minimized.

Copy link

@brroong brroong commented Mar 23, 2020

Thank you!

@baraka2000

This comment has been minimized.

Copy link

@baraka2000 baraka2000 commented Mar 25, 2020

I guess this is still open? Having same issues as everyone above. @fuadkhan1996 Did you find a solution? I'm on a new mac with an ol rails app I need to maitain and this is preventing me... Newer 0.5 mysql2 installs fine. But have tried everything above and still get the same error you get... frustrating... And I know the sky is falling, but would greatly appreciate anyone who has an insight into this...

@davidbyrnes

This comment has been minimized.

Copy link

@davidbyrnes davidbyrnes commented Mar 26, 2020

Works. Thank you

@edwardsotelojr

This comment has been minimized.

Copy link

@edwardsotelojr edwardsotelojr commented Apr 10, 2020

Thank u!

@markthomasmiller

This comment has been minimized.

Copy link

@markthomasmiller markthomasmiller commented Apr 10, 2020

Thank you so much.

I also needed to run this:

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

And then:

bundle install

Saved my day! 😄

@gregblass

This comment has been minimized.

Copy link

@gregblass gregblass commented Apr 11, 2020

Thank you sir!

@axopadyani

This comment has been minimized.

Copy link

@axopadyani axopadyani commented Apr 14, 2020

Thank you! This problem wasted so much time.

@rgp

This comment has been minimized.

Copy link

@rgp rgp commented Apr 15, 2020

Worked on Catalina as well 10.15.4.
Thanks

@adonley

This comment has been minimized.

Copy link

@adonley adonley commented Apr 17, 2020

Thank you so much for saving me hours.

@nighttiger1990

This comment has been minimized.

Copy link

@nighttiger1990 nighttiger1990 commented May 5, 2020

Thank this work on macOS Mojave 10.14.5

@jedkaryd

This comment has been minimized.

Copy link

@jedkaryd jedkaryd commented May 12, 2020

Thank you so much! This worked for me on macOS Catalina 10.15.3

@zbeach

This comment has been minimized.

Copy link

@zbeach zbeach commented May 13, 2020

Thank you so much!!!

@dogweather

This comment has been minimized.

Copy link

@dogweather dogweather commented May 24, 2020

Every year, tracking down the same failures...

@AlexKVal

This comment has been minimized.

Copy link

@AlexKVal AlexKVal commented May 24, 2020

Thank you 👍

@eric-gymondo

This comment has been minimized.

Copy link

@eric-gymondo eric-gymondo commented Jun 11, 2020

Thank you, you made my day.

@zrahash

This comment has been minimized.

Copy link

@zrahash zrahash commented Jun 14, 2020

Thanks a lot ! it worked

@dlugo06

This comment has been minimized.

Copy link

@dlugo06 dlugo06 commented Jun 18, 2020

Bookmarking this has saved me multiple times. Thank you!

@lazy-ninja

This comment has been minimized.

Copy link

@lazy-ninja lazy-ninja commented Jun 19, 2020

thanks. I keep finding this page again and again

@VnMa

This comment has been minimized.

Copy link

@VnMa VnMa commented Jul 15, 2020

thanks a lot

@esambo

This comment has been minimized.

Copy link

@esambo esambo commented Jul 23, 2020

bundle config build.mysql2 --with-opt-dir=$(brew --prefix openssl)

fixed my ld: library not found for -lssl error, because homebrew only had openssl@1.1 installed

@alexeymorozov

This comment has been minimized.

Copy link

@alexeymorozov alexeymorozov commented Jul 31, 2020

bundle config build.mysql2 --with-opt-dir=$(brew --prefix openssl)

fixed my ld: library not found for -lssl error, because homebrew only had openssl@1.1 installed

👍

Thank you, E. Sambo! It works!

@joallard

This comment has been minimized.

Copy link

@joallard joallard commented Aug 5, 2020

@gee-forr thank you!

@Jaleelahmed94

This comment has been minimized.

Copy link

@Jaleelahmed94 Jaleelahmed94 commented Aug 8, 2020

Thank you!!

@redlightmikey

This comment has been minimized.

Copy link

@redlightmikey redlightmikey commented Aug 19, 2020

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

Getting same issue

I am having the same issue...

@jun-iida

This comment has been minimized.

Copy link

@jun-iida jun-iida commented Sep 6, 2020

Nice!!!

@nehagupta93

This comment has been minimized.

Copy link

@nehagupta93 nehagupta93 commented Oct 19, 2020

Life saver!

@daicoden

This comment has been minimized.

Copy link

@daicoden daicoden commented Dec 29, 2020

Still works using mysql2 0.5.3

@zahhar

This comment has been minimized.

Copy link

@zahhar zahhar commented Jan 13, 2021

As of today Homebrew defaults to MySQL 8.0, OpenSSL 1.1 and mysql2 0.5 that work great all together out of the box. However to upgrade of my legacy applications from Rails 3.2 to more recent versions I had to setup local environment on my MacOS BigSur 11.1.

My goal was to install mysql2 gem version 0.3.x (this is only one allowing smooth migration from Rails 3.2 to Rails 4.2 through versions 4.0 and 4.1) together with mySQL 5.x. Both of them in turn depend on OpenSSL 1.0

Here is what worked for me:

// Clean-up all your faulty attempts you did before
// Homebrew does not store openssl 1.0 anymore, so I had to find alternative — openssl1.0.2t 
wget https://github.com/tebelorg/Tump/releases/download/v1.0.0/openssl.rb
brew install openssl.rb

brew install mysql@5.7
brew link mysql@5.7 --force
brew install mysql-client@5.7
// NB! Pay close attention to path where you have mysql_config and openssl 1.0 installed
gem install mysql2 -v 0.3.21 -- --with-mysql-config=/usr/local/bin/mysql_config --with-ldflags=-L/usr/local/Cellar/openssl/1.0.2t/lib --with-cppflags=-I/usr/local/Cellar/openssl/1.0.2t/include

Voilá:

Building native extensions with: '--with-mysql-config=/usr/local/bin/mysql_config --with-ldflags=-L/usr/local/Cellar/openssl/1.0.2t/lib --with-cppflags=-I/usr/local/Cellar/openssl/1.0.2t/include'
This could take a while...
Successfully installed mysql2-0.3.21
Parsing documentation for mysql2-0.3.21
Installing ri documentation for mysql2-0.3.21
Done installing documentation for mysql2 after 0 seconds
1 gem installed
@hiep2fooder

This comment has been minimized.

Copy link

@hiep2fooder hiep2fooder commented Jan 17, 2021

As of today Homebrew defaults to MySQL 8.0, OpenSSL 1.1 and mysql2 0.5 that work great all together out of the box. However to upgrade of my legacy applications from Rails 3.2 to more recent versions I had to setup local environment on my MacOS BigSur 11.1.

My goal was to install mysql2 gem version 0.3.x (this is only one allowing smooth migration from Rails 3.2 to Rails 4.2 through versions 4.0 and 4.1) together with mySQL 5.x. Both of them in turn depend on OpenSSL 1.0

Here is what worked for me:

// Clean-up all your faulty attempts you did before
// Homebrew does not store openssl 1.0 anymore, so I had to find alternative — openssl1.0.2t 
wget https://github.com/tebelorg/Tump/releases/download/v1.0.0/openssl.rb
brew install openssl.rb

brew install mysql@5.7
brew link mysql@5.7 --force
brew install mysql-client@5.7
// NB! Pay close attention to path where you have mysql_config and openssl 1.0 installed
gem install mysql2 -v 0.3.21 -- --with-mysql-config=/usr/local/bin/mysql_config --with-ldflags=-L/usr/local/Cellar/openssl/1.0.2t/lib --with-cppflags=-I/usr/local/Cellar/openssl/1.0.2t/include

Voilá:

Building native extensions with: '--with-mysql-config=/usr/local/bin/mysql_config --with-ldflags=-L/usr/local/Cellar/openssl/1.0.2t/lib --with-cppflags=-I/usr/local/Cellar/openssl/1.0.2t/include'
This could take a while...
Successfully installed mysql2-0.3.21
Parsing documentation for mysql2-0.3.21
Installing ri documentation for mysql2-0.3.21
Done installing documentation for mysql2 after 0 seconds
1 gem installed

Thanks a lot Bro. Saved my day!

@weihangswtch

This comment has been minimized.

Copy link

@weihangswtch weihangswtch commented Feb 1, 2021

thanks, it works

@Willardgmoore

This comment has been minimized.

Copy link

@Willardgmoore Willardgmoore commented Feb 12, 2021

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

@mgoggin Thank you for this!! This is what I needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment