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

6

45

while running bundle install

An error occurred while installing mysql2 (0.5.3), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'` succeeds before bundling.

Rails app 6.1.3

Bundler version 2.2.19

macOS Big Sur 11.4

Apple M1

in the Gemfile

ruby '2.6.6'
gem 'rails', '~> 6.1.3', '>= 6.1.3.1'
gem 'mysql2'

in the Gemfile.lock mysql is at 0.5.3

when I run bundle install it crashes with the normal:

enter image description here

Examining the build error, which appears earlier in the output, the build error appears to be: (full results below)

 ld: library not found for -lzstd

The full results from bundle install are

jason@MacBook-Air  % bundle install    
Fetching gem metadata from https://rubygems.org/
Fetching gem metadata from https://rubygems.org/........
Using rake 13.0.3
Using concurrent-ruby 1.1.8
Using rack 2.2.3
Using nio4r 2.5.7
Using builder 3.2.4
Using marcel 1.0.1
Using mini_portile2 2.5.1
Using multi_json 1.15.0
Using method_source 1.0.0
Using crass 1.0.6
Using thor 1.1.0
Using public_suffix 4.0.6
Using execjs 2.7.0
Using aws-eventstream 1.1.1
Using aws-partitions 1.447.0
Using jmespath 1.4.0
Using bcrypt 3.1.16
Using betterlorem 0.1.2
Using bindex 0.8.1
Using msgpack 1.4.2
Using popper_js 1.16.0
Using ffi 1.15.0
Using tilt 2.0.10
Using bundler 2.2.19
Using byebug 11.1.3
Using regexp_parser 2.1.1
Using childprocess 3.0.0
Using connection_pool 2.2.5
Using rexml 3.2.5
Using orm_adapter 0.5.0
Using diff-lcs 1.4.4
Using unf_ext 0.0.7.7
Using docile 1.3.5
Using e2mmap 0.1.0
Using erubis 2.7.0
Using ffaker 2.18.0
Using finishing_moves 1.0.1
Using raabro 1.4.0
Using temple 0.8.2
Using sexp_processor 4.15.2
Using hashdiff 1.0.1
Using hiredis 0.6.3
Using honeybadger 4.8.0
Using kaminari-core 1.2.1
Using rb-fsevent 0.10.4
Using ruby_dep 1.5.0
Using ruby2_keywords 0.0.4
Fetching mysql2 0.5.3
Using minitest 5.14.4
Using zeitwerk 2.4.2
Using parslet 1.8.2
Using puma 3.12.6
Using rails_http_options 0.1.0
Using redis 4.2.5
Using rubyzip 2.3.0
Using semantic_range 3.0.0
Using rspec-support 3.10.2
Using simple-random 1.0.3
Using simplecov-html 0.12.3
Using simplecov_json_formatter 0.1.2
Using spring 2.1.1
Using sqlite3 1.4.2
Using stripe 5.32.1
Using vcr 6.0.0
Using i18n 1.8.10
Using tzinfo 2.0.4
Using rack-test 1.1.0
Using sprockets 3.7.2
Using warden 1.2.9
Using rack-attack 6.5.0
Using rack-cors 1.1.1
Using rack-protection 2.1.0
Using rack-proxy 0.6.5
Using sitemap_generator 6.1.2
Using addressable 2.7.0
Using autoprefixer-rails 10.2.4.0
Using aws-sigv4 1.2.3
Using bootsnap 1.7.4
Using sassc 2.4.0
Using crack 0.4.5
Using unf 0.1.4
Using haml 5.2.1
Using ruby_parser 3.15.1
Using rb-inotify 0.10.1
Using mustermann 1.1.1
Using thwait 0.2.0
Using sidekiq 6.2.1
Using websocket-extensions 0.1.5
Using activesupport 6.1.3.2
Using aws-sdk-core 3.114.0
Using simpleidn 0.2.1
Using et-orbi 1.2.4
Using faker 2.17.0
Using sass-listen 4.0.0
Using launchy 2.5.0
Using listen 3.1.5
Using rspec-core 3.10.1
Using rspec-expectations 3.10.1
Using rspec-mocks 3.10.2
Using selenium-webdriver 3.142.7
Using simplecov 0.21.2
Using sinatra 2.1.0
Using webmock 3.12.2
Using erubi 1.10.0
Using mini_mime 1.0.3
Using racc 1.5.2
Using ruby_http_client 3.5.2
Using nokogiri 1.11.3
Using websocket-driver 0.7.3
Using globalid 0.4.2
Using activemodel 6.1.3.2
Using mail 2.7.1
Using aws-sdk-kms 1.43.0
Using dnsruby 1.61.5
Using factory_bot 6.1.0
Using fugit 1.4.5
Using sass 3.7.4
Using jbuilder 2.11.2
Using rspec 3.10.0
Using sendgrid-ruby 6.4.0
Using shoulda-matchers 4.5.1
Using simplecov-rcov 0.2.3
Using split 3.4.1
Using spring-watcher-listen 2.0.1
Using rails-dom-testing 2.0.3
Using loofah 2.9.1
Using activejob 6.1.3.2
Using activerecord 6.1.3.2
Using aws-sdk-s3 1.93.1
Using xpath 3.2.0
Using html2haml 2.2.0
Using roo 2.8.3
Using rspec-wait 0.0.9
Using rufus-scheduler 3.7.0
Using webdrivers 4.6.0
Using rails-html-sanitizer 1.3.0
Using sidekiq-scheduler 3.0.1
Using capybara 3.35.3
Using counter_culture 2.8.0
Using actionview 6.1.3.2
Using kaminari-activerecord 1.2.1
Using actionpack 6.1.3.2
Using kaminari-actionview 1.2.1
Using nonschema_migrations 5.0.2
Using actionmailer 6.1.3.2
Using railties 6.1.3.2
Using sprockets-rails 3.2.2
Using kaminari 1.2.1
Using activestorage 6.1.3.2
Using activerecord-session_store 2.0.0
Using font_awesome5_rails 1.4.0
Using actioncable 6.1.3.2
Using haml-rails 2.0.1
Using sass-rails 5.1.0
Using jquery-rails 4.4.0
Using rspec-rails 5.0.1
Using web-console 4.1.0
Using webpacker 5.2.1
Using actionmailbox 6.1.3.2
Using actiontext 6.1.3.2
Using sassc-rails 2.1.2
Using responders 3.0.1
Using bootstrap 4.4.1
Using rails 6.1.3.2
Using factory_bot_rails 6.1.0
Using devise 4.7.3
Using turbo-rails 0.5.9 from https://github.com/hotwired/turbo-rails.git (at main@2945dcb)
Using stimulus-rails 0.2.3
Using stripe-rails 2.3.0
Using universal-track-manager 0.6.1.alpha
Using paranoia 2.4.3
Using spf-query 0.1.5 from https://github.com/trailofbits/spf-query.git (at master@6dac502)
Installing mysql2 0.5.3 with native extensions
Using hot-glue 0.1.1
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/jason/.rvm/gems/ruby-2.6.6/gems/mysql2-0.5.3/ext/mysql2
/Users/jason/.rvm/rubies/ruby-2.6.6/bin/ruby -I /Users/jason/.rvm/rubies/ruby-2.6.6/lib/ruby/site_ruby/2.6.0 -r
./siteconf20210604-8580-1kmtwzs.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.25_1/lib
-----
creating Makefile

