Rails & MySQL on Apple Silicon
Asked Answered
T

4

13

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!

Tangleberry answered 21/5, 2021 at 6:4 Comment(0)
R
20

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 
Rebellion answered 6/6, 2021 at 1:10 Comment(1)
This is the one. Change 8.0.25_1 to 8.0.26 for the specific version of msyql we have installed. But the rest worked great. ProTip: I would actually configure Bundler to use this globally so that in the event that you need to reinstall the gem, it will use this configuration automatically. I've modified your answer with this approach.Superaltar
P
19

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.

Predecessor answered 1/12, 2021 at 14:36 Comment(1)
This worked for me on a brand new M1 Pro mac! Fresh Homebrew install. brew install rbenv. rbenv install 3.1.0. brew install mysql. brew install openssl. brew install zstd. and then the above :-) and then the usual monkeying around to makeEisteddfod
M
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://mcmap.net/q/341949/-an-error-occurred-while-installing-mysql2-0-4-8-and-bundler-cannot-continue

Mcgrew answered 17/1, 2022 at 16:54 Comment(1)
This did it for me, cheersEmiliaemiliaromagna
T
0

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.

Tangleberry answered 24/5, 2021 at 16:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.