小林ノエルのエンジニア的忘備録

フリーランス兼会社員エンジニアが技術とかリモートワークのこととかをツラツラ書いていきます

mysql2 gemのインストールに失敗した時の対処メモ

下記の記事の続きの話。

f-world21.hatenablog.com

問題

ひとまずrubyの再インストールはできて、bundle installをしていたらmysql2 gemのインストールでコケた。

...
...
...
Fetching mysql2 0.5.2
Installing mysql2 0.5.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/noel/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/mysql2-0.5.2/ext/mysql2
/Users/noel/.rbenv/versions/2.6.2/bin/ruby -I /Users/noel/.rbenv/versions/2.6.2/lib/ruby/2.6.0 -r ./siteconf20190815-27563-t7pbet.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/noel/.rbenv/versions/2.6.2/bin/$(RUBY_BASE_NAME)
/Users/noel/.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/noel/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:552:in `try_link0'
    from /Users/noel/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:570:in `try_link'
    from /Users/noel/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:782:in `try_func'
    from /Users/noel/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:1069:in `block in have_func'
    from /Users/noel/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:959:in `block in checking_for'
    from /Users/noel/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:361:in `block (2 levels) in postpone'
    from /Users/noel/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:331:in `open'
    from /Users/noel/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:361:in `block in postpone'
    from /Users/noel/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:331:in `open'
    from /Users/noel/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:357:in `postpone'
    from /Users/noel/.rbenv/versions/2.6.2/lib/ruby/2.6.0/mkmf.rb:958:in `checking_for'
    from /Users/noel/.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/noel/.rbenv/versions/2.6.2/lib/ruby/gems/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/noel/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/mysql2-0.5.2 for inspection.
Results logged to /Users/noel/.rbenv/versions/2.6.2/lib/ruby/gems/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.

In Gemfile:
  mysql2

指定通り下記のようにやってみたが、同様のエラーでインストールできなかった。

$ gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'
Building native extensions. This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.
...
...
...

解決方法

ググってたらこちらのqiitaの記事の3番目の方法が良さそうだったのでそのようにした。

qiita.com

homebrewでopensslはインストール済みだったので、下記のコマンドだけでいけた。

$ gem install mysql2 -v '0.5.2' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
Building native extensions with: '--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include'
This could take a while...
Successfully installed mysql2-0.5.2
1 gem installed

その後、無事にbundle installできた!めでたしめでたし。

環境

  • macOS Mojave 10.14.6
  • ruby 2.6.2
  • openssl 1.0.2s(homebrewでインストール)
  • MySQL 5.7.24(homebrewでインストール)
  • mysql2 gem 0.5.2