ld: library not found for -lzstd while bundle install for mysql2 gem Ruby on macOS Big Sur 11.4 Apple M1

Step 1:

confirm you have both openssl and MySQL installed via brew

brew install mysql

brew install openssl

or if you have previous version of openssl try

brew reinstall openssl@3

Step 2:

Run this on your Rails app to make sure you can get through bundler:

gem install mysql2 -v '0.5.3' -- --with-opt-dir=$(brew --prefix openssl) --with-ldflags=-L/opt/homebrew/opt/zstd/lib

two things to note: $(brew --prefix openssl) dynamically replaces the stuff inside the evaluation $(...) to the directory of openssl on your machine.

/opt/homebrew/opt/zstd is really a symlink to the current version of zstd on your machine (which links to something like /opt/homebrew/opt/Cellar/zstd/1.5.2)

Step 3:

If you want this globally, run this:

bundle config --global build.mysql2 "--with-opt-dir=$(brew --prefix openssl) --with-ldflags=-L/opt/homebrew/opt/zstd/lib"

note that this will change your ~/.bundler/config file and then applies globally to bundler

Leave a Comment