macOS Ventura causes failure to install 'mysql2' due to 'ld: library not found for -lzlib'
P粉156415696
P粉156415696 2023-11-11 08:40:10
0
2
914

Unable to install mysql2 using bundle install. I ask this because some websites and blogs have shown how to solve problems caused by ssl, zstd, but I can't find a solution caused by zlib plan. Has anyone else encountered the same problem?

BTW, I'm running bundle install locally, not on docker.

  • Operating System: Ventura(13.3.1)
  • Xcode: 14.3
  • Ruby:3.1.4
  • Guide rail: 6.1
  • mysql2:0.5.5

Has been completed:

  • brew install mysql, openssl@3, zstd, zlib

This is the log output.

Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
Using rake 13.0.6
Using concurrent-ruby 1.2.2
Using minitest 5.18.0
Using zeitwerk 2.6.8
Using websocket-extensions 0.1.5
Using erubi 1.12.0
Using racc 1.6.2
Using date 3.3.3
Using rack 2.2.7
Using bindex 0.8.1
Using bundler 2.3.26
Using marcel 1.0.2
Using mini_mime 1.1.2
Using timeout 0.3.2
Using rb-fsevent 0.11.2
Using crass 1.0.6
Using msgpack 1.7.0
Using builder 3.2.4
Using byebug 11.1.3
Using semantic_range 3.0.0
Using method_source 1.0.0
Using nio4r 2.5.9
Using spring 4.1.1
Using turbolinks-source 5.2.0
Using thor 1.2.1
Using i18n 1.13.0
Using nokogiri 1.14.3 (arm64-darwin)
Using rack-test 2.1.0
Using ffi 1.15.5
Using websocket-driver 0.7.5
Using tzinfo 2.0.6
Using sprockets 4.2.0
Using net-protocol 0.2.1
Using rack-proxy 0.7.6
Using bootsnap 1.16.0
Using puma 5.6.5
Using tilt 2.1.0
Using turbolinks 5.2.1
Using rack-mini-profiler 2.3.4
Using loofah 2.20.0
Using net-imap 0.3.4
Using net-smtp 0.3.3
Using rb-inotify 0.10.1
Using sassc 2.4.0
Using net-pop 0.1.2
Using rails-html-sanitizer 1.5.0
Using lis ten 3.8.0
Using activesupport 6.1.7.3
Using mail 2.8.1
Using rails-dom-testing 2.0.3
Using globalid 1.1.0
Using activemodel 6.1.7.3
Using actionview 6.1.7.3
Using activejob 6.1.7.3
Using activerecord 6.1.7.3
Using actionpack 6.1.7.3
Using jbuilder 2.11.5
Using railties 6.1.7.3
Using actioncable 6.1.7.3
Using web-console 4.2.0
Using actionmailer 6.1.7.3
Using sprockets-rails 3.4.2
Using activestorage 6.1.7.3
Using webpacker 5.4.4
Using sassc-rails 2.1.2
Using actionmailbox 6.1.7.3
Using actiontext 6.1.7.3
Using sass-rails 6.0.0
Using rails 6.1.7.3
Installing mysql2 0.5.5 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/qounme/Developments/projects/templates/rails-6/vendor/bundle/ruby/3.1.0/gems/mysql2-0.5.5/ext/mysql2
/Users/qounme/.rbenv/versions/3.1.4/bin/ruby -I /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0 extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_gc_mark_movable()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_enc_interned_str() in ruby.h... yes
-----
Using --with-openssl-dir=/Users/qounme/.rbenv/versions/3.1.4/openssl
-----
-----
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_DEFAULT_AUTH in mysql.h... yes
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.33/lib
-----
creating Makefile

current directory: /Users/qounme/Developments/projects/templates/rails-6/vendor/bundle/ruby/3.1.0/gems/mysql2-0.5.5/ext/mysql2
make DESTDIR= sitearchdir=./.gem.20230505-8569-clyohw sitelibdir=./.gem.20230505-8569-clyohw cl ean