current directory: /Users/jason/.rvm/gems/ruby-2.6.6/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/jason/.rvm/gems/ruby-2.6.6/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/jason/.rvm/gems/ruby-2.6.6/gems/mysql2-0.5.3 for inspection.
Results logged to /Users/jason/.rvm/gems/ruby-2.6.6/extensions/-darwin-20/2.6.0/mysql2-0.5.3/gem_make.out

An error occurred while installing mysql2 (0.5.3), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  mysql2
jason@MacBook-Air  % 

I have already successfully run brew install mysql ... if I run it again it tells me:

Warning: mysql 8.0.25_1 is already installed and up-to-date. To reinstall 8.0.25_1, run: brew reinstall mysql

Remediable answered 4/6, 2021 at 16:18 Comment(0)
A
93

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)

Also note, 0.5.3 is no longer the latest gem version for mysql2, find the latest version here:

https://rubygems.org/gems/mysql2

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

Angeli answered 27/6, 2021 at 19:1 Comment(10)
Thanks! This worked for me on an M1 Mac with Big Sur!Assort
Finally! After half-day of pulling my hair out, this worked for my M1 Big Sur. Thank you so much!Fico
Thanks. This worked for me on Mac OS Monterey and a MacBook Pro with M1.Pavlodar
Bundler is at fault again... This have finally worked for me. Unbelievable, I wouldn't have thought. So much time wasted... Mac Big Sur IntelHyksos
Works M1 Mac, Monterey! One thing: brew --prefix openssl returns openssl@3 for me, but I have [email protected] in homebrew/opt, so it errors with not found. For those having same issue, either brew install openssl to get the newer version or directly point to the version you have, in my case /opt/homebrew/opt/[email protected]/.Thermel
Rather than pointing at a specific version with --with-ldflags=-L/opt/homebrew/Cellar/zstd/1.5.0/lib use the symlinked dir which will point at whatever version you have: --with-ldflags=-L/opt/homebrew/opt/zstd/libEyeshade
On my machine, /opt/homebrew/opt/zstd/lib was not the correct path, but --with-ldflags=-L$(brew --prefix zstd)/lib worked.Maggio
running > echo gem install mysql2 -v '0.5.3' -- --with-opt-dir=(brew --prefix openssl) --with-ldflags=-L(brew --prefix zstd)/lib gives gem install mysql2 -v 0.5.3 -- --with-opt-dir=/opt/homebrew/opt/openssl@3 --with-ldflags=-L/opt/homebrew/opt/zstd/lib and runnign the command doesn't work, it errors outAslam
I get error: call to undeclared function 'rb_tainted_str_new2'; ISO C99 and later do not support implicit function declarationsAslam
bundle config build.mysql2 "--with-opt-dir=$(brew --prefix openssl) --with-ldflags=-L/opt/homebrew/opt/zstd/lib" worked for me (M2 Mac)Marshmallow
R
46

