无法在 Bigsur 上安装 mysql2 Gem

Posted

技术标签:

【中文标题】无法在 Bigsur 上安装 mysql2 Gem【英文标题】:unable to install mysql2 Gem on Bigsur 【发布时间】:2021-12-23 22:43:16 【问题描述】:

我正在使用 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

有人有什么建议吗?

【问题讨论】:

不要使用sudo 安装gem。除了安全问题之外,您只是在为将来的文件权限问题做好准备。 moncefbelyamani.com/… 保存问题,当然不使用sudo 【参考方案1】:

我在安装包之前安装了 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

现在我很好。

【讨论】:

您可能需要更新您的路径以指向您已安装的版本,但之后它就像一个魅力!【参考方案2】:
    检查是否安装了 openssl。 如果未安装,请安装它。 brew install openssl 链接它。 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"
    复制
  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 install

它适用于我的项目。

更新:

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

可能有效。

【讨论】:

在我的情况下不起作用。我仍然得到同样的错误 ld: library not found for -lssl 编辑的答案。【参考方案3】:

这对我有用:

brew 安装 openssl

酿造链接mysql

【讨论】:

现在当我启动 rails s... 我收到一条错误消息:安装 mysql2 (0.5.3) 时出错,并且 Bundler 无法继续。在捆绑之前确保gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/' 成功。【参考方案4】:

你可能需要 libmysqlclient-dev 看看Error installing mysql2: ERROR: Failed to build gem native extension线程上的最后一条评论(https://***.com/a/53081763/7205276)

【讨论】:

我尝试 brew install libmysqlclient-dev 并没有找到库。我什至安装了 mysql-connector-c 并且无法正常工作。【参考方案5】:

我使用的是 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

【讨论】:

以上是关于无法在 Bigsur 上安装 mysql2 Gem的主要内容,如果未能解决你的问题,请参考以下文章

错误:安装 mysql2 时出错:错误:无法在 AWS Linux 上构建 gem 原生扩展

无法在 Windows 7 上安装 MySQL2 gem

MySQL2 gem 无法安装

无法安装旧的 mysql2 gem

在 Ubuntu 20.04 上安装 mysql2 gem 版本“0.3.21”时出错

无法为 Rails 项目安装 mysql2 gem