【Rails】bundle installするとlibv8というgemでコケる

バックエンド

Railsプロジェクトでbundle installすると以下のエラーになりました。どうやらtherubyracerというgemが依存しているlibv8でエラーが起きているようです。

Fetching libv8 3.16.14.19
 Installing libv8 3.16.14.19 with native extensions
 Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
 current directory: /Users/me/Documents/sample/vendor/gems/ruby/2.4.0/gems/libv8-3.16.14.19/ext/libv8
 /Users/me/.rbenv/versions/2.4.0/bin/ruby -r ./siteconf20200126-98789-1gtp8r5.rb extconf.rb
 creating Makefile
 Applying /Users/me/Documents/sample/vendor/gems/ruby/2.4.0/gems/libv8-3.16.14.19/patches/disable-building-tests.patch
 Applying /Users/me/Documents/sample/vendor/gems/ruby/2.4.0/gems/libv8-3.16.14.19/patches/disable-werror-on-osx.patch
 Applying /Users/me/Documents/sample/vendor/gems/ruby/2.4.0/gems/libv8-3.16.14.19/patches/disable-xcode-debugging.patch
 Applying /Users/me/Documents/sample/vendor/gems/ruby/2.4.0/gems/libv8-3.16.14.19/patches/do-not-imply-vfp3-and-armv7.patch
 Applying /Users/me/Documents/sample/vendor/gems/ruby/2.4.0/gems/libv8-3.16.14.19/patches/do-not-use-MAP_NORESERVE-on-freebsd.patch
 Applying /Users/me/Documents/sample/vendor/gems/ruby/2.4.0/gems/libv8-3.16.14.19/patches/do-not-use-vfp2.patch
 Applying /Users/me/Documents/sample/vendor/gems/ruby/2.4.0/gems/libv8-3.16.14.19/patches/fPIC-for-static.patch
 Compiling v8 for x64
 Using python 2.7.17
 Using compiler: c++ (clang version 11.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/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/me/Documents/sample/vendor/gems/ruby/2.4.0/gems/libv8-3.16.14.19/vendor/v8/out/x64.release/obj.target/preparser_lib/src/allocation.o
 warning: include path for stdlibc++ headers not found; pass '-stdlib=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 
      ^~~~~~~~~
 1 warning and 1 error generated.
 make[1]: ***
 [/Users/me/Documents/sample/vendor/gems/ruby/2.4.0/gems/libv8-3.16.14.19/vendor/v8/out/x64.release/obj.target/preparser_lib/src/allocation.o] Error 1
 make: *** [x64.release] Error 2
 /Users/me/Documents/sample/vendor/gems/ruby/2.4.0/gems/libv8-3.16.14.19/ext/libv8/location.rb:36:in block in verify_installation!': libv8 did not install properly, expected binary v8 archive '/Users/me/Documents/sample/vendor/gems/ruby/2.4.0/gems/libv8-3.16.14.19/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/me/Documents/sample/vendor/gems/ruby/2.4.0/gems/libv8-3.16.14.19/ext/libv8/location.rb:35:ineach'
         from /Users/me/Documents/sample/vendor/gems/ruby/2.4.0/gems/libv8-3.16.14.19/ext/libv8/location.rb:35:in verify_installation!'         from /Users/me/Documents/sample/vendor/gems/ruby/2.4.0/gems/libv8-3.16.14.19/ext/libv8/location.rb:26:ininstall!'
         from extconf.rb:7:in `
'
 extconf failed, exit code 1
 Gem files will remain installed in /Users/me/Documents/sample/vendor/gems/ruby/2.4.0/gems/libv8-3.16.14.19 for inspection.
 Results logged to /Users/me/Documents/sample/vendor/gems/ruby/2.4.0/extensions/x86_64-darwin-19/2.4.0/libv8-3.16.14.19/gem_make.out
 An error occurred while installing libv8 (3.16.14.19), and Bundler cannot continue.
 Make sure that gem install libv8 -v '3.16.14.19' --source 'https://rubygems.org/' succeeds before bundling.
 In Gemfile:
   therubyracer was resolved to 0.12.3, which depends on
     libv8

昨日まではエラーが起きなかったのですが、Ruby2.4.0を再インストールしたことが原因かもしれません。ネットの対処法にかかれている、libv8の古いバージョンをインストールするなどを試しましたが変わらず。

そもそも、therubyracerって何…?調べてみると、サーバ上でjavascriptを実行するためのgemらしいですが、何に使っているのか不明です。ためしにgemをコメントアウトすると、もちろんbundle installは通りました。Railsも起動できて動いているようです。

「開発環境はこれででいいや、gemfileをコミットしなきゃ影響ないし」と思っていたのですが、以下の記事を発見。

asset pipelineに使っているのだとか。開発環境ってasset pipelineは実行されないんだっけ…?でもやりたい時も出そうなのでずっとこのままはまずいかもなぁと思っているところです。

こんな記事も見つけました。therubyracerはハメgemか…。
Railsの難所についてと、therubyracerはすでに終わっていた話

解決法が見つかったら追記します。

解決した方法

こちらのサイトを参考にさせていただきました。
macOS Mojaveで古いlibv8とtherubyracerが入らない時の対処法

以下のコマンドを入力。

$ bundle config --local build.libv8 --with-system-v8 You are replacing the current $ bundle config --local build.therubyracer --with-v8-dir=$(brew --prefix v8-315)

すると、.bundle/config に以下が追記されます。

BUNDLE_BUILD__LIBV8: "--with-system-v8"
BUNDLE_BUILD__THERUBYRACER: "--with-v8-dir=/usr/local/opt/v8@3.15"

これで動くようになりました。

タイトルとURLをコピーしました