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

新しい端末で新しい環境を構築していく際に`eventmachine`というGemでタイトルのエラーが出て約２日悩まされたのでまとめていきます。
ちなみに今までOSアップデートでmacOSを使っていたためこの手の問題は無かったです。

## 環境

MacBookPro Mojave - macOS 10.14.6

## 事象

```
$ bundle install

...

Installing eventmachine 1.2.7 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /private/var/folders/p8/37pd2jf119jd4tlmf0nm85kw0000gn/T/bundler20191213-83767-sh7gkueventmachine-1.2.7/gems/eventmachine-1.2.7/ext
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby -r ./siteconf20191213-83767-1nxkkq.rb extconf.rb
mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/include/ruby.h

extconf failed, exit code 1

Gem files will remain installed in /var/folders/p8/37pd2jf119jd4tlmf0nm85kw0000gn/T/bundler20191213-83767-sh7gkueventmachine-1.2.7/gems/eventmachine-1.2.7 for inspection.
Results logged to /var/folders/p8/37pd2jf119jd4tlmf0nm85kw0000gn/T/bundler20191213-83767-sh7gkueventmachine-1.2.7/extensions/universal-darwin-18/2.3.0/eventmachine-1.2.7/gem_make.out

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

In Gemfile:
  appium_lib was resolved to 9.16.1, which depends on
    appium_lib_core was resolved to 2.2.0, which depends on
      faye-websocket was resolved to 0.10.7, which depends on
        eventmachine

$ sudo gem install eventmachine
Building native extensions.  This could take a while...
ERROR:  Error installing eventmachine:
	ERROR: Failed to build gem native extension.

    current directory: /Library/Ruby/Gems/2.3.0/gems/eventmachine-1.2.7/ext
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby -r ./siteconf20191213-83820-1pf9cvb.rb extconf.rb
mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/include/ruby.h

extconf failed, exit code 1

Gem files will remain installed in /Library/Ruby/Gems/2.3.0/gems/eventmachine-1.2.7 for inspection.
Results logged to /Library/Ruby/Gems/2.3.0/extensions/universal-darwin-18/2.3.0/eventmachine-1.2.7/gem_make.out

```

## 一度消して単体で入れ直してみる

それでも上手く入りませんでした。

```
$ sudo rm -fr /Library/Ruby/Gems/2.3.0/gems/eventmachine-1.2.7

$ sudo gem install eventmachine
Building native extensions.  This could take a while...
ERROR:  Error installing eventmachine:
	ERROR: Failed to build gem native extension.

    current directory: /Library/Ruby/Gems/2.3.0/gems/eventmachine-1.2.7/ext
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby -r ./siteconf20191213-83844-1xn766m.rb extconf.rb
mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/include/ruby.h

extconf failed, exit code 1

Gem files will remain installed in /Library/Ruby/Gems/2.3.0/gems/eventmachine-1.2.7 for inspection.
Results logged to /Library/Ruby/Gems/2.3.0/extensions/universal-darwin-18/2.3.0/eventmachine-1.2.7/gem_make.out
```



## 解決方法

ググってみるとdocker界隈でも似たような現象があったようです。
[Can't run docker-sync after upgrading to Xcode 11 #679](https://github.com/EugenMayer/docker-sync/issues/679)を参考にすると[AdamBDがMojaveユーザーはこれで直る](https://github.com/EugenMayer/docker-sync/issues/679#issuecomment-534941906)と言っていたので試したところ上手くいきました。

ちなみにXcodeをインストールしたのちにCommandLineToolsを公式からインストールしていましたが、それで上記の状況だったのでCLIでの対応が必要なようです。

```
$ sudo rm -rf /Library/Developer/CommandLineTools
$ xcode-select --install
$ sudo xcodebuild -license accept
$ open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg 

$ sudo gem install eventmachine
Building native extensions.  This could take a while...
Successfully installed eventmachine-1.2.7
Parsing documentation for eventmachine-1.2.7
Installing ri documentation for eventmachine-1.2.7
Done installing documentation for eventmachine after 4 seconds
1 gem installed
```