STUDY MEMO

学習のメモ書き

<Ruby on Rails エラー編> bundle installで、Gem::Ext::BuildError: ERROR: Failed to build gem native extension.が発生

発生したエラー

> bundle
Warning: the running version of Bundler (2.0.2) is older than the version that created the lockfile (2.1.2). We suggest you upgrade to the latest version of Bundler by running `gem install bundler`.
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/.........
Using rake 13.0.1
Using concurrent-ruby 1.1.5
Using i18n 1.8.2
Using minitest 5.14.0
Using thread_safe 0.3.6
Using tzinfo 1.2.6
Using zeitwerk 2.2.2
Using activesupport 6.0.2.1
Using builder 3.2.4
Using erubi 1.9.0
Using mini_portile2 2.4.0
Fetching nokogiri 1.10.7
Installing nokogiri 1.10.7 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/Users/user_name/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/nokogiri-1.10.7/ext/nokogiri
/Users/user_name/.rbenv/versions/2.6.5/bin/ruby -I
/Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0 -r ./siteconf20210315-26465-xk69qq.rb
extconf.rb
checking if the C compiler accepts  -I
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/libxml2... *** 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/user_name/.rbenv/versions/2.6.5/bin/$(RUBY_BASE_NAME)
        --help
        --clean
/Users/user_name/.rbenv/versions/2.6.5/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/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:585:in `block in
try_compile'
        from /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:532:in `with_werror'
        from /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:585:in `try_compile'
        from extconf.rb:138:in `nokogiri_try_compile'
        from extconf.rb:162:in `block in add_cflags'
        from /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:643:in `with_cflags'
        from extconf.rb:161:in `add_cflags'
        from extconf.rb:416:in `<main>'

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

/Users/user_name/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-20/2.6.0/nokogiri-1.10.7/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/Users/user_name/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/nokogiri-1.10.7 for
inspection.
Results logged to
/Users/user_name/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-20/2.6.0/nokogiri-1.10.7/gem_make.out

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

In Gemfile:
  rails was resolved to 6.0.2.1, which depends on
    actioncable was resolved to 6.0.2.1, which depends on
      actionpack was resolved to 6.0.2.1, which depends on
        actionview was resolved to 6.0.2.1, which depends on
          rails-dom-testing was resolved to 2.0.3, which depends on
            nokogiri

Result

上記エラーで記載のあった、Result toの
/Users/user_name/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-20/2.6.0/nokogiri-1.10.7/gem_make.outを確認。

current directory: /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/nokogiri-1.10.7/ext/nokogiri
/Users/user_name/.rbenv/versions/2.6.5/bin/ruby -I /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0 -r ./siteconf20210315-26465-xk69qq.rb extconf.rb
checking if the C compiler accepts  -I /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/libxml2... *** 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/user_name/.rbenv/versions/2.6.5/bin/$(RUBY_BASE_NAME)
    --help
    --clean
/Users/user_name/.rbenv/versions/2.6.5/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/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:585:in `block in try_compile'
    from /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:532:in `with_werror'
    from /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:585:in `try_compile'
    from extconf.rb:138:in `nokogiri_try_compile'
    from extconf.rb:162:in `block in add_cflags'
    from /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:643:in `with_cflags'
    from extconf.rb:161:in `add_cflags'
    from extconf.rb:416:in `<main>'

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

  /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-20/2.6.0/nokogiri-1.10.7/mkmf.log

extconf failed, exit code 1

mkmf.log

上記で記載のあった
/Users/user_name/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-20/2.6.0/nokogiri-1.10.7/mkmf.logを確認。

"clang -o conftest -I/Users/user_name/.rbenv/versions/2.6.5/include/ruby-2.6.0/x86_64-darwin20 -I/Users/user_name/.rbenv/versions/2.6.5/include/ruby-2.6.0/ruby/backward -I/Users/user_name/.rbenv/versions/2.6.5/include/ruby-2.6.0 -I. -I/Users/user_name/.rbenv/versions/2.6.5/include  -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT   -O3 -ggdb3 -Wall -Wextra -Wdeclaration-after-statement -Wdeprecated-declarations -Wdivision-by-zero -Wimplicit-function-declaration -Wimplicit-int -Wpointer-arith -Wshorten-64-to-32 -Wwrite-strings -Wmissing-noreturn -Wno-constant-logical-operand -Wno-long-long -Wno-missing-field-initializers -Wno-overlength-strings -Wno-parentheses-equality -Wno-self-assign -Wno-tautological-compare -Wno-unused-parameter -Wno-unused-value -Wunused-variable -Wextra-tokens  -fno-common -pipe  -I /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/libxml2 conftest.c  -L. -L/Users/user_name/.rbenv/versions/2.6.5/lib -L. -L/Users/user_name/.rbenv/versions/2.6.5/lib  -fstack-protector-strong -L/usr/local/lib     -lruby.2.6    "
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
CommandLineToolsのパスが無効になってるということは、CommandLineToolsがinstallされてない可能性がある?
以下の対処法を実施。

対処方法

以下のコマンドでxcodeをinstallする。

$ xcode-select --install

余談

xcodeがインストールされて上記のコマンドを実行すると、以下の表示が出る。

$ xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates