'Rails & MySQL on Apple Silicon

has anyone been able to get Rails running with the MySQL via the mysql2 gem on Apple Silicon? I'm working with Ruby 2.5.3 and Rails 5.2.3 but would love to hear of any successes with any versions. Currently I am stuck with the mysql2 gem install failing on:

linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

Thank you!



Solution 1:[1]

I've had success with just the following:

rbenv exec gem install mysql2 -- \
--with-mysql-lib=/opt/homebrew/Cellar/mysql/8.0.25_1/lib \
--with-mysql-dir=/opt/homebrew/Cellar/mysql/8.0.25_1 \
--with-mysql-config=/opt/homebrew/Cellar/mysql/8.0.25_1/bin/mysql_config \
--with-mysql-include=/opt/homebrew/Cellar/mysql/8.0.25_1/include 

(Note that you may need to change the version number in those paths)

Notably, this worked with the most recent version of mysql and does not require any Intel versions or using an emulated version of HomeBrew (e.g. ibrew).

Configure Bundler to use this build configuration automatically:

You may also want to set this configuration as your default for mysql2. This way anytime bundler has to re-install mysql2 (on this project or any other project on the same computer), it will automatically use this configuration. You can do that with the following:

bundle config set --global build.mysql2 \
--with-mysql-lib=/opt/homebrew/Cellar/mysql/8.0.25_1/lib \
--with-mysql-dir=/opt/homebrew/Cellar/mysql/8.0.25_1 \
--with-mysql-config=/opt/homebrew/Cellar/mysql/8.0.25_1/bin/mysql_config \
--with-mysql-include=/opt/homebrew/Cellar/mysql/8.0.25_1/include 

Solution 2:[2]

Based on @matthias-winkelmann's answer, use brew to find the correct version of mysql for you:

gem install mysql2 -v '0.5.3' -- \
--with-mysql-lib=$(brew --prefix mysql)/lib \
--with-mysql-dir=$(brew --prefix mysql) \
--with-mysql-config=$(brew --prefix mysql)/bin/mysql_config \
--with-mysql-include=$(brew --prefix mysql)/include 

Do notice the specific version of the mysql2 gem I'm referring to here.

Solution 3:[3]

I am using Montery12.1 with M1-14inc

Install env with brew

brew install openssl
brew install zstd

export env data

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
export LIBRARY_PATH=$LIBRARY_PATH:$(brew --prefix zstd)/lib/
export LIBRARY_PATH=$LIBRARY_PATH:$(brew --prefix [email protected])/lib/

Install mysql

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

I got error like that enter image description here I have tried with, it's work fine:

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

UPDATE

I found a simple and fast solution

arch -x86_64 gem install mysql2 -v 0.5.3 -- --srcdir=/usr/local/mysql/include

Refs: https://stackoverflow.com/a/71006010/11049888

Solution 4:[4]

After many attempts and much searching I have a working answer. First, you have to have the two brews installed on your M1 Mac- homebrew under arm and homebrew (ibrew) under emulated x86. See https://soffes.blog/homebrew-on-apple-silicon for this. Then, I was able to install [email protected] under emulation, as well as openssl. This is needed because mysql2 is compiling under emulation, it seems, and is referencing those libraries.

ibrew install [email protected]
ibrew install openssl

I then needed to add mysql and its libraries to my PATH.

 export PATH="/opt/homebrew/bin:/usr/local/bin:/opt/homebrew/opt/mysql:/opt/homebrew/opt/mysql/lib:/usr/local/Cellar//[email protected]//5.7.34:/usr/local/Cellar//[email protected]//5.7.34/lib:/usr/local/Cellar//[email protected]//5.7.34/bin:$PATH"

You might need to tweak this based on version numbers - and when you have it working put it in your ~/.zshrc or other shell initializer file.

Then I was able to install the mysql2 gem with these flags:

 gem install mysql2 -V -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include --with-opt-dir="$(ibrew --prefix openssl)"

I'm not a C coder so I don't totally get the details of how mysql2 is linking up to the mysql libraries. I would prefer to use the available mysql under ARM, expecting it must run faster, but not sure how to get it to link up.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joshua Pinter
Solution 2 mtrolle
Solution 3
Solution 4 Eskim0