xcrun: errorが原因でbundle updateに失敗する無限ループに陥った話

#目次
- はじめに
- 使用環境
- bundle updateが出来ない？
- gemが原因ではなかった
- macOSのバージョンアップが原因
- xcrun: errorへの対処法
- 終わりに

#はじめに
Railsチュートリアルを進めるべくローカル環境にて環境構築をして、Gemfileに記載したGemを更新($bundle update)したら、以下のようなエラーが出ました。

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

    current directory: /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/nio4r-2.5.7/ext/nio4r
/Users/yuuu/.rbenv/versions/2.6.6/bin/ruby -I /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/site_ruby/2.6.0 -r ./siteconf20210309-21181-1wgmv65.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/yuuu/.rbenv/versions/2.6.6/bin/$(RUBY_BASE_NAME)
/Users/yuuu/.rbenv/versions/2.6.6/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/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:601:in `try_cpp'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:1109:in `block in have_header'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:959:in `block in checking_for'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:361:in `block (2 levels) in postpone'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:331:in `open'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:361:in `block in postpone'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:331:in `open'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:357:in `postpone'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:958:in `checking_for'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:1108:in `have_header'
        from extconf.rb:15:in `<main>'

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

  /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-16/2.6.0/nio4r-2.5.7/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/nio4r-2.5.7 for inspection.
Results logged to /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-16/2.6.0/nio4r-2.5.7/gem_make.out

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

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


#使用環境
macOS BigSur 11.2.2
ruby 2.6.6
Rails 6.0.3
Visual Studio Code(エディタ)

#bundle updateが出来ない？
上記のエラー分からして、何やら、”nio4r”というGemをインストールしてくれ的なエラーが出ました。以下のコマンド

```
$gem install nio4r -v '2.5.7'
```
を実行しましたが、ダメでした。

Railsチュートリアル第3章に記載されているコマンド

```
$ bundle install --without production
```
を実行しても同様のエラーが出てダメでした、。

(※--without productionオプションを使って、production環境でしか使わないgemはインストールしないようにしました。)



#gemが原因ではなかった？
試しにサーバーを立ち上げてみようとすると、

```
$ rails s
Could not find gem 'sqlite3 (= 1.4.1)' in rubygems repository https://rubygems.org/ or installed locally.
The source contains the following versions of 'sqlite3': 1.4.2
Run `bundle install` to install missing gems.
```
どうやらGemのバージョンを書き換えて、$bundle install してくれとのことなのでやってみますが、

