STUDY MEMO

勉強した内容をアウトプットしていきます。

<Ruby on Rails エラー編> bundle installが失敗した時の対処 〜libv8, mysql2, therubyracer〜

前提

Mac RetinaMySQL Ver 14.14 Distrib 5.7.32 ・Redis 3.2.9 ・Node.js 12.14.0 ・Yarn 1.22.10

# Gemfile
source 'https://rubygems.org'
ruby '2.6.5'

gem 'rails', '5.2.3'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'therubyracer'

# Assets
gem 'bootstrap-sass'
gem 'font-awesome-rails'
gem 'jquery-rails'
gem 'sass-rails'
gem 'uglifier'
gem 'webpacker'

# UI/UX
gem 'rails-i18n', '~> 5.0.0'
# gem 'turbolinks'
gem 'jbuilder'
gem 'meta-tags'
gem 'slim-rails'

# Authentication
gem 'pundit'
gem 'sorcery'

# Configuration
gem 'config'
gem 'dotenv-rails', require: 'dotenv/rails-now'

# Database
gem 'mysql2'
gem 'redis-rails'

# Seeds
gem 'seed-fu'

# Pagination
gem 'bootstrap4-kaminari-views'
gem 'kaminari'
gem 'kaminari-i18n'

# Breadcrumbs
gem 'gretel'

# Form
gem 'cocoon'
gem 'simple_form'

# Soft delete
gem 'paranoia'
gem 'paranoia_uniqueness_validator'

# Model
gem 'active_hash'
gem 'enum_help'

# Validation
gem 'validate_url'
gem 'validates_email_format_of'

# Decorator
gem 'active_decorator'

# Storage
gem 'aws-sdk-s3', require: false
gem 'mini_magick'

# Application server
gem 'puma'

# Background Job
gem 'resque'
gem 'whenever', require: false

# Debugger
group :development do
  gem 'listen'
  gem 'web-console'
end

group :development, :test do
  gem 'sqlite3'

  # Email
  gem 'letter_opener_web'

  # CLI
  gem 'spring'
  gem 'spring-commands-rspec'

  # Test
  gem 'factory_bot_rails'
  gem 'rspec-rails'
  gem 'simplecov', require: false

  # Code analyze
  gem 'brakeman', require: false
  gem 'bullet'
  gem 'coffeelint'
  gem 'rails_best_practices'
  gem 'reek'
  gem 'rspec_junit_formatter'
  gem 'rubocop'
  gem 'rubocop-checkstyle_formatter'
  gem 'rubocop-rails'
  gem 'scss_lint', require: false
  gem 'slim_lint'

  # Debugger
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'byebug'
  gem 'pry'
  gem 'pry-byebug'
  gem 'pry-doc'
  gem 'pry-rails'

  # Print debug
  gem 'awesome_print'
  gem 'tapp'

  # Table/Schema
  gem 'annotate', github: 'ctran/annotate_models', branch: 'develop'
  gem 'migration_comments'

  # Deploy
  gem 'capistrano', '3.9.0'
  gem 'capistrano-bundler'
  gem 'capistrano-rails'
  gem 'capistrano-rails-console'
  gem 'capistrano-rbenv'
  gem 'capistrano-resque', require: false
  gem 'capistrano3-puma'
end

group :test do
  gem 'capybara'
  gem 'faker'
  gem 'fuubar'
  gem 'shoulda-matchers'
  gem 'timecop'
  gem 'webdrivers'
end

libv8エラー

