Help us understand the problem. What is going on with this article?

bundle install 時、mysql2でエラー

実行環境

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.14.2
$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
$ rails -v
Rails 5.1.2

エラー内容

gemfileにgem 'mysql2'を記述しbundle installを行うと下記のようなエラーが発生。


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

    current directory: /Users/{username}/rails_app/test-mysql/vendor/bundle/gems/mysql2-0.5.2/ext/mysql2
/Users/{username}/.rbenv/versions/2.5.1/bin/ruby -I /Users/{username}/.rbenv/versions/2.5.1/lib/ruby/site_ruby/2.5.0 -r
./siteconf20181226-54895-ler7vx.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/{username}/.rbenv/versions/2.5.1/bin/$(RUBY_BASE_NAME)
/Users/{username}/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file.
(RuntimeError)
You have to install development tools first.
  from /Users/{username}/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:541:in `try_link0'
  from /Users/{username}/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:559:in `try_link'
  from /Users/{username}/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:770:in `try_func'
  from /Users/{username}/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:1057:in `block in have_func'
  from /Users/{username}/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:947:in `block in checking_for'
  from /Users/{username}/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:350:in `block (2 levels) in postpone'
  from /Users/{username}/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:320:in `open'
  from /Users/{username}/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:350:in `block in postpone'
  from /Users/{username}/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:320:in `open'
  from /Users/{username}/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:346:in `postpone'
  from /Users/{username}/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:946:in `checking_for'
  from /Users/{username}/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:1056: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/{username}/rails_app/test-mysql/vendor/bundle/extensions/x86_64-darwin-17/2.5.0-static/mysql2-0.5.2/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/{username}/rails_app/test-mysql/vendor/bundle/gems/mysql2-0.5.2 for inspection.
Results logged to
/Users/{username}/rails_app/test-mysql/vendor/bundle/extensions/x86_64-darwin-17/2.5.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.

意味がなかったこと(直接解決には繋がらなかったこと)

$ xcode-select --install

=> すでに入っとるわ

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

=>エラー、そして/vendor/bundle/extensions/x86_64-darwin-17/2.5.0-static/mysql2-0.5.2/mkmf.logに下記の記述。

clang: error: unsupported option '--with-cppflags=-I/usr/local/opt/openssl/include'

解決策

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

または

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

の後に

$ bundle install
Bundle complete! 19 Gemfile dependencies, 80 gems now installed.
Bundled gems are installed into `./vendor/bundle`

"--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"と、オプションを2つつけると通らないみたいですね。

長い道のりであった...

tktcorporation
勉強中
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした