macOS Mojaveで古いlibv8とtherubyracerが入らない時の対処法

## 概要

- macOS Mojaveにしたら `bundle install` でコケるようになった
  - `therubyracer` と依存する `libv8` という `gem` が入らない
- どうやら`v8`が合わないせい
  - 3系の `v8` をインストールしてそれでビルドすると通る
- バージョン上げられないと大変ね、というお話

## 環境

- MacBook Pro (13-inch, 2016, Four Thunderbolt 3 Ports)
- macOS Mojave バージョン 10.14
- Ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin18]
- RubyGems 2.4.5
- ruby -rubygems -e 'puts Gem::Platform.new(RUBY_PLATFORM)' => `x86_64-darwin-18`
- Bundler version 1.16.6
- Rails 4.2.1
- gem therubyracer (0.12.2)
- gem libv8 (~> 3.16.14.0)

## 対処法

Rails4のプロジェクトディレクトリにて

```shell
$ brew install v8-315
$ bundle config --local build.libv8 --with-system-v8
You are replacing the current local value of build.libv8, which is currently nil
$ bundle config --local build.therubyracer --with-v8-dir=$(brew --prefix v8-315)
You are replacing the current local value of build.therubyracer, which is currently nil
```
してから`bundle install`

これでもできなかったら
[「libv8とtherubyracerが入らない時の対処法」をやっても入らないとき](https://qiita.com/shunsuke1980/items/b16f3f87b6df4e9de1c1)
を試すと良いかもしれません @shunsuke1980++

## 起きたエラーと都度対処したログ

そのまま `bundle install` しようとすると

```shell
Fetching libv8 3.16.14.15
Installing libv8 3.16.14.15 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /Users/shimx/.rbenv/versions/2.2.1/bin/ruby -r ./siteconf20181012-66191-2n8ulx.rb extconf.rb --with-cxx=clang++
creating Makefile
Applying /Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/gems/libv8-3.16.14.15/patches/fPIC-for-static.patch
Applying /Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/gems/libv8-3.16.14.15/patches/do-not-imply-vfp3-and-armv7.patch
Applying /Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/gems/libv8-3.16.14.15/patches/disable-building-tests.patch
Applying /Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/gems/libv8-3.16.14.15/patches/disable-werror-on-osx.patch
Applying /Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/gems/libv8-3.16.14.15/patches/do-not-use-vfp2.patch
Compiling v8 for x64
Using python 2.7.15
Using compiler: clang++ (Apple LLVM version 10.0.0)
Unable to find a compiler officially supported by v8.
It is recommended to use GCC v4.4 or higher
Beginning compilation. This will take some time.
Building v8 with env CXX=clang++ LINK=clang++  /usr/bin/make x64.release ARFLAGS.target=crs werror=no
GYP_GENERATORS=make \
	build/gyp/gyp --generator-output="out" build/all.gyp \
	              -Ibuild/standalone.gypi --depth=. \
	              -Dv8_target_arch=x64 \
	              -S.x64  -Dv8_enable_backtrace=1 -Dv8_can_use_vfp2_instructions=true -Darm_fpu=vfpv2 -Dv8_can_use_vfp3_instructions=true -Darm_fpu=vfpv3 -Dwerror=''
  CXX(target) /Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/gems/libv8-3.16.14.15/vendor/v8/out/x64.release/obj.target/preparser_lib/src/allocation.o
warning: include path for stdlibc++ headers not found; pass '-std=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]
In file included from ../src/allocation.cc:33:
../src/utils.h:33:10: fatal error: 'climits' file not found
#include <climits>
         ^~~~~~~~~
1 warning and 1 error generated.
make[1]: *** [/Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/gems/libv8-3.16.14.15/vendor/v8/out/x64.release/obj.target/preparser_lib/src/allocation.o] Error 1
make: *** [x64.release] Error 2
/Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/gems/libv8-3.16.14.15/ext/libv8/location.rb:36:in `block in verify_installation!': libv8 did not install properly, expected binary v8 archive
'/Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/gems/libv8-3.16.14.15/vendor/v8/out/x64.release/obj.target/tools/gyp/libv8_base.a'to exist, but it was not found
(Libv8::Location::Vendor::ArchiveNotFound)
	from /Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/gems/libv8-3.16.14.15/ext/libv8/location.rb:35:in `each'
	from /Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/gems/libv8-3.16.14.15/ext/libv8/location.rb:35:in `verify_installation!'
	from /Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/gems/libv8-3.16.14.15/ext/libv8/location.rb:26:in `install!'
	from extconf.rb:7:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in /Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/gems/libv8-3.16.14.15 for inspection.
Results logged to /Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/extensions/x86_64-darwin-18/2.2.0-static/libv8-3.16.14.15/gem_make.out

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

In Gemfile:
  therubyracer was resolved to 0.12.2, which depends on
    libv8
```

とコケる
ここで`v8@315`を入れてシステムのv8を参照するように設定

```shell
$ brew install v8-315
$ bundle config --local build.libv8 --with-system-v8
You are replacing the current local value of build.libv8, which is currently nil
```

再び実行

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

    /Users/shimx/.rbenv/versions/2.2.1/bin/ruby -r ./siteconf20181012-92599-p7s45t.rb extconf.rb
checking for main() in -lpthread... yes
checking for main() in -lobjc... yes
checking for v8.h... no
*** 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/shimx/.rbenv/versions/2.2.1/bin/$(RUBY_BASE_NAME)
	--with-pthreadlib
	--without-pthreadlib
	--with-objclib
	--without-objclib
	--enable-debug
	--disable-debug
	--with-v8-dir
	--without-v8-dir
	--with-v8-include
	--without-v8-include=${v8-dir}/include
	--with-v8-lib
	--without-v8-lib=${v8-dir}/lib
/Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/gems/libv8-3.16.14.15/ext/libv8/location.rb:50:in `configure': By using --with-system-v8, you have chosen to use the version
(Libv8::Location::System::NotFoundError)
of V8 found on your system and *not* the one that is bundled with
the libv8 rubygem.

However, your system version of v8 could not be located.

Please make sure your system version of v8 that is compatible
with 3.16.14.15 installed. You may need to use the
--with-v8-dir option if it is installed in a non-standard location
	from /Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/gems/libv8-3.16.14.15/lib/libv8.rb:7:in `configure_makefile'
	from extconf.rb:32:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in /Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/gems/therubyracer-0.12.2 for inspection.
Results logged to /Users/shimx/.ghq/github.com/shimx/rails4_2_project/vendor/bundle/ruby/2.2.0/extensions/x86_64-darwin-18/2.2.0-static/therubyracer-0.12.2/gem_make.out

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

In Gemfile:
  therubyracer
```

まだ`therubyracer`がコケるので

```shell
$ bundle config --local build.therubyracer --with-v8-dir=$(brew --prefix v8-315)
```

とビルド時に`v8@315`を参照するように設定
再び`bundle install`をしたら通った

## 参照

[Railsの難所についてと、therubyracerはすでに終わっていた話](https://note.mu/konpyu/n/na213a14ad1e3)
[therubyracer・nokogiri・libv8の3大つまづくgem](https://blog.skylarking.me/2016/12/09/nokogiri_therubyracer_libv8_error/)
[Installing libv8 gem on OS X 10.9+](https://stackoverflow.com/questions/19577759/installing-libv8-gem-on-os-x-10-9)
[El Capitanでv8-315を使ってtherubyracerをインストールする方法](https://qiita.com/akm/items/02c104fc3d39d68f96c7)