Skip to content

Instantly share code, notes, and snippets.

@fernandoaleman
Last active April 22, 2024 23:15
Show Gist options
  • Star 53 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save fernandoaleman/385aad12a18fe50cf5fd1e988e76fd63 to your computer and use it in GitHub Desktop.
Save fernandoaleman/385aad12a18fe50cf5fd1e988e76fd63 to your computer and use it in GitHub Desktop.
How to install mysql2 gem on m1 Mac

Problem

Installing mysql2 gem errors on Apple silicon M1, M2 or M3 Mac running macOS Sonoma.

Solution

Make sure mysql-client, openssl and zstd are installed on Mac via Homebrew.

Replace mysql-client with whichever mysql package you are using

brew install mysql-client openssl zstd

Install mysql2 gem.

Replace 0.5.2 with whichever version you are using

 gem install mysql2 -v '0.5.2' -- --with-mysql-lib=$(brew --prefix mysql-client)/lib --with-mysql-dir=$(brew --prefix mysql-client) --with-mysql-config=$(brew --prefix mysql-client)/bin/mysql_config --with-mysql-include=$(brew --prefix mysql-client)/include --with-ldflags="-L$(brew --prefix zstd)/lib -L$(brew --prefix openssl)/lib -L$(brew --prefix zlib)/lib" --with-cppflags="-I$(brew --prefix openssl)/include -I$(brew --prefix zlib)/include"
@chmich
Copy link

chmich commented Mar 10, 2023

Solved my issue. First i had the same error like mentioned at the top of this issue, and then «Error symbol not found in flat namespace '_mysql_affected_rows'». My Mistake was that i transferred my user, including all the rvm staff from my intel mac to my M2.

So i reset my mac, installed everything new from ground up, including MacOs, brew, rvm mysql, AND: ruby. The last was the most critical for my feeling.

After that i didn't need any additional configs, like mentioned at the top of this isse, just ran gem install mysql2, or just bundle without any specific configs and it all runs fine.

My Ruby Projects are from version 2.7.5 until 3.1.2, mysql2 is -v 0.5.5 and a mariadb 10.11.2

@onyxraven
Copy link

if you only installed brew mysql-client --- use that for your prefix

gem install mysql2 -v '0.5.3' -- --with-mysql-config=$(brew --prefix mysql-client)/bin/mysql_config --with-ldflags="-L$(brew --prefix zstd)/lib -L$(brew --prefix openssl)/lib" --with-cppflags=-I$(brew --prefix openssl)/include

@andreyd22
Copy link

thank you! its workl

@AlexanderFisenko
Copy link

thank you!

@onyxraven
Copy link

I've had to recently expand this because of the latest mysql brew packages. This incantation worked for me:

gem install mysql2 -v '0.5.3' -- --with-mysql-lib=$(brew --prefix mysql-client)/lib --with-mysql-dir=$(brew --prefix mysql-client) --with-mysql-config=$(brew --prefix mysql-client)/bin/mysql_config --with-mysql-include=$(brew --prefix mysql-client)/include --with-ldflags="-L$(brew --prefix zstd)/lib -L$(brew --prefix openssl)/lib -L$(brew --prefix zlib)/lib" --with-cppflags="-I$(brew --prefix openssl)/include -I$(brew --prefix zlib)/include"

(replace mysql-client with mysql if you've installed that instead)

@andreyd22
Copy link

for new version mysql2 just change -v '0.5.5' for example

@jess
Copy link

jess commented Jun 13, 2023

   \o/
    |
   / \

@jthomp
Copy link

jthomp commented Jun 13, 2023

Exactly what I needed. Thanks!

@PabloScolpino
Copy link

Thanks a lot.

@tnoworyta
Copy link

Thank you!

@bjvta
Copy link

bjvta commented Jul 11, 2023

Thank you! It worked!

@Nalymoslih
Copy link

ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory
what's problem?

@fernandoaleman
Copy link
Author

gem install mysql2 -v '0.5.3' -- --with-mysql-lib=$(brew --prefix mysql-client)/lib --with-mysql-dir=$(brew --prefix mysql-client) --with-mysql-config=$(brew --prefix mysql-client)/bin/mysql_config --with-mysql-include=$(brew --prefix mysql-client)/include --with-ldflags="-L$(brew --prefix zstd)/lib -L$(brew --prefix openssl)/lib -L$(brew --prefix zlib)/lib" --with-cppflags="-I$(brew --prefix openssl)/include -I$(brew --prefix zlib)/include"

Thanks @onyxraven, this worked for me while trying to install mysql2 on macOS Sonoma. 👍

@dariodaich
Copy link

@fernandoaleman thank you so, so much for this!

@fernandoaleman
Copy link
Author

@fernandoaleman thank you so, so much for this!

@dariodaitch You're welcome. I'm glad it was helpful. 👍

@johnivanoff
Copy link

Thank you. it worked. :-)