current directory: /Users/qounme/Developments/projects/templates/rails-6/vendor/bundle/ruby/3.1.0/gems/mysql2-0.5.5/ext/mysql2
make DESTDIR= sitearchdir=./.gem.20230505-8569-clyohw sitelibdir=./.gem.20230505-8569-clyohw
compiling client.c
In file included from client.c:15:
./mysql_enc_name_to_ruby.h:43:1: warning: a function declaration without a prototype is deprecated in all versions of C and is not supported in C2x [-Wdeprecated-non-prototype]
mysql2_mysql_enc_name_to_rb_hash (str, len)
^
./mysql_enc_name_to_ruby.h:86:1: warning: a function declaration without a prototype is deprecated in all versions of C and is not supported in C2x [-Wdeprecated-non-prototype]
mysql2_mysql_enc_name_to_rb (str, len)
^
2 warnings generated.
compiling infile.c
compiling mysql2_ext.c
compiling result.c
result.c:304:35: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
        precision = field->length - (field->decimals > 0 ? 2 : 1);
                  ~ ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lzlib
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/qounme/Developments/projects/templates/rails-6/vendor/bundle/ruby/3.1.0/gems/mysql2-0.5.5 for inspection.
Results logged to /Users/qounme/Developments/projects/templates/rails-6/vendor/bundle/ruby/3.1.0/extensions/arm64-darwin-22/3.1.0/mysql2-0.5.5/gem_make.out

  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/rubygems/ext/builder.rb:102:in `run'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/rubygems/ext/builder.rb:51:in `block in make'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/rubygems/ext/builder.rb:43:in `each'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/rubygems/ext/builder.rb:43:in `make'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/rubygems/ext/ext_conf_builder.rb:42:in `build'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/rubygems/ext/builder.rb:171:in `build_extension'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/rubygems/ext/builder.rb:205:in `block in build_extensions'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/rubygems/ext/builder.rb:202:in `each'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/rubygems/ext/builder.rb:202:in `build_extensions'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/rubygems/installer.rb:843:in `build_extensions'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/bundler/rubygems_gem_installer.rb:72:in `build_extensions'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/bundler/rubygems_gem_installer.rb:28:in `install'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/bundler/source/rubygems.rb:207:in `install'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/bundler/installer/gem_installer.rb:54:in `install'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/bundler/installer/gem_installer.rb:16:in `install_from_spec'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/bundler/installer/parallel_installer.rb:186:in `do_install'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/bundler/installer/parallel_installer.rb:177:in `block in worker_pool'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/bundler/worker.rb:62:in `apply_func'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/bundler/worker.rb:57:in `block in process_queue'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/bundler/worker.rb:54:in `loop'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/bundler/worker.rb:54:in `process_queue'
  /Users/qounme/.rbenv/versions/3.1.4/lib/ruby/3.1.0/bundler/worker.rb:91:in `block (2 levels) in create_threads'

An error occurred while installing mysql2 (0.5.5), and Bundler cannot continue.

In Gemfile:
  mysql2

What I tried

・After executing brew install zlib, add the following content to .zshrc

export LDFLAGS="-L/opt/homebrew/opt/zlib/lib"
export CPPFLAGS="-I/opt/homebrew/opt/zlib/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/zlib/lib/pkgconfig"

・Add the following, which are not shown in brew info zlib but are mentioned in the answers here.

export DYLD_LIBRARY_PATH=/opt/homebrew/opt/zlib/lib:$DYLD_LIBRARY_PATH

·ReinstallCommand line tools by xcode-select --install

result:

  • The output log has no change.
  • Unable to bundle installation mysql2


P粉156415696
P粉156415696

reply all(2)
P粉748218846

The solutions proposed by Fyorden worked for me, but in my case they were installed by homebrew, so they were in different paths. I edited the file in the root project .bundle/config using the parameters:

BUNDLE_BUILD__MYSQL2: "--with-mysql-lib=/opt/homebrew/opt/mysql/lib --with-mysql-dir=/opt/homebrew/opt/mysql --with-mysql-config=/opt/homebrew/opt/mysql/bin/mysql_config \ --with-mysql-include=/opt/homebrew/opt/mysql/include --with-ldflags=-L/opt/homebrew/opt/zlib/lib"
P粉009186469

Hello, to correct the same error, I have created a file on the root project: .bundle/config

with this parameter
---
BUNDLE_BUILD__MYSQL2: "--with-mysql-lib=/usr/local/opt/mysql/lib --with-mysql-dir=/usr/local/opt/mysql --with-mysql-config=/usr/local/opt/mysql/bin/mysql_config \ --with-mysql-include=/usr/local/opt/mysql/include --with-ldflags=-L/usr/local/opt/zlib/lib"

I think this is a bug with the last mysql brew package

I was inspired byhttps://prabinpoudel .com.np/notes/error-while-installing-mysql2-in-m1-mac/

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template