尝试使用mysql2 gem安装应用程序时出现错误

106

我正在尝试安装一个使用mysql2宝石的开源rails 3.2.21应用程序,但是当我尝试运行bundle命令时,会出现以下错误:

Fetching: mysql2-0.3.18.gem (100%)
Building native extensions.  This could take a while...
p
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    /Users/my_username/.rvm/rubies/ruby-2.1.2/bin/ruby -r ./siteconf20150614-72129-orqsb7.rb extconf.rb
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/Cellar/mysql/5.6.25/lib
-----
creating Makefile

make "DESTDIR=" clean

make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
linking shared-object mysql2/mysql2.bundle
ld: warning: directory not found for option '-L/Users/travis/.sm/pkg/active/lib'
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

make failed, exit code 2

Gem files will remain installed in /Users/my_username/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/mysql2-0.3.18 for inspection.
Results logged to /Users/my_username/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/extensions/x86

我尝试通过Homebrew卸载所有安装的mysql版本,并重新安装它们,步骤如下:

brew uninstall --force mysql && brew install mysql

接下来运行:

sudo gem install mysql2

正如这里提出的一些类似问题所建议的,但仍然导致与上述相同的错误。

请有人能够提供如何启动和运行此程序的指导吗?


尝试运行“brew doctor”并检查是否有任何错误。如果有,请按照说明进行操作,然后再次尝试。 - K.M.
你已经安装了 gem 的依赖项吗?https://github.com/brianmario/mysql2#general-instructions - lcguida
@ValAsensio,我非常不同意你的观点,我认为如果可能的话,你应该始终尝试在开发中使用生产数据库。我从不在开发中使用sqlite,也没有和我一起工作的人这样做。 - niels
@niels。是的,我同意“我认为您应该始终尝试在开发过程中使用生产数据库”。我先前的那条古老评论已经没有用了,我已将其删除。 - Elvn
19个回答

178

对于仍然遇到这个问题的人:

当你通过brew安装openssl时,应该会收到以下消息:

Apple已弃用OpenSSL,转而使用自己的TLS和加密库。

通常您不会因此受到任何影响。如果您构建自己的软件并且需要此公式,则需要添加到构建变量中:

LDFLAGS: -L/usr/local/opt/openssl/lib
CPPFLAGS: -I/usr/local/opt/openssl/include
PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig

您可以通过运行以下命令设置这些构建标志(用于本地应用程序):

bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"

这对我有用。

查看bundler文档获取更多信息。


85
如果您只想使用gem install,则可以使用以下命令: gem install mysql2 -v '0.3.21' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include 请注意,这是一个安装MySQL2的命令,并指定了版本号为0.3.21。在安装过程中,它将使用--with-ldflags=-L/usr/local/opt/openssl/lib--with-cppflags=-I/usr/local/opt/openssl/include选项来设置所需的库和头文件路径。 - niels
2
@the12 Niels 的解决方案是针对 "gem install" 的,我的是针对 "bundle install" 的。你遇到了什么问题,能否发一下错误信息?我发布的解决方案在我的电脑上仍然有效。 - Alessandro Berardi
1
我开始尝试bundle install依赖于mysql的repo。@AlessandroBerardi的解决方案对我没有用;@niels的解决方案有用。 :/ - vergenzt
2
bundle config 命令对我没用(虽然过去它是有效的)。Niels 的 'gem install' 命令在几个小时后成功了。bundle config 命令失败,因为它似乎将 --with-ldflags 和 --with-cppflags 作为参数传递给 clang,从而产生以下错误:clang: error: unsupported option '--with-cppflags=-I/usr/local/opt/openssl/include' - jpayne
5
在最新的OSX系统上,使用“--with-cppflags”似乎会出现问题。我认为可以省略它。下面是需要翻译的命令:“bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib"”。 - Xavier Shay
显示剩余4条评论

78

错误日志显示:

ld: library not found for -lssl

所以,您需要安装libssl

brew install openssl

正如评论中指出的那样,可能需要将路径导出给库。


export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/

36
这还不够,之后需要像这样进行导出: export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/ - Gilg Him
1
导出 LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/` 在尝试多次后终于奏效。谢谢!! - Abhi
为什么Ruby比其他生态系统更容易遭受这个问题的困扰?为什么Gem开发者不努力制定良好的安装流程? - Andy Ray
@AndyRay 这绝对不是 Ruby 的问题,而是 MacOS 的问题。这个操作系统从来都不适合开发。 - Aleksei Matiushkin

47

试试这个:

gem install mysql2 -v '0.5.2' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include

(根据需要更新版本)


1
鉴于未找到SSL库,这是正确的方法,并且对我来说运行良好。 - TehJabbit
2
首先安装 brew install openssl 然后运行 gem install mysql2 -v '0.4.10' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include 对我有用,谢谢! - Touseef Murtaza
1
@TouseefMurtaza 你的答案对我有用 - 谢谢! - Jared

36

对我来说,解决方案是安装Xcode命令行工具。

最近我通过Mac App Store更新了Xcode,但每次这样做时,我发现我必须重新安装命令行工具。

xcode-select --install

33

基于这里的解决方案here

brew install openssl

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/

问题已解决。


1
"export" 真的很有用。即使存在 OpenSSL,路径也会丢失。 - Yakob Ubaidi

18

在Homebrew更新(openssl@1.1)后,库的路径发生了变化,因此可以使用以下命令:

bundle config build.mysql2 --with-opt-dir=$(brew --prefix openssl)
bundle install

它将修复ld: library not found for -lssl error错误。


这就是方法。 - Jeremy Ferguson
确认这在2022年12月仍然有效,而其他答案有点过时。 - Gary Bao 鲍昱彤

11

感谢 @mudasobwa 指引我正确的方向。结果发现错误是由一个未链接的 openssl 文件引起的,因此运行以下命令:

brew reinstall openssl && brew link openssl --force 

问题已解决。我在这里找到了解决方案:OpenSSL、RVM、Brew 冲突错误


11

在 MacBook Air M1 (macOS) 上,这对我有用。

安装 zstd

使用 brew 安装 zstd

安装 mysql2

使用 gem 安装 mysql2 -v '0.5.3' -- --with-opt-dir=$(brew --prefix openssl) --with-ldflags=-L/opt/homebrew/Cellar/zstd/1.5.0/lib


这是唯一在M1 Mac Mini上为我工作的东西。感谢您的发布! - Joshua Pinter
感谢您的这篇文章! - Paul Trott
这是针对 Mac M1 Pro 的解决方案。 - Israel

3

2

在我的 macOS Monterey 12.3 (M1 Pro) 上,最终这个方法对我起作用了:

 gem install mysql2 -- --with-mysql-dir=/opt/homebrew/Cellar/mysql/8.0.28_1

请确保您已阅读安装说明。对我来说,值得注意的是:

  • 确保已安装MySQL (brew install mysql)
  • 确保已安装XCode选择工具 (xcode-select --install)
  • with-mysql-dir选项设置为mysql安装的位置(用brew info mysql检查)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接