@BigHHu
Copy link

BigHHu commented Dec 22, 2023

thanks!

@jonahgeorge
Copy link

Small update from the instructions above: brew install mysql@5.7 as brew install mysql now defaults to the 8.X series

@llothar
Copy link

llothar commented Feb 5, 2024

And now this doesn't work in Feb 2024 anymore with the latest XCode.

client.c:178:24: error: call to undeclared function 'rb_tainted_str_new2'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] VALUE rb_sql_state = rb_tainted_str_new2(mysql_sqlstate(wrapper->client));

But we just need one more argument

gem install mysql2 -v '0.5.3' -- --with-mysql-config=$(brew --prefix mysql-client)/bin/mysql_config --with-ldflags="-L$(brew --prefix zstd)/lib -L$(brew --prefix openssl)/lib" --with-cppflags=-I$(brew --prefix openssl)/include --with-cflags="-Wno-error=implicit-function-declaration"

But this will not find the new homebrew location, so remove the version fixing and do

gem install mysql2 -- --with-mysql-config=$(brew --prefix mysql-client)/bin/mysql_config --with-ldflags="-L$(brew --prefix zstd)/lib -L$(brew --prefix openssl)/lib" --with-cppflags=-I$(brew --prefix openssl)/include --with-cflags="-Wno-error=implicit-function-declaration"

@rajanverma-me
Copy link

rajanverma-me commented Feb 6, 2024

And now this doesn't work in Feb 2024 anymore with the latest XCode.

client.c:178:24: error: call to undeclared function 'rb_tainted_str_new2'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] VALUE rb_sql_state = rb_tainted_str_new2(mysql_sqlstate(wrapper->client));

But we just need one more argument

gem install mysql2 -v '0.5.3' -- --with-mysql-config=$(brew --prefix mysql-client)/bin/mysql_config --with-ldflags="-L$(brew --prefix zstd)/lib -L$(brew --prefix openssl)/lib" --with-cppflags=-I$(brew --prefix openssl)/include --with-cflags="-Wno-error=implicit-function-declaration"

But this will not find the new homebrew location, so remove the version fixing and do

gem install mysql2 -- --with-mysql-config=$(brew --prefix mysql-client)/bin/mysql_config --with-ldflags="-L$(brew --prefix zstd)/lib -L$(brew --prefix openssl)/lib" --with-cppflags=-I$(brew --prefix openssl)/include --with-cflags="-Wno-error=implicit-function-declaration"

Last command worked for me Mac Sonoma 14.2.1

gem install mysql2 -- --with-mysql-config=$(brew --prefix mysql)/bin/mysql_config --with-ldflags="-L$(brew --prefix zstd)/lib -L$(brew --prefix openssl)/lib" --with-cppflags=-I$(brew --prefix openssl)/include --with-cflags="-Wno-error=implicit-function-declaration"

@timokleemann
Copy link

timokleemann commented Feb 7, 2024

Thanks, @llothar, this is the only command that worked for me in February 2024 on Mac OS Sonoma for the M1.

@navin-onetote
Copy link

@llothar's last command worked for me as well on my M1. Thanks a ton - have been beating myself up for the past 24 hours!

@fernandoaleman
Copy link
Author

@llothar, @cereberaldebris, @timokleemann, @rajanverma-me I just updated this gist with the command that is working for me as of today February 08, 2024. Would any one of you mind testing it to confirm it is also working for you?

@johnivanoff
Copy link

This worked for me on Mac Sonoma 14.2.1

brew install mysql-client openssl zstd
gem install mysql2  -- --with-mysql-config=$(brew --prefix mysql)/bin/mysql_config --with-ldflags="-L$(brew --prefix zstd)/lib -L$(brew --prefix openssl)/lib" --with-cppflags=-I$(brew --prefix openssl)/include --with-cflags="-Wno-error=implicit-function-declaration"

@trevorh
Copy link

trevorh commented Feb 10, 2024

This worked for me on Mac Sonoma 14.2.1

brew install mysql-client openssl zstd
gem install mysql2  -- --with-mysql-config=$(brew --prefix mysql)/bin/mysql_config --with-ldflags="-L$(brew --prefix zstd)/lib -L$(brew --prefix openssl)/lib" --with-cppflags=-I$(brew --prefix openssl)/include --with-cflags="-Wno-error=implicit-function-declaration"

This worked perfectly, even with using rbenv. Thanks!

@caroline-medeiros
Copy link

for sonoma 14.3.1, anyone???

@fernandoaleman
Copy link
Author

@caroline-medeiros I am running this on the latest macOS Sonoma and it is working.

@caroline-medeiros
Copy link

I needed to add this cflags --with-cflags="-Wno-error=implicit-function-declaration" thanks!

@haanhduclinh
Copy link

Thanks. your solution is work. I try many before but it doesn't work

@naoyanickf
Copy link

@trevorh you are awesome

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