Got the same error when try to install mysql2 -v 0.4.1 on M1
--SOLVED--

brew install zstd

which zstd                                                                                             

my path is -> /opt/homebrew/bin/zstd
you need to check where zstd linked from

cd /opt/homebrew/bin
ls -la

zstd -> ../Cellar/zstd/1.5.0/bin/zstd
so you need get path to zstd lib, my is:

/opt/homebrew/Cellar/zstd/1.5.0/lib

And then do classic combination with some magic

bundle config --local build.mysql2 "--with-ldflags=-L/opt/homebrew/Cellar/zstd/1.5.0/lib"
bundle install

gem list my                                                                                                 

*** LOCAL GEMS ***

mysql2 (0.4.10)
Ribbonfish answered 7/6, 2021 at 19:36 Comment(3)
It worked just fine! Thank you so much!Sachsen
this is a good example showing how to set this globally in homebrew; the accepted answer above should have a thorough walk through of all the steps and shows how to get it working with just gem install first, then once you confirm that works make the change globally to your bundler fileRemediable
Thanks a bunch. it was starting to get on my nerves😓Armored
M
20

This is what worked for me on the M1.

First make sure you have mysql installed and openssl with Homebrew

brew install mysql
brew install openssl

Then set these values for the install of mysql2 manually. My path for zstd was the same as Octocats, so be sure to follow their instructions and change the --with-ldflags option appropriately.

bundle config --local build.mysql2 "--with-opt-dir="$(brew --prefix openssl) --with-ldflags=-L/opt/homebrew/Cellar/zstd/1.5.0/lib""
Metaphor answered 21/6, 2021 at 11:46 Comment(2)
does someone know the alternative for pip?Nic
This is the only solution that worked for me on the Apple M1Pavlodar
J
17

This happened to me using MacOS 11.6 (Big Sur) on an M1 chip.

Try

brew install mysql

then

gem install mysql2 -- --with-opt-dir="$(brew --prefix zstd)"

which worked for me.

If that works, then use this to configure Bundler to do it for you (in this directory, i.e. "local"):

bundle config --local build.mysql2 "--with-opt-dir="$(brew --prefix zstd)""

Then bundle install should work.

Jehiah answered 26/10, 2021 at 11:10 Comment(3)
this answer worked for me w/ openssl, e.g.: gem install mysql2 -- --with-opt-dir="$(brew --prefix openssl)" but for some reason I can't get it to work when I use the bundle config command to save the settingHaller
@Haller are you using bundle install after you set the config for Bundler? gem install doesn't know anything about Bundler config. You can check what config is set using bundle config list in the directory containing your application's Gemfile.Jehiah
that's a good point, I think I was in a rabbit hole trying to get that command to pass on its ownHaller
A
2
bundle config --global build.mysql2 "--with-ldflags=-L/opt/homebrew/opt/zstd/lib"
bundle install

As a solution to other problems as well, I have a ~/.bundle/config with:

---
BUNDLE_JOBS: "9"
BUNDLE_BUILD__SQLITE3: "--with-cflags=-Wno-implicit-function-declaration"
BUNDLE_BUILD__FFI: "--with-cflags=-Wno-implicit-function-declaration"
BUNDLE_BUILD__MYSQL2: "--with-ldflags=-L/opt/homebrew/opt/zstd/lib"

And make sure you don't have anything in your local repo in .bundle/config

Aslam answered 11/1, 2024 at 12:1 Comment(0)
C
0

On ruby 3.3.0 I configured it as follows:

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

I had to set-up both --with-opt-dir and --with-ldflags and it worked.

Coenzyme answered 31/12, 2023 at 3:5 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.