Rails: macOSをMojaveにアップグレード後`bundle install`がエラーになった場合の対応方法

問題

macOSをHigh Sierra(10.13)からMojave(10.14)にアップグレードした後、Railsアプリを新規作成するためにbundle installすると以下のエラーが発生しました。

なお、私のMacBook ProにはXcodeは入れておらず(サイズがでかすぎるので)、CommandLineTools(Command_Line_Tools_macOS_10.13_for_Xcode_10.dmg)をインストールしていましたが、Mojaveにアップグレードした機会にhttps://developer.apple.com/download/more/から現時点で最新のCommand_Line_Tools_macOS_10.14_for_Xcode_10.1_Beta_2.dmgをダウンロードして上書きインストールした状態でした。

  • bundle initを実行し、生成されたGemfileの#gem "rails"のコメントを外す。
  • bundle installを実行(bundler抜きでのrails newでもおそらく同じ結果になります)
$ bundle install
Fetching gem metadata from https://rubygems.org/..........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Fetching concurrent-ruby 1.0.5
Fetching rake 12.3.1
Fetching minitest 5.11.3
Installing minitest 5.11.3
Installing rake 12.3.1
Installing concurrent-ruby 1.0.5
Fetching thread_safe 0.3.6
Installing thread_safe 0.3.6
Fetching builder 3.2.3
Installing builder 3.2.3
Fetching erubi 1.7.1
Fetching mini_portile2 2.3.0
Installing erubi 1.7.1
Fetching crass 1.0.4
Installing mini_portile2 2.3.0
Fetching rack 2.0.5
Installing crass 1.0.4
Installing rack 2.0.5
Fetching nio4r 2.3.1
Installing nio4r 2.3.1 with native extensions
Fetching websocket-extensions 0.1.3
Installing websocket-extensions 0.1.3
Fetching mini_mime 1.0.1
Installing mini_mime 1.0.1
Fetching arel 9.0.0
Installing arel 9.0.0
Fetching mimemagic 0.3.2
Using bundler 1.16.2
Fetching method_source 0.9.0
Installing method_source 0.9.0
Fetching thor 0.20.0
Installing mimemagic 0.3.2
Installing thor 0.20.0
Fetching tzinfo 1.2.5
Fetching i18n 1.1.0
Installing tzinfo 1.2.5
Installing i18n 1.1.0
Fetching nokogiri 1.8.5
Fetching websocket-driver 0.7.0
Installing websocket-driver 0.7.0 with native extensions
Installing nokogiri 1.8.5 with native extensions
Fetching rack-test 1.1.0
Fetching mail 2.7.0
Installing rack-test 1.1.0
Installing mail 2.7.0
Fetching sprockets 3.7.2
Installing sprockets 3.7.2
Fetching marcel 0.3.3
Installing marcel 0.3.3
Fetching activesupport 5.2.1
Installing activesupport 5.2.1
Fetching activemodel 5.2.1
Fetching globalid 0.4.1
Installing globalid 0.4.1
Installing activemodel 5.2.1
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/Users/hachi8833/deve/rails/rails5_2_1/vendor/bundle/ruby/2.5.0/gems/nokogiri-1.8.5/ext/nokogiri
/Users/hachi8833/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181010-6601-9lqb3q.rb
extconf.rb
checking if the C compiler accepts ... *** 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/hachi8833/.rbenv/versions/2.5.1/bin/$(RUBY_BASE_NAME)
    --help
    --clean
/Users/hachi8833/.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/hachi8833/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:574:in `block in
try_compile'
from /Users/hachi8833/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:521:in
`with_werror'
from /Users/hachi8833/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:574:in
`try_compile'
    from extconf.rb:138:in `nokogiri_try_compile'
    from extconf.rb:162:in `block in add_cflags'
from /Users/hachi8833/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:632:in
`with_cflags'
    from extconf.rb:161:in `add_cflags'
    from extconf.rb:410:in `<main>'

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

/Users/hachi8833/deve/rails/rails5_2_1/vendor/bundle/ruby/2.5.0/extensions/x86_64-darwin-17/2.5.0-static/nokogiri-1.8.5/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/Users/hachi8833/deve/rails/rails5_2_1/vendor/bundle/ruby/2.5.0/gems/nokogiri-1.8.5
for inspection.
Results logged to
/Users/hachi8833/deve/rails/rails5_2_1/vendor/bundle/ruby/2.5.0/extensions/x86_64-darwin-17/2.5.0-static/nokogiri-1.8.5/gem_make.out

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

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


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

