4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Rails 5.2.3 で mysql2 が bundle install できない

Last updated at Posted at 2019-09-24

mysql2がbundle installできない

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/Users/naoya.ohsaki/.rbenv/versions/2.6.2/gemsets/finacial_analysis/gems/mysql2-0.5.2/ext/mysql2
/Users/naoya.ohsaki/.rbenv/versions/2.6.2/bin/ruby -I /Users/naoya.ohsaki/.rbenv/versions/2.6.2/lib/ruby/2.6.0 -r
./siteconf20190911-34230-1pqv7kn.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
-----
Using mysql_config at /usr/local/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_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 /usr/local/Cellar/mysql/8.0.17_1/lib
-----
creating Makefile

current directory: /Users/naoya.ohsaki/.rbenv/versions/2.6.2/gemsets/finacial_analysis/gems/mysql2-0.5.2/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/naoya.ohsaki/.rbenv/versions/2.6.2/gemsets/finacial_analysis/gems/mysql2-0.5.2/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
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/naoya.ohsaki/.rbenv/versions/2.6.2/gemsets/finacial_analysis/gems/mysql2-0.5.2 for inspection.
Results logged to
/Users/naoya.ohsaki/.rbenv/versions/2.6.2/gemsets/finacial_analysis/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.

いつもの無駄に長いエラーが出ました。

ld: library not found for -lssl

なんて書いてあります。

解決策

$ brew info openssl
openssl: stable 1.0.2s (bottled) [keg-only]
SSL/TLS cryptography library
https://openssl.org/
/usr/local/Cellar/openssl/1.0.2s (1,795 files, 12.0MB)
  Poured from bottle on 2019-07-24 at 18:03:22
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/openssl.rb
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.zshrc

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

For pkg-config to find openssl you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl/lib/pkgconfig"

==> Analytics
install: 404,377 (30 days), 1,646,550 (90 days), 6,456,690 (365 days)
install_on_request: 49,803 (30 days), 222,487 (90 days), 862,858 (365 days)
build_error: 0 (30 days)

opensslのコンパイルする情報を手に入れられます。下記がそれです。

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

最初、これを一緒にしたコマンドを打ってみました。

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

すると、

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/Users/naoya.ohsaki/.rbenv/versions/2.6.2/gemsets/finacial_analysis/gems/mysql2-0.5.2/ext/mysql2
/Users/naoya.ohsaki/.rbenv/versions/2.6.2/bin/ruby -I /Users/naoya.ohsaki/.rbenv/versions/2.6.2/lib/ruby/2.6.0 -r
./siteconf20190911-38087-14y1iqw.rb extconf.rb --with-ldflags\=-L/usr/local/opt/openssl/lib\
--with-cppflags\=-I/usr/local/opt/openssl/include
checking for rb_absint_size()... *** 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/naoya.ohsaki/.rbenv/versions/2.6.2/bin/$(RUBY_BASE_NAME)
/Users/naoya.ohsaki/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:467:in `try_do': The compiler failed to generate an
executable file. (RuntimeError)
You have to install development tools first.
	from /Users/naoya.ohsaki/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:552:in `try_link0'
	from /Users/naoya.ohsaki/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:570:in `try_link'
	from /Users/naoya.ohsaki/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:782:in `try_func'
	from /Users/naoya.ohsaki/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:1069:in `block in have_func'
	from /Users/naoya.ohsaki/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:959:in `block in checking_for'
	from /Users/naoya.ohsaki/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:361:in `block (2 levels) in postpone'
	from /Users/naoya.ohsaki/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:331:in `open'
	from /Users/naoya.ohsaki/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:361:in `block in postpone'
	from /Users/naoya.ohsaki/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:331:in `open'
	from /Users/naoya.ohsaki/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:357:in `postpone'
	from /Users/naoya.ohsaki/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:958:in `checking_for'
	from /Users/naoya.ohsaki/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:1068:in `have_func'
	from extconf.rb:25:in `<main>'

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

/Users/naoya.ohsaki/.rbenv/versions/2.6.2/gemsets/finacial_analysis/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/naoya.ohsaki/.rbenv/versions/2.6.2/gemsets/finacial_analysis/gems/mysql2-0.5.2 for inspection.
Results logged to
/Users/naoya.ohsaki/.rbenv/versions/2.6.2/gemsets/finacial_analysis/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.

In Gemfile:
  mysql2

若干エラーが変わりました。いつものオプションを示してくるエラーです。オプション下の文章に注目してみると、compileにまた失敗しているようです。

To see why this extension failed to compile,

どちらか片っぽうだけを指定すればいいようです。

$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib"
.
.
.
Fetching mysql2 0.5.2
Installing mysql2 0.5.2 with native extensions
Fetching ngram 1.0.0
Installing ngram 1.0.0
.
.
.

うまくいきました。
LDFLAGSとCPPFLAGSの違いとかはまた時間があった時に調べます。

上記の方法でもインストールが始まらない場合、

$ gem install mysql2 -v '0.4.5' --source 'https://rubygems.org/' -- --with-ldflags=-L/usr/local/opt/openssl/lib

こちらでできます。

4
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?