therubyracerがinstallできない

はじめに・環境

このエラーについては様々な記事を読んだが、どれも自分に当てはまらないものが多かった。 libv8などのversionによっても解決方法が変わるため、あくまで参考程度に読んでいただくと良いかもしれません。


対象 Version
OS macOS Catalina version10.15.7
libv8 3.16.14.19
therubyracer 0.12.3
rails 5.2.3
ruby 2.6.5
bundler 2.0.2

therubyracerとは

therubyracerは、Javascriptの実行エンジンであるv8をrubyから叩くために導入するgem。
libv8は以下の様に、therubyracerとの依存関係があり、インストールされるようになっている。
Gemfile.lock

therubyracer (0.12.3)
      libv8 (~> 3.16.14.15)
      ref

経緯

まず、bundle install --vendor/bundleを実行した際に以下エラーが出現。

Fetching therubyracer 0.12.3
Installing therubyracer 0.12.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/myname/Desktop/runteq/advanced/workdir/vendor/bundle/ruby/2.6.0/gems/therubyracer-0.12.3/ext/v8
/Users/myname/.rbenv/versions/2.6.5/bin/ruby -I /Users/myname/.rbenv/versions/2.6.5/lib/ruby/2.6.0 -r ./siteconf20210123-53283-11tly30.rb extconf.rb
--with-v8-dir\=/usr/local/Cellar/v8/8.8.278.14
checking for -lpthread... yes
checking for -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/hamorotaiga/.rbenv/versions/2.6.5/bin/$(RUBY_BASE_NAME)
        --with-pthreadlib
        --without-pthreadlib
        --with-objclib
        --without-objclib
        --enable-debug
        --disable-debug
        --with-v8-dir
        --with-v8-include
        --without-v8-include=${v8-dir}/include
        --with-v8-lib
        --without-v8-lib=${v8-dir}/lib
/Users/myname/Desktop/workdir/vendor/bundle/ruby/2.6.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/myname/Desktop/workdir/vendor/bundle/ruby/2.6.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/myname/workdir/vendor/bundle/ruby/2.6.0/extensions/x86_64-darwin-19/2.6.0/therubyracer-0.12.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/myname/workdir/vendor/bundle/ruby/2.6.0/gems/therubyracer-0.12.3 for inspection.
Results logged to /Users/myname/workdir/vendor/bundle/ruby/2.6.0/extensions/x86_64-darwin-19/2.6.0/therubyracer-0.12.3/gem_make.out

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

In Gemfile:
  therubyracer

最後らへんに、

gem install therubyracer -v '0.12.3' --source 'https://rubygems.org/'

とあり、実行してもこんな感じのエラーが出る。

checking for -lpthread... yes
checking for -lobjc... yes
*** 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.

~中略~

extconf failed, exit code 1

~後略~

解決策

brew listを実行し、v8@3.15がインストールされているのを確認

autoconf                ghostscript             imagemagick             liblqr                  node                    pkg-config              shared-mime-info        yarn
composer                glib                    jpeg                    libomp                  oniguruma               postgresql              sqlite
docbook                 gnu-getopt              jq                      libpng                  openexr                 protobuf                v8@3.15
docbook-xsl             heroku                  krb5                    libtiff                 openjpeg                python@3.8              webp
freetype                heroku-node             libde265                libtool                 openssl@1.1             rbenv                   x265
gdbm                    icu4c                   libffi                  little-cms2             pcre                    readline                xmlto
gettext                 ilmbase                 libheif                 mysql                   phpmyadmin              ruby-build              xz

brew uninstall v8@3.15で、v8@3.15を一旦アンインストールする。
brew install v8@3.15で、再びインストールする。
④ libv8のパスの設定をする

bundle config --local build.libv8 --with-system-v8

⑤ therubyracerのパスの設定をする

myname@mbp workdir % 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 "--with-v8-dir=/usr/local/Cellar/v8/8.8.278.14"

which is currently のあとが nilになってしまう場合は失敗している可能性がある。

--with-v8-dir=$(brew --prefix v8-315)

このフラグは、brew installした際に格納される場所を返している。
⑥ これでbundle install --vendor/bundleを実行


以上、手順で解決。