current directory:
/Users/hachi8833/deve/rails/rails5_2_1/vendor/bundle/ruby/2.5.0/gems/nio4r-2.3.1/ext/nio4r
/Users/hachi8833/.rbenv/versions/2.5.1/bin/ruby -r
./siteconf20181010-6601-1octr2h.rb extconf.rb
checking for unistd.h... *** 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/hachi8833/.rbenv/versions/2.5.1/bin/$(RUBY_BASE_NAME)
/Users/hachi8833/.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/hachi8833/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:590:in `try_cpp'
from /Users/hachi8833/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:1097:in `block
in have_header'
from /Users/hachi8833/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:947:in `block in
checking_for'
from /Users/hachi8833/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:350:in `block (2
levels) in postpone'
    from /Users/hachi8833/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:320:in `open'
from /Users/hachi8833/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:350:in `block in
postpone'
    from /Users/hachi8833/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:320:in `open'
from /Users/hachi8833/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:346:in
`postpone'
from /Users/hachi8833/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:946:in
`checking_for'
from /Users/hachi8833/.rbenv/versions/2.5.1/lib/ruby/2.5.0/mkmf.rb:1096:in
`have_header'
    from extconf.rb:14:in `<main>'

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

/Users/hachi8833/deve/rails/rails5_2_1/vendor/bundle/ruby/2.5.0/extensions/x86_64-darwin-17/2.5.0-static/nio4r-2.3.1/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/Users/hachi8833/deve/rails/rails5_2_1/vendor/bundle/ruby/2.5.0/gems/nio4r-2.3.1 for
inspection.
Results logged to
/Users/hachi8833/deve/rails/rails5_2_1/vendor/bundle/ruby/2.5.0/extensions/x86_64-darwin-17/2.5.0-static/nio4r-2.3.1/gem_make.out

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

In Gemfile:
  rails was resolved to 5.2.1, which depends on
    actioncable was resolved to 5.2.1, which depends on
      nio4r


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

current directory:
/Users/hachi8833/deve/rails/rails5_2_1/vendor/bundle/ruby/2.5.0/gems/websocket-driver-0.7.0/ext/websocket-driver
/Users/hachi8833/.rbenv/versions/2.5.1/bin/ruby -r
./siteconf20181010-6601-1svzpns.rb extconf.rb
creating Makefile

current directory:
/Users/hachi8833/deve/rails/rails5_2_1/vendor/bundle/ruby/2.5.0/gems/websocket-driver-0.7.0/ext/websocket-driver
make "DESTDIR=" clean

current directory:
/Users/hachi8833/deve/rails/rails5_2_1/vendor/bundle/ruby/2.5.0/gems/websocket-driver-0.7.0/ext/websocket-driver
make "DESTDIR="
compiling websocket_mask.c
In file included from websocket_mask.c:1:
In file included from
/Users/hachi8833/.rbenv/versions/2.5.1/include/ruby-2.5.0/ruby.h:33:
In file included from
/Users/hachi8833/.rbenv/versions/2.5.1/include/ruby-2.5.0/ruby/ruby.h:29:
/Users/hachi8833/.rbenv/versions/2.5.1/include/ruby-2.5.0/ruby/defines.h:112:10:
fatal error: 'stdio.h' file not found
#include <stdio.h>
         ^~~~~~~~~
1 error generated.
make: *** [websocket_mask.o] Error 1

make failed, exit code 2

Gem files will remain installed in
/Users/hachi8833/deve/rails/rails5_2_1/vendor/bundle/ruby/2.5.0/gems/websocket-driver-0.7.0
for inspection.
Results logged to
/Users/hachi8833/deve/rails/rails5_2_1/vendor/bundle/ruby/2.5.0/extensions/x86_64-darwin-17/2.5.0-static/websocket-driver-0.7.0/gem_make.out

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

In Gemfile:
  rails was resolved to 5.2.1, which depends on
    actioncable was resolved to 5.2.1, which depends on
      websocket-driver

nokogiriやwebsocket-driverなどのnative extensionのコンパイルでstdio.hなどのヘッダファイルが見つからないということのようです。

参考: Go言語のテストでもエラー

このとき、go testでも同じくcgoでヘッダファイルが見つからないというエラーが発生していました。

go test -ldflags "-s -w" ./...
# runtime/cgo
_cgo_export.c:3:10: fatal error: 'stdlib.h' file not found

試してはいませんが、この他のC言語のコンパイル関連もおそらく軒並み同じような結果になるでしょう。

解決方法

最終的に以下のツイートのおかげで解決できました。ありがとうございます🙇。

具体的にはコマンドラインで以下を実行します(pkgをダブルクリックしても同じです)。インストーラが起動し、ヘッダファイルがインストールされます。

sudo open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg

これでbundle installが正常に動作するようになりました😊。

おそらくですが、Mojave用のCommandLineToolsがベータ版のため、インストーラからmacOS_SDK_headers_for_macOS_10.14.pkgが呼び出されていなかったのだと思います。通常はインストールすればヘッダファイルもインストールされます。

関連記事

[Rails 5] rbenvでRubyをインストールして新規Rails開発環境を準備する

macOSのアップデート失敗後にダウンロード前の状態に戻す

デザインも頼めるシステム開発会社をお探しならBPS株式会社までどうぞ 開発エンジニア積極採用中です! Ruby on Rails の開発なら実績豊富なBPS

この記事の著者

hachi8833

Twitter: @hachi8833、GitHub: @hachi8833 コボラー、ITコンサル、ローカライズ業界、Rails開発を経てTechRachoの編集・記事作成を担当。 これまでにRuby on Rails チュートリアル第2版の半分ほど、Railsガイドの初期翻訳ではほぼすべてを翻訳。その後も折に触れてそれぞれ一部を翻訳。 かと思うと、正規表現の粋を尽くした日本語エラーチェックサービス enno.jpを運営。 実は最近Go言語が好き。 仕事に関係ないすっとこブログ「あけてくれ」は2000年頃から多少の中断をはさんで継続、現在はnote.muに移転。

hachi8833の書いた記事

週刊Railsウォッチ

インフラ

ActiveSupport探訪シリーズ