$ bundle install
...省略...
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/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/libv8-3.16.14.19/ext/libv8
/Users/ユーザー名/.rbenv/versions/2.6.5/bin/ruby -I /Users/ユーザー名/.rbenv/versions/2.6.5/lib/ruby/2.6.0 -r
./siteconf20210127-3572-1g6465c.rb extconf.rb
creating Makefile
Applying
/Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/libv8-3.16.14.19/patches/disable-building-tests.patch
Applying
/Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/libv8-3.16.14.19/patches/disable-werror-on-osx.patch
Applying
/Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/libv8-3.16.14.19/patches/disable-xcode-debugging.patch
Applying
/Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/libv8-3.16.14.19/patches/do-not-imply-vfp3-and-armv7.patch
Applying
/Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/libv8-3.16.14.19/patches/do-not-use-MAP_NORESERVE-on-freebsd.patch
Applying
/Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/libv8-3.16.14.19/patches/do-not-use-vfp2.patch
Applying
/Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/libv8-3.16.14.19/patches/fPIC-for-static.patch
Compiling v8 for x64
Using python 2.7.16
Using compiler: c++ (clang version 12.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/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/libv8-3.16.14.19/vendor/v8/out/x64.release/obj.target/preparser_lib/src/allocation.o
clang: warning: include path for libstdc++ 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 <climits>
         ^~~~~~~~~
1 error generated.
make[1]: ***
[/Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.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/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.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/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.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/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/libv8-3.16.14.19/ext/libv8/location.rb:35:in
`each'
from
/Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/libv8-3.16.14.19/ext/libv8/location.rb:35:in
`verify_installation!'
from
/Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/libv8-3.16.14.19/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/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/libv8-3.16.14.19 for
inspection.
Results logged to
/Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/extensions/x86_64-darwin-20/2.6.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

対処法

$ 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 <name> <value>

は、ローカルのアプリケーションに対して設定を行う。この設定は、app/.bundle/configに格納される。

ビルドオプション(build.)はbundle configを使用してbundlerへフラグを指定する。
特定のGemのインストールを行う度にそのフラグをgemインストーラーに渡す。

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

このコマンドの実行後に、bundlerがGemのインストールを必要とすると、この指定に沿ってフラグが渡される。

参考文献

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

bundle config | Bundler日本語ドキュメント | Ruby STUDIO

mysql2エラー

$ bundle
...省略...
Fetching mysql2 0.5.3
Installing mysql2 0.5.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.3/ext/mysql2
/Users/ユーザー名/.rbenv/versions/2.6.5/bin/ruby -I /Users/ユーザー名/.rbenv/versions/2.6.5/lib/ruby/2.6.0 -r ./siteconf20210127-35425-nrmtfm.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
-----
Using mysql_config at /usr/local/opt/mysql@5.7/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in mysql.h... yes
checking for MYSQL.net.vio in mysql.h... yes
checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes
checking for my_bool in mysql.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/opt/mysql@5.7/lib
-----
creating Makefile

current directory: /Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.3 for inspection.
Results logged to
/Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/extensions/x86_64-darwin-20/2.6.0/mysql2-0.5.3/gem_make.out

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

In Gemfile:
  mysql2

対処法

$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib"

参考文献

mysql2がbundle installエラーになるときの解決方法 - Qiita bundle config | Bundler日本語ドキュメント | Ruby STUDIO

therubyracerエラー

$ 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/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/therubyracer-0.12.3/ext/v8
/Users/ユーザー名/.rbenv/versions/2.6.5/bin/ruby -I /Users/ユーザー名/.rbenv/versions/2.6.5/lib/ruby/2.6.0 -r ./siteconf20210127-36002-1qz94cy.rb extconf.rb
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/ユーザー名/.rbenv/versions/2.6.5/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/ユーザー名/workspace/runteq_learning_advanced/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/ユーザー名/workspace/runteq_learning_advanced/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/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/extensions/x86_64-darwin-20/2.6.0/therubyracer-0.12.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/gems/therubyracer-0.12.3 for inspection.
Results logged to
/Users/ユーザー名/workspace/runteq_learning_advanced/vendor/bundle/ruby/2.6.0/extensions/x86_64-darwin-20/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

対処法

$ bundle config --local build.therubyracer --with-v8-dir=/usr/local/opt/v8@3.15
You are replacing the current local value of build.therubyracer, which is currently nil

参考文献

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

最終的な.bundle/configの中身

BUNDLE_PATH: "vendor/bundle"
BUNDLE_BUILD__LIBV8: "--with-system-v8"
BUNDLE_BUILD__MYSQL2: "--with-ldflags=-L/usr/local/opt/openssl/lib"
BUNDLE_BUILD__THERUBYRACER: "--with-v8-dir=/usr/local/opt/v8@3.15"