gem install rails でエラーが発生した時の対処法

本投稿は、Mac環境で ``gem install rails`` を実行した際にエラーが発生したときに自分が対応した内容を備忘録として、まとめたものです。

## 環境

| 項目 | バージョン |
| :--- | :--- |
| macOS Mojave | 10.14.5 |

## 発生したエラー

自信のMac環境で railsの環境をセットアップしていたところ以下のようなエラーが発生しました。

```bash:発生したエラー
$ gem install rails -v 5.2.1
Building native extensions. This could take a while...
ERROR:  Error installing rails:
	ERROR: Failed to build gem native extension.

    current directory: /usr/local/lib/ruby/gems/2.5.0/gems/nokogiri-1.10.3/ext/nokogiri
/usr/local/opt/ruby/bin/ruby -I /usr/local/lib/ruby/site_ruby/2.5.0 -r ./siteconf20190624-15093-apcrqk.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
	--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=/usr/local/Cellar/ruby/2.5.1/bin/$(RUBY_BASE_NAME)
	--help
	--clean
/usr/local/Cellar/ruby/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 /usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/mkmf.rb:574:in `block in try_compile'
	from /usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/mkmf.rb:521:in `with_werror'
	from /usr/local/Cellar/ruby/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 /usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/mkmf.rb:632: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:

  /usr/local/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-17/2.5.0/nokogiri-1.10.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /usr/local/lib/ruby/gems/2.5.0/gems/nokogiri-1.10.3 for inspection.
Results logged to /usr/local/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-17/2.5.0/nokogiri-1.10.3/gem_make.out
```

## 対処した方法

調査した結果、``xcode-select --install`` を実行することで、解消できることがわかり実際に実行してみました。
その結果以下の成功した結果のように無事に railsをインストールすることができました。

```bash:対応したコマンド
$ xcode-select --install
xcode-select: note: install requested for command line developer tools
```

```bash:成功した内容
$ gem install rails -v 5.2.1
Building native extensions. This could take a while...
Successfully installed nokogiri-1.10.3
Successfully installed rails-dom-testing-2.0.3
.
.
.
Done installing documentation for nokogiri, rails-dom-testing, crass, loofah, rails-html-sanitizer, erubi, builder, actionview, rack, rack-test, actionpack, sprockets, sprockets-rails, method_source, thor, railties, mimemagic, marcel, arel, activemodel, activerecord, activestorage, websocket-extensions, websocket-driver, nio4r, actioncable, globalid, activejob, mini_mime, mail, actionmailer, rails after 35 seconds
32 gems installed
```