Skip to content

Instantly share code, notes, and snippets.

@fernandoaleman
Last active February 7, 2024 19:19
Show Gist options
  • Save fernandoaleman/ee3ac6957c2ba4f7d7d33a251d58b191 to your computer and use it in GitHub Desktop.
Save fernandoaleman/ee3ac6957c2ba4f7d7d33a251d58b191 to your computer and use it in GitHub Desktop.
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
@zahhar
Copy link

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
Copy link

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
Copy link

thanks, it works

@Willardgmoore
Copy link

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.

@ShadSterling
Copy link

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:

This is the key line in the error:

/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

Note the extra backslashes; it's escaping the string in the bundle config so it's passed to extconf.rb as a single parameter, which is silently ignored.

In my case it was also enough to only specify the lib dir. I'm guessing the --with-opt-dir in the newer comments is a way to set both with a single option in cases where the include dir also needs to be set. It would be better if bundle had a way to specify multiple parameters.

I did this with bundle config --local build.mysql2 --with-ldflags=-L/usr/local/opt/openssl/lib, including --local so it would be saved in the repo so future developers don't have to hunt this down like I did, but we'll see if that breaks our CICD.

@enowmbi
Copy link

enowmbi commented Mar 26, 2021

Great it works 👍

@jbaranski
Copy link

👍

@KimGemRuby
Copy link

this true solution is brew install mysql

@denemorhun
Copy link

This issue persist in Big Sur:

urrent directory: /Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: warning: directory not found for option '-L/usr/local/opt/openssl/lib'
ld: library not found for -lzstd
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3 for inspection.
Results logged to /Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/extensions/-darwin-20/2.6.0/mysql2-0.5.3/gem_make.out
bash-3.2$ gem install mysql2 -v '0.5.3' -- --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

Building native extensions with: '--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'
This could take a while...
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.

current directory: /Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2

/Users/dorhun/.rbenv/versions/2.6.3/bin/ruby -I /Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/2.6.0 -r ./siteconf20210624-12722-nl7ij9.rb extconf.rb --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
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes

Cannot find mysql_config at /usr/local/opt/mysql-client/bin/mysql_config

*** 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/dorhun/.rbenv/versions/2.6.3/bin/$(RUBY_BASE_NAME)
--with-mysql-dir
--without-mysql-dir
--with-mysql-include
--without-mysql-include=${mysql-dir}/include
--with-mysql-lib
--without-mysql-lib=${mysql-dir}/lib
--with-mysql-config

To see why this extension failed to compile, please check the mkmf.log which can be found here:

/Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/extensions/-darwin-20/2.6.0/mysql2-0.5.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3 for inspection.
Results logged to /Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/extensions/-darwin-20/2.6.0/mysql2-0.5.3/gem_make.out

@jmgarnier
Copy link

On Apple silicon M1:

gem install mysql2 -v '0.5.3' -- --with-opt-dir=$(brew --prefix openssl)

@shireeshj
Copy link

On Apple silicon M1:

gem install mysql2 -v '0.5.3' -- --with-opt-dir=$(brew --prefix openssl)

The above did not work for me on Apple Silicon M1.
However, this worked:

gem install mysql2 -- --srcdir=/usr/local/mysql/include

Or with bundler

bundle config build.mysql2 --srcdir=/usr/local/mysql/include
bundle install

@jmgarnier
Copy link

@shireeshj you are not using Homebrew?

@shireeshj
Copy link

shireeshj commented Jul 23, 2021

@shireeshj you are not using Homebrew?

Using homebrew to install mysql.

@shobhitchittora
Copy link

bundle config build.mysql2 --srcdir=/usr/local/mysql/include
bundle install

This worked for me. Thanks all.

@gukandrew
Copy link

gukandrew commented Jul 30, 2021

Alternative way is to install using Brew's Cellar Openssl. Tested on MacOS Big Sur with brew:

gem install mysql2 -v '0.4.10' -- --with-cflags=\"-I$(brew --cellar openssl@1.1)/1.1.1k/include\" --with-ldflags=\"-L$(brew --cellar openssl@1.1)/1.1.1k/lib\"

@ARSLANALI2121
Copy link

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

Thanks

@ssagli
Copy link

ssagli commented Sep 21, 2021

This issue persist in Big Sur:

urrent directory: /Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: warning: directory not found for option '-L/usr/local/opt/openssl/lib'
ld: library not found for -lzstd
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3 for inspection.
Results logged to /Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/extensions/-darwin-20/2.6.0/mysql2-0.5.3/gem_make.out
bash-3.2$ gem install mysql2 -v '0.5.3' -- --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

Building native extensions with: '--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'
This could take a while...
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.

current directory: /Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2

/Users/dorhun/.rbenv/versions/2.6.3/bin/ruby -I /Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/2.6.0 -r ./siteconf20210624-12722-nl7ij9.rb extconf.rb --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

checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes

Cannot find mysql_config at /usr/local/opt/mysql-client/bin/mysql_config

*** 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/dorhun/.rbenv/versions/2.6.3/bin/$(RUBY_BASE_NAME)
--with-mysql-dir
--without-mysql-dir
--with-mysql-include
--without-mysql-include=${mysql-dir}/include
--with-mysql-lib
--without-mysql-lib=${mysql-dir}/lib
--with-mysql-config

To see why this extension failed to compile, please check the mkmf.log which can be found here:

/Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/extensions/-darwin-20/2.6.0/mysql2-0.5.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3 for inspection.
Results logged to /Users/dorhun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/extensions/-darwin-20/2.6.0/mysql2-0.5.3/gem_make.out

I had the same issue on Mac Big Sur 11.6. after running these commands it worked:
brew install openssl
brew install mysql
then
gem install mysql2 -v '0.5.3' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include

@EnglonKavhuru
Copy link

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

I am getting the same error when I try to run the command bundle install, how did you managed to solve it?

@bhargavaruby
Copy link

gem install mysql2  
                                           
Building native extensions. This could take a while...
ERROR:  Error installing mysql2:
	ERROR: Failed to build gem native extension.

    current directory: /Users/bhargavap/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
/Users/bhargavap/.rbenv/versions/2.6.6/bin/ruby -I /Users/bhargavap/.rbenv/versions/2.6.6/lib/ruby/2.6.0 -r ./siteconf20211109-84053-5ctkl1.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
-----
Using mysql_config at /opt/homebrew/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in mysql.h... yes
checking for MYSQL.net.vio in mysql.h... yes
checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes
checking for my_bool in mysql.h... no
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /opt/homebrew/Cellar/mysql/8.0.27/lib
-----
creating Makefile

current directory: /Users/bhargavap/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/bhargavap/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lzstd
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/bhargavap/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3 for inspection.

I am getting the error 'library not found for -lzstd' can someone help me.

@gukandrew
Copy link

@bhargavaruby try this:

brew install openssl zstd
gem install mysql2 -v '0.5.3' -- --with-opt-dir="$(brew --prefix openssl@1.1)" --with-ldflags=\"-L$(brew --cellar zstd)/1.5.0/lib\"

@bhargavaruby
Copy link

thanks @gukandrew
it's working...

@khoerodin
Copy link

Thanks @gukandrew 🥳

@leonmezu1
Copy link

Thanks worked like a charm @gukandrew

@iamajvillalobos
Copy link

bundle config build.mysql2 --srcdir=/usr/local/mysql/include
bundle install

This worked for me on MacOS Monterey w/ Macbook Air M1 2020

@pbredenberg
Copy link

@gukandrew confirmed working for me on my MBA M1 2020

@Klekum
Copy link

Klekum commented Jan 21, 2022

In case none of the solutions above worked for you on an Apple M1 silicon, you may want to have a look at this one:
brianmario/mysql2#1175 (comment)

@bparanj
Copy link

bparanj commented Feb 21, 2022

This:

bundle config build.mysql2 --srcdir=/usr/local/mysql/include
bundle install

worked for me on Monterey 12.2.1 with M1 chip

@dblanken
Copy link

Thank you @bparanj. That worked perfectly for me as well.

@DavidAmyot
Copy link

Tried all of the solutions from multiple sources/websites and the only one that worked on a M2 2023 was gem install mysql2 -v '0.3.18' -- --srcdir=/opt/homebrew/Cellar/mysql/8.0.33_2/include. Other solutions were working for more recent versions of the mysql2 gem though.

@fcastaneitron
Copy link

Tried all of the solutions from multiple sources/websites and the only one that worked on a M2 2023 was gem install mysql2 -v '0.3.18' -- --srcdir=/opt/homebrew/Cellar/mysql/8.0.33_2/include. Other solutions were working for more recent versions of the mysql2 gem though.

Awesome!!! This was the only solution that worked for me. You saved my day!

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