无法在macOS Ventura上捆绑安装'mysql2',原因是'ld:library not found for -lzlib'

回答 3 浏览 1110 2023-05-05

无法用bundle install安装mysql2。 我这样问是因为一些网站和博客显示了如何解决sslzstd引起的问题,但我找不到zlib引起的解决方案。有没有人遇到过同样的问题?

顺便说一下,我是在本地运行bundle install,而不是在docker上。

  • 操作系统: Ventura(13.3.1).
  • Xcode: 14.3
  • ruby: 3.1.4
  • rails: 6.1
  • mysql2: 0.5.5

已经完成了:

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

下面是日志输出。

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 listen 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 clean

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

我试过的东西

在做完brew install zlib后,将以下内容添加到.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"

・添加在brew info zlib中没有显示,但在答案中提到的这里

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

・用xcode-select --install重新安装command line tools

其结果是:

  • 输出的日志没有变化。
  • 无法进行捆绑安装 mysql2
qounme 提问于2023-05-05
3 个回答
#1楼 已采纳
得票数 7

你好,为了纠正同样的错误,我在根项目上创建了一个文件:.bundle/config,参数如下

---
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"

我想这是最后一个mysql brew包的一个bug。

我受到https://prabinpoudel.com.np/notes/error-while-installing-mysql2-in-m1-mac/的启发。

Fiyorden 提问于2023-05-08
Fiyorden 修改于2023-05-08
在我的例子中,mysql的路径是/opt/homebrew/opt/mysqlqounme 2023-05-09
你也可以用bundle config mysql2 ...来设置。tadman 2023-05-15
#2楼
得票数 2

Fyorden提出的解决方案对我有用,但在我的情况下,这些是由homebrew安装的,所以它们位于不同的路径中。我编辑了根项目中的文件.bundle/config,参数为::

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"
ahmed0hazem 提问于2023-05-11
ahmed0hazem 修改于2023-05-12
#3楼
得票数 0

我遇到了与你相同或相似的问题。绕过brew,从mysql的网站上手动安装对我来说是个好办法:https://dev.mysql.com/downloads/mysql/

你可能会遇到一个问题,就是openssl库后来没有正确链接起来,这个问题通过这里发现的一些建议来解决symlink:https://github.com/se3000/ruby-eth/issues/47

Donaven Snowden 提问于2023-05-08