无法在Big Sur上安装mysql2 Gem。

6

我正在使用Ruby 3.0.1p64版本,运行于macOS BigSur 11.04系统,已安装并运行MySQL。

在运行bundle install时,提示需要安装mysql2。

我尝试通过以下方式安装mysql2 gem:

sudo gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'

我得到了这个:

 Building native extensions. This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    current directory: /Users/henri/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/mysql2-0.5.3/ext/mysql2
/Users/henri/.rbenv/versions/3.0.1/bin/ruby -I /Users/henri/.rbenv/versions/3.0.1/lib/ruby/3.0.0 -r ./siteconf20210617-53969-y4phap.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdier
    --ruby=/Users/henri/.rbenv/versions/3.0.1/bin/$(RUBY_BASE_NAME)
    --with-mysql-dir
    --without-mysql-dir
    --with-mysql-include
    --without-mysql-include=${mysql-dir}/include
    --with-mysql-lib
    --without-mysql-lib=${mysql-dir}/lib
    --with-mysql-config
    --without-mysql-config
    --with-mysqlclient-dir
    --without-mysqlclient-dir
    --with-mysqlclient-include
    --without-mysqlclient-include=${mysqlclient-dir}/include
    --with-mysqlclient-lib
    --without-mysqlclient-lib=${mysqlclient-dir}/lib
    --with-mysqlclientlib
    --without-mysqlclientlib
/Users/henri/.rbenv/versions/3.0.1/lib/ruby/3.0.0/mkmf.rb:1050:in `block in find_library': undefined method `split' for nil:NilClass (NoMethodError)
    from /Users/henri/.rbenv/versions/3.0.1/lib/ruby/3.0.0/mkmf.rb:1050:in `collect'
    from /Users/henri/.rbenv/versions/3.0.1/lib/ruby/3.0.0/mkmf.rb:1050:in `find_library'
    from extconf.rb:87:in `<main>'

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /Users/henri/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/extensions/arm64-darwin-20/3.0.0/mysql2-0.5.3/mkmf.log

extconf failed, exit code 1

我的 mkmf.log 文件如下:

    --------------------

have_func: checking for rb_wait_for_single_fd()... -------------------- yes

"clang -fdeclspec -o conftest -I/Users/henriknafo/.rbenv/versions/3.0.1/include/ru$
conftest.c:14:57: error: use of undeclared identifier 'rb_wait_for_single_fd'
int t(void) { void ((*volatile p)()); p = (void ((*)()))rb_wait_for_single_fd; ret$
                                                        ^
1 error generated.
checked program was:
/* begin */
 1: #include "ruby.h"
 2:
 3: /*top*/
 4: extern int t(void);
 5: int main(int argc, char **argv)
 6: {
 7:   if (argc > 1000000) {
 8:     int (* volatile tp)(void)=(int (*)(void))&t;
 9:     printf("%d", (*tp)());
10:   }
11:
12:   return !!argv[argc];
13: }
14: int t(void) { void ((*volatile p)()); p = (void ((*)()))rb_wait_for_single_fd;$
/* end */

这可能与 Ruby 3.0.1 不兼容吗? 我应该安装哪个库,才能安装 mysql2 gem?

有人有建议吗?


1
不要使用 sudo 安装 gem。除了安全问题外,您还会在文件权限方面遇到未来的问题。https://www.moncefbelyamani.com/why-you-should-never-use-sudo-to-install-ruby-gems/ - max
保存问题,尽管当然没有使用 sudo - Victor S
如果你使用的是 M1 / arm64 Mac,请查看这个 Github 评论 - 它对我有用:https://github.com/brianmario/mysql2/issues/1175#issuecomment-910236422 - Joshua Pinter
5个回答

16

在执行bundle install之前,我安装了mysql2,并删除了名为.bundle的文件夹:

gem install mysql2 -v '0.5.3' -- \
 --with-mysql-lib=/opt/homebrew/Cellar/mysql/8.0.25_1/lib \
 --with-mysql-dir=/opt/homebrew/Cellar/mysql/8.0.25_1 \
 --with-mysql-config=/opt/homebrew/Cellar/mysql/8.0.25_1/bin/mysql_config \
 --with-mysql-include=/opt/homebrew/Cellar/mysql/8.0.25_1/include

现在我很好。


你可能需要更新路径,以指向你已安装的版本,但之后它就像魔法般地运行! - jewilmeer
对于Intel Mac,请将路径更改为/usr/local/Cellar/经过多个月的努力,我的问题终于得到解决,非常感谢。 - stepheaw

4
  1. 检查是否安装了openssl。
  2. 如果未安装,请安装。 brew install openssl
  3. 链接它。 brew link openssl

你会得到类似这样的东西。

 ➜ brew link openssl
Warning: Refusing to link macOS provided/shadowed software: openssl@1.1
If you need to have openssl@1.1 first in your PATH, run:
echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@1.1 you may need to set:
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

For pkg-config to find openssl@1.1 you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"
  1. 复制
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

而且

  export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

然后运行它。

  1. 再次运行bundle install

在我的项目中使用正常。

更新:

  1. export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
  2. 运行bundle install

可能会起作用。


在我的情况下没有起作用。我仍然得到相同的错误:ld: 找不到库-lssl。 - Henri
编辑后的答案。 - togi
这对我在 Monterrey 进行安装时有效。 - Derrell Durrett
这个解决方案对我来说完美地运作了。我正在使用 macOS Monterey。 - Lawrence Chege

1
我正在使用Macbook OS Big Sur,这对我起作用了:
brew install openssl
brew link openssl

  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
  export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

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

bundle install

0
这是对我有用的解决方案:
brew install openssl
brew link mysql

现在当我启动rails s时,会出现一个错误提示:安装mysql2(0.5.3)时发生了错误,Bundler无法继续。 请确保在捆绑之前成功运行gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/' - Henri

0

1
我尝试使用brew install libmysqlclient-dev命令安装库,但是它没有找到该库。我甚至安装了mysql-connector-c也无法正常工作。 - Henri

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