```
$  bundle install --without production
[DEPRECATED] The `--without` flag is deprecated because it relies on being remembered across bundler invocations, which bundler will no longer do in future versions. Instead please use `bundle config set --local without 'production'`, and stop using this flag
Fetching gem metadata from https://rubygems.org/............
Resolving dependencies...........
Using rake 12.3.3 (was 13.0.3)
Using concurrent-ruby 1.1.8
Using builder 3.2.4
Using erubi 1.10.0
Using racc 1.5.2
Using zeitwerk 2.4.2
Using rack 2.2.3
Using minitest 5.11.3 (was 5.14.4)
Using websocket-extensions 0.1.5
Using crass 1.0.6
Using mini_mime 1.0.2
Using thread_safe 0.3.6
Using mimemagic 0.3.5
Using bindex 0.8.1
Using msgpack 1.4.2
Using bundler 2.2.13
Using ansi 1.5.0
Using regexp_parser 1.8.2 (was 2.1.1)
Using public_suffix 4.0.6
Using coderay 1.1.3
Using ffi 1.14.2
Using formatador 0.2.5
Using childprocess 2.0.0 (was 3.0.0)
Using lumberjack 1.2.8
Using nenv 0.3.0
Fetching nio4r 2.5.7 (was 2.5.5)
Using rb-fsevent 0.10.4
Using method_source 1.0.0
Using shellany 0.0.1
Using thor 1.1.0
Using guard-compat 1.2.1
Using ruby-progressbar 1.11.0
Using rubyzip 1.3.0 (was 2.3.0)
Using tilt 2.0.10
Using spring 2.1.1
Using sqlite3 1.4.2
Using turbolinks-source 5.2.0
Using i18n 1.8.9
Using nokogiri 1.11.1 (x86_64-darwin)
Using rack-test 1.1.0
Using rack-proxy 0.6.5
Using sprockets 3.7.2 (was 4.0.2)
Using tzinfo 1.2.9
Using websocket-driver 0.7.3
Using marcel 0.3.3
Using mail 2.7.1
Using bootsnap 1.5.1 (was 1.7.2)
Using addressable 2.7.0
Using rb-inotify 0.10.1
Fetching byebug 11.0.1 (was 11.1.3)
Using activesupport 6.0.3 (was 6.0.3.5)
Using xpath 3.2.0
Using listen 3.4.1
Using notiffany 0.1.3
Using pry 0.14.0
Using guard-minitest 2.4.6
Using minitest-reporters 1.3.8
Using sass-listen 4.0.0
Using selenium-webdriver 3.142.4 (was 3.142.7)
Using turbolinks 5.2.0 (was 5.2.1)
Using loofah 2.9.0
Using globalid 0.4.2
Using activemodel 6.0.3 (was 6.0.3.5)
Using jbuilder 2.9.1 (was 2.11.2)
Using capybara 3.28.0 (was 3.35.3)
Using rails-dom-testing 2.0.3
Using guard 2.16.2
Using sass 3.7.4
Using webdrivers 4.1.2 (was 4.6.0)
Using rails-html-sanitizer 1.3.0
Using activejob 6.0.3 (was 6.0.3.5)
Using activerecord 6.0.3 (was 6.0.3.5)
Using spring-watcher-listen 2.0.1
Using actionview 6.0.3 (was 6.0.3.5)
Using actionpack 6.0.3 (was 6.0.3.5)
Using activestorage 6.0.3 (was 6.0.3.5)
Using actionmailer 6.0.3 (was 6.0.3.5)
Using railties 6.0.3 (was 6.0.3.5)
Using sprockets-rails 3.2.2
Using rails-controller-testing 1.0.4
Using actiontext 6.0.3 (was 6.0.3.5)
Using actionmailbox 6.0.3 (was 6.0.3.5)
Using web-console 4.0.1 (was 4.1.0)
Using webpacker 4.0.7 (was 4.3.0)
Using sass-rails 5.1.0 (was 6.0.0)
Installing byebug 11.0.1 (was 11.1.3) with native extensions
Installing nio4r 2.5.7 (was 2.5.5) with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/nio4r-2.5.7/ext/nio4r
/Users/yuuu/.rbenv/versions/2.6.6/bin/ruby -I /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/site_ruby/2.6.0 -r ./siteconf20210309-20830-j4owaa.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/yuuu/.rbenv/versions/2.6.6/bin/$(RUBY_BASE_NAME)
/Users/yuuu/.rbenv/versions/2.6.6/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/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:601:in `try_cpp'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:1109:in `block in have_header'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:959:in `block in checking_for'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:361:in `block (2 levels) in postpone'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:331:in `open'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:361:in `block in postpone'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:331:in `open'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:357:in `postpone'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:958:in `checking_for'
        from /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/2.6.0/mkmf.rb:1108:in `have_header'
        from extconf.rb:15:in `<main>'

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

  /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-16/2.6.0/nio4r-2.5.7/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/nio4r-2.5.7 for inspection.
Results logged to /Users/yuuu/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-16/2.6.0/nio4r-2.5.7/gem_make.out

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

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

また同じエラーが出ました、。(※これの繰り返しです)


#macOSのバージョンアップが原因
何度も同じところで立ち止まっていると、どうやらGemが原因で起きているエラーではないなと感じ始めます（遅い）

色々調べていると、以下の記事に辿り着きました。

<a href="https://qiita.com/ss_33_sss/items/7e026bbbc7e42db88da7" target="_blank">Gitコマンドでxcrun: errorが出た時の対処
</a>

どうやらmacOSを最近アップデート(macOS Sierra 10.12.6 → macOS BigSur 11.2.2)したことが原因で起きたxcrunエラーのようです。macをアップデートすることで起こるエラーのようですね。

#xcrun: errorへの対処法
Xcodeの開発者ツールが見当たらなくなってしまっていることが原因らしいので、ターミナルで以下を実行すると直りました。

```
$xcode-select --install
```

なお、インストールには時間がかかる（筆者の場合だと４時間ほどかかりました。。）場合があるので、インストールは作業のキリがいいタイミングか、作業終わりのタイミングがいいかと思います。

インストールが終わると、以下を実行しました。

```
$ xcode-select --reset
```

すると、以下のようにエラーが出ました。

```
xcode-select: error: --reset must be run as root (e.g. `sudo xcode-select --reset`).
```

なので、以下のように打ち直しました。

```
$ sudo xcode-select --reset
```

リセットが完了したので、$bundle update を実行すると無事成功しました。

#終わりに
OS由来のエラーは初学者にとってはかなり難敵ですが、しっかりとエラー文を読んで、尽くせる手は全てやってみるのが大事ですね。