使用“gem install...”命令安装后,在bundle install过程中mysql2 gem失败。

5

以下是我的bundle install的日志记录

Fetching mysql2 0.5.2
Installing mysql2 0.5.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
...
...
...
Cannot find mysql_config at /usr/local/opt/mysql@5.6/bin/mysql_config
--with-ldflags=-L/usr/local/opt/openssl/lib
--with-cppflags=-I/usr/local/opt/openssl/include
-----
*** 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=.
    --curdir
    --ruby=/Users/admin/.asdf/installs/ruby/2.6.3/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

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

/Users/admin/development/locari/vendor/bundle/ruby/2.6.0/extensions/x86_64-darwin-18/2.6.0-static/mysql2-0.5.2/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/Users/admin/development/locari/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.2 for
inspection.
Results logged to
/Users/admin/development/locari/vendor/bundle/ruby/2.6.0/extensions/x86_64-darwin-18/2.6.0-static/mysql2-0.5.2/gem_make.out

An error occurred while installing mysql2 (0.5.2), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'` succeeds
before bundling.

所以我按照它的指示执行,并运行 sudo gem install mysql2 -v '0.5.2' --source

$ sudo gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'
Building native extensions. This could take a while...
Successfully installed mysql2-0.5.2
Parsing documentation for mysql2-0.5.2
Done installing documentation for mysql2 after 0 seconds
1 gem installed

安装成功了!但是当我再次运行bundle install时,我遇到了同样的错误。我尝试过在brew中卸载、取消链接和重新安装mysql

接下来我该尝试什么呢?


你是使用Windows还是Linux? - kishore cheruku
@kishorecheruku MacOS - coconutcrab
https://dev59.com/1GQm5IYBdhLWcg3w6SdR#53747176 - nourza
首先尝试在本地安装MySQL,然后您可以尝试bundle install。 - kishore cheruku
@nourza 那个页面上的内容都没有帮助。 @kishorecheruku 我已经说过了,当我本地安装时它可以工作,但是当我将其作为“bundle install”的一部分运行时就不行了。 - coconutcrab
这个命令是:$bundle config build.mysql2 --with-mysql-config=/usr/local/Cellar/mysql/5.6.10/bin/mysql_config - nourza
3个回答

7

首先运行以下命令:

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

然后运行bundle install即可。


该命令涉及IT技术,可以帮助安装mysql2 gem。

分别运行这两个命令只会覆盖第一个。你需要将这两个安装选项放入第一个命令的单个字符串中。 - Joshua Pinter
这可能取决于打包程序的版本,但当我将两者放在单个字符串中时,它将整个字符串作为单个参数传递,然后在构建时被静默忽略。 对我有用的是只需设置 bundle config --local build.mysql2 --with-ldflags=-L/usr/local/opt/openssl/lib - ShadSterling

5

在M1 Mac上安装

该方法适用于ruby v2.7.2macOS Big Sur 11.4

安装依赖项

brew install mysql zstd openssl

安装 gem

gem install mysql2 -- --with-ldflags="-L $(brew --cellar zstd)/1.5.0/lib -L $(brew --prefix openssl)/lib" --with-ldlibs="-lzstd -lssl" --with-cppflags="-I $(brew --prefix openssl)/include"

作为替代方案,您可以修改打包器配置:
bundle config --local build.mysql2 --with-ldflags="-L $(brew --cellar zstd)/1.5.0/lib -L $(brew --prefix openssl)/lib" --with-ldlibs="-lzstd -lssl" --with-cppflags="-I $(brew --prefix openssl)/include"

并运行bundle install


我没有M1 Mac,但这对我有效! - gosukiwi

3

使用构建选项与bundler一起使用。

在应用目录中尝试运行以下命令:

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

这样设置只适用于您的应用程序,如果其他人需要相似的配置,则可以方便地将其检入您的存储库中。但是,您还可以通过将 --local 替换为 --global 将其作为计算机的全局配置,它将适用于您用户账户上的所有 bundle install 命令。

更新

实际上这对我没有起作用,但我会把它留在这里,因为它应该会工作。我基本上只需要运行:

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

即使设置了此构建配置,也不要使用bundler。


谢谢,这对我很有帮助! - LisaH
@LisaH 不客气!如果它对你有帮助,你应该点个赞。;) - Joshua Pinter

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