Rails の bundle install 時に出会った therubyracer のエラーと、その解決方法

# `bundle install`  
とっても長いエラーがでました😱



```console:エラー内容
Building native extensions.  This could take a while...
ERROR:  Error installing therubyracer:
	ERROR: Failed to build gem native extension.
    current directory: /Users/koichi/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/therubyracer-0.12.3/ext/v8
/Users/koichi/.rbenv/versions/2.3.4/bin/ruby -r ./siteconf20200120-28144-771md0.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/koichi/.rbenv/versions/2.3.4/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/koichi/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/libv8-3.16.14.19/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.19 installed. You may need to use the 
--with-v8-dir option if it is installed in a non-standard location
	from /Users/koichi/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/libv8-3.16.14.19/lib/libv8.rb:7:in `configure_makefile'
	from extconf.rb:32:in `<main>'
To see why this extension failed to compile, please check the mkmf.log which can be found here:
  /Users/koichi/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-19/2.3.0-static/therubyracer-0.12.3/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /Users/koichi/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/therubyracer-0.12.3 for inspection.
Results logged to /Users/koichi/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-19/2.3.0-static/therubyracer-0.12.3/gem_make.out
```

このエラーを読んで、

- therubyracer でエラー
- libv8 も絡んでる
- option をつけろ

ということかと🤔



# 3つの手順で解決

## 1: V8 の 3.15 を入手
```console:
brew install v8@3.15
```


## 2: ローカルの設定ファイルへ書き込み
```console:
bundle config --local build.libv8 --with-system-v8
bundle config --local build.therubyracer --with-v8-dir=/usr/local/opt/v8@3.15
```
## 3: インストール
```console:
bundle install
```
これでうまくいきました👍🏼


# 解決方法にたどり着くまでに調べたこと
以下、まとまってないです． メモとして残してます



## [therubyracer](https://rubygems.org/gems/therubyracer/versions/0.9.7?locale=ja)
> Call JavaScript code and manipulate JavaScript objects from Ruby. Call Ruby code and manipulate Ruby objects from JavaScript.

- 2017年のver0.12.3で止まっている
- 依存関係： libv8 ~> 3.16.14.0 つまり 3.16.14.0 から 3.16.14.19 まで
- mini racer??



## libv8
> Distributes the V8 JavaScript engine in binary and source forms in order to support fast builds of The Ruby Racer

### バージョン履歴  
- 7.3.492.27.1 - July 22, 2019 universal-darwin19 (6.99MB)
- 7.3.492.27.1 - July 22, 2019 x86_64-darwin-19 (6.99MB)
- **3.16.14.19 - January 11, 2018 universal-darwin-<font color="Red">17</font> (2.36MB)**



## mini racer
> Minimal embedded v8 engine for Ruby  

[github/readme.md参照](https://github.com/rubyjs/mini_racer)

### 依存性
libv8 >= 6.9.411

余談
["~>1.0.1"という記法について](https://thoughtbot.com/blog/rubys-pessimistic-operator)

## Darwinって？
[Wikipedia]([https://ja.wikipedia.org/wiki/Darwin_(%E3%82%AA%E3%83%9A%E3%83%AC%E3%83%BC%E3%83%86%E3%82%A3%E3%83%B3%E3%82%B0%E3%82%B7%E3%82%B9%E3%83%86%E3%83%A0)])

> Darwin（ダーウィン）はアップルが開発するUnix系のPOSIX準拠オペレーティングシステム (OS) である。技術的にはNEXTSTEPからOPENSTEPに続く流れを汲み、Mach 3.0+BSDをベースとし、一部の機能は他のBSD系OSからも取り入れている。DarwinはmacOSやiOS、さらにはwatchOSとtvOSの基礎となる部分でもある。

### 自分のMac環境（2020-01-21現在）
- Darwin Kernel Version **<font color="Red">19</font>**.2.0

## V8 javascript engine
[V8とは](https://v8.dev/)
> V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++. It is used in Chrome and in Node.js, among others. It implements ECMAScript and WebAssembly, and runs on Windows 7 or later, macOS 10.12+, and Linux systems that use x64, IA-32, ARM, or MIPS processors. V8 can run standalone, or can be embedded into any C++ application.

[`brew install v8` の参考ページ](https://formulae.brew.sh/formula/v8)
### 疑問
```
v8 engine に ”3.15系統” と ”最新の７系統” がある？
```
### 自分のGoogle Chrome のバージョンをしらべるには？
[chromeバージョン確認ページ](chrome://version/page)
自分のChromeのバージョンを確認したところ↓
JavaScript: V8 7.9.317.32 

もう一度、[`brew install v8` の参考ページ](https://formulae.brew.sh/formula/v8)をよく見ると
[![Screenshot from Gyazo](https://gyazo.com/6e1cc33d97376042afbd12fe451b0d7c/raw)](https://gyazo.com/6e1cc33d97376042afbd12fe451b0d7c)
バージョン`3.15`は<font color="Red">open source</font>となってる
対して、`7.9`にはついていない
３系統だけopen source?



[libv8のreadme](https://www.rubydoc.info/gems/libv8/3.16.14.19)
ここに次のような記載があります
> **Versioning**
Versions of the libv8 gem track the version of v8 itself, adding its own point release after the main v8 version. So libv8 3.11.8.5 and 3.11.8.14 both correspond to v8 version 3.11.8. Another way to think about it would be that 3.11.8.14 is the 14th release of the libv8 rubygem based on v8 version 3.11.8

つまり、V8のバージョン番号がほぼそのままlibv8のバージョン番号になっているということ

```console:
% brew info v8
v8: stable 7.9.317.31 (bottled)
Google's JavaScript engine
https://github.com/v8/v8/wiki
/usr/local/Cellar/v8/7.9.317.31 (38 files, 38.9MB) *
  Poured from bottle on 2020-01-22 at 11:43:33
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/v8.rb
==> Dependencies
Build: ninja ✘
==> Requirements
Build: xcode ✔
==> Analytics
install: 2,444 (30 days), 10,549 (90 days), 38,848 (365 days)
install-on-request: 2,138 (30 days), 8,718 (90 days), 28,839 (365 days)
build-error: 0 (30 days)
```

### その他参考にしたサイト
https://github.com/rubyjs/therubyracer/issues/455

https://bundler.io/v1.16/bundle_config.html

https://docs.brew.sh/Bottles

https://formulae.brew.sh/api/formula/v8@3.15.json

https://github.com/rubyjs/libv8/issues/282

http://blog.cyclogy.com/2014/02/08/homebrew-versions/