libv8というgemのインストールが失敗する

エラー内容 

Ruby3系のバージョンで作られたRailsのプロジェクトを動かしたかったので、

環境構築後bundle installをしたら以下のようなエラーが出てしまいました。。。

libv8 というgemのコンパイルで失敗しているようでした。 

 

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

current directory: /Users/hayashiyoshino/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/libv8-3.16.14.15/ext/libv8
/Users/hayashiyoshino/.rbenv/versions/2.3.0/bin/ruby -r ./siteconf20190515-89699-1kv0iuy.rb extconf.rb
creating Makefile
Applying /Users/hayashiyoshino/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/libv8-3.16.14.15/patches/fPIC-for-static.patch
Applying /Users/hayashiyoshino/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/libv8-3.16.14.15/patches/do-not-imply-vfp3-and-armv7.patch
Applying /Users/hayashiyoshino/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/libv8-3.16.14.15/patches/disable-building-tests.patch
Applying /Users/hayashiyoshino/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/libv8-3.16.14.15/patches/disable-werror-on-osx.patch
Applying /Users/hayashiyoshino/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/libv8-3.16.14.15/patches/do-not-use-vfp2.patch
Compiling v8 for x64
Using python 2.7.10
Using compiler: c++ (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=c++ LINK=c++ /usr/local/bin/gmake 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=''
gmake[1]: Entering directory '/Users/hayashiyoshino/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/libv8-3.16.14.15/vendor/v8/out'
CXX(target)
/Users/hayashiyoshino/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.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.
gmake[1]: *** [tools/gyp/preparser_lib.target.x64.mk:126:
/Users/hayashiyoshino/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/libv8-3.16.14.15/vendor/v8/out/x64.release/obj.target/preparser_lib/src/allocation.o]
Error 1
gmake[1]: Leaving directory '/Users/hayashiyoshino/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/libv8-3.16.14.15/vendor/v8/out'
gmake: *** [Makefile:195: x64.release] Error 2
/Users/hayashiyoshino/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.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/hayashiyoshino/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.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/hayashiyoshino/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/libv8-3.16.14.15/ext/libv8/location.rb:35:in `each'
from /Users/hayashiyoshino/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/libv8-3.16.14.15/ext/libv8/location.rb:35:in
`verify_installation!'
from /Users/hayashiyoshino/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.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/hayashiyoshino/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/libv8-3.16.14.15 for inspection.
Results logged to
/Users/hayashiyoshino/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-18/2.3.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

 

解決方法 

どうやらMac OS Mojaveだとlibv8というgemのビルドに失敗してしまうようです。

libv8はtherubyracerというgemに関連して必要なgemのようです。

github.com

 

therubyracerはV8というGoogleが開発するJavaScript実行エンジンをRubyで使えるようにしてくれるもののようです。

 

3系のv8を入れてあげたのち、それをビルドするとlibv8もインストールできるようになるという情報があったので、まず3系のv8をインストールします。

(develop|✚1) [1] $ brew install v8-315
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 3 taps (homebrew/core, homebrew/cask and homebrew/services).
==> New Formulae
allureofthestars cfn-lint cypher-shell ktlint pipx
appstream-glib cpp-gsl gcc@8 leela-zero proteinortho
boringtun cubelib itk otf2 scws
==> Updated Formulae
imagemagick ✔ grpc netcdf

  略

==> Deleted Formulae
js-test-driver minisat node@6 typesafe-activator

==> Downloading https://homebrew.bintray.com/bottles/v8@3.15-3.15.11.18_1.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/38/38ef56c652ac4f91ec1ddac61b8719ffba4c949103a6636e15aa1cd768e2d14d?__gda__=exp=1
######################################################################## 100.0%
==> Pouring v8@3.15-3.15.11.18_1.mojave.bottle.tar.gz
==> Caveats
v8@3.15 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have v8@3.15 first in your PATH run:
echo 'export PATH="/usr/local/opt/v8@3.15/bin:$PATH"' >> ~/.bash_profile

For compilers to find v8@3.15 you may need to set:
export LDFLAGS="-L/usr/local/opt/v8@3.15/lib"
export CPPFLAGS="-I/usr/local/opt/v8@3.15/include"

==> Summary
🍺 /usr/local/Cellar/v8@3.15/3.15.11.18_1: 22 files, 152MB

 

システムのv8を参照するように設定します。

(develop|✚1) $ bundle config --local build.libv8 --with-system-v8
You are replacing the current local value of build.libv8, which is currently nil

 

 ビルド時に3系のv8を参照するように設定します。

(develop|✚1) $ 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をしてあげると無事インストールされるようになりました!!

、、、となるはずでしたが、今度はmysqlの部分でコケましたw

 

mysqlも入るように修正したら今度こそ全てのgemがインストールされました!!

 

 

 

感想その他

ビルドやら環境によるエラーって難しいですねw 

https://qiita.com/shimx/items/32e85093f21e673c7127 この記事書いてくれた方に感謝です!!