therubyracer・nokogiri・libv8の3大つまづくgem

bundle install一筋縄で行かない3大Gemとして(私の中で)おなじみのnokogiri, libv8, therubyracerで躓いた際のメモです。

nokogiriでつまづいた事例

libxml2でつまづく事が多い印象。

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
(略)
Running 'install' for libxml2 2.9.2... ERROR, 
(略)
*** 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=/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
  --help
  --clean
  --use-system-libraries
  --enable-static
  --disable-static
  --with-zlib-dir
  --without-zlib-dir
  --with-zlib-include
  --without-zlib-include=${zlib-dir}/include
  --with-zlib-lib
  --without-zlib-lib=${zlib-dir}/lib
  --enable-cross-build
  --disable-cross-build
/Users/hoge/hoge_project/vendor/bundle/ruby/2.0.0/gems/mini_portile2-2.0.0/lib/mini_portile2/mini_portile.rb:366:in `block in execute': Failed to complete install task (RuntimeError)
()
extconf failed, exit code 1

Gem files will remain installed in /Users/hoge/hoge_project/vendor/bundle/ruby/2.0.0/gems/nokogiri-1.6.7.2 for inspection.
Results logged to /Users/hoge/hoge_project/vendor/bundle/ruby/2.0.0/extensions/universal-darwin-13/2.0.0/nokogiri-1.6.7.2/gem_make.out
An error occurred while installing nokogiri (1.6.7.2), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.6.7.2'` succeeds before bundling.

libxml2のインストールでエラーが起きているけど、そもそもlibxml2は存在する

% brew info libxml2
libxml2: stable 2.9.4 (bottled), HEAD [keg-only]
(以下略)

システムのものを使ってもらおうという事で、nokogiriインストールの際にlibxml2の場所を指定してみる

% bundle config build.nokogiri --use-system-libraries --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib/

こちらで解決。同様にnokogumboでも同じようにlibxml2のエラーが出た場合、build.nokogumboで同じ内容を追加すれば大丈夫。

therubyracer, libv8でつまづいた事例

ruby2.2で、以下のようなメッセージが登場。

/vendor/bundle/gems/libv8-3.16.14.13/ext/libv8/location.rb:50:in `configure': You have
chosen to use the version of V8 found on your system (Libv8::Location::System::NotFoundError)
and *not* the one that is bundle with the libv8 rubygem. However,
it could not be located. please make sure you have a version of
v8 that is compatible with 3.16.14.13 installed. You may
need to special --with-v8-dir options if it is in a non-standard
location

thanks,
The Mgmt

との事なので–with-v8-dirについて調べた。

v8とは

GoogleのオープンソースハイパフォーマンスJavaScriptエンジンである。
therubyracerはこのv8をrubyから使えるようにしているgemだそうな。

この辺のインストールで、環境によってバージョンが違う問題があり、ググれども同じ問題で躓いている人は沢山見つかるが、その方法を試しても自分の環境ではうまく行かないぞ!という事態が起きたりする。

このあたりにつまづいたお友達は誰もが一度は「therubyracer 0.11.0問題まとめ」を読んだ事があると思う。
「応援とフィードバックは大事」という事で記事のご紹介ついでに厚い御礼を申し上げたい。とてもわかりやすかったです。ありがとうございます。

therubyracerとlibv8はマシンによってバージョンが異なる

色々やってみてるけど全然入らないというお友達は自分の環境をチェックして、入れるべきバージョンを確かめよう。

自分の環境の確かめ方は以下

% ruby -rubygems -e 'puts Gem::Platform.new(RUBY_PLATFORM)'

私はx86_64-darwin_13なので、以下のコマンドでどのバージョンを入れるべきか確認。

therubyracerのgemリスト

% gem list -rd '^therubyracer$'

*** REMOTE GEMS ***

therubyracer (0.12.3, 0.5.2, 0.5.1, 0.5.0)
    Platforms:
        0.5.0: x86-linux, x86_64-linux
        0.5.1: x86-darwin-9
        0.5.2: x86-darwin-10
        0.12.3: ruby
    Author: Charles Lowell
    Homepage: http://github.com/cowboyd/therubyracer

    Embed the V8 JavaScript interpreter into Ruby

% gem list -rd '^libv8$'

*** REMOTE GEMS ***

libv8 (5.3.332.38.3, 5.3.332.38.1, 3.16.14.17, 3.16.14.15, 3.16.14.7, 3.16.14.5, 3.16.14.3, 3.16.14.1, 3.11.8.13, 3.11.8.3, 3.3.10.4)
    Platforms:
        3.3.10.4: amd64-freebsd-8, x86-darwin-10, x86-darwin-11
        3.11.8.3: x86-freebsd-9
        3.11.8.13: x86_64-darwin-11
        3.16.14.1: x86_64-darwin-10
        3.16.14.3: amd64-freebsd-9, x86_64-darwin-12, x86_64-solaris-2.11
        3.16.14.5: armv6l-linux
        3.16.14.7: x86_64-darwin-13
        3.16.14.15: universal.x86_64-darwin-13, universal.x86_64-darwin-14
        3.16.14.17: universal.x86_64-darwin-15, universal.x86_64-darwin-16
        5.3.332.38.1: x86_64-darwin-14
        5.3.332.38.3: amd64-freebsd-10, amd64-freebsd-11, arm-linux, ruby,
                      universal-darwin-14, universal-darwin-15, universal-darwin-16,
                      x86-linux, x86_64-darwin-15, x86_64-darwin-16, x86_64-linux
    Author: Charles Lowell
    Homepage: http://github.com/cowboyd/libv8

    Distribution of the V8 JavaScript engine

therubyracerはよくわからないが(結果0.12.3で動いた。)libv8は3.16.14.15でOKのはず。
やだ、私のバージョンどうなってるのかしら…と不安になったらgem list {gem}で自分の環境に入っているバージョンを確かめよう。

いざインストール

色々試してみたけれども、一番安定している方法っぽい「brewでv8をインストールしておいて、optionでそちらを使ってもらうように指定する」というやり方でいく事にする。

nativeのv8をインストールし、gemで使ってもらう

% brew install v8
% bundle config --local build.libv8 --with-system-v8
% bundle config --local build.therubyracer --with-v8-dir=$(brew --prefix v8)

こんなかんじに。
brew –prefixってするとインストール先を返してくれるのはとても便利だなぁ。

という訳でこれで無事にtherubyracerがインストールされた。
bundle installされた問題のgemたちのバージョンは
・nokogiri 1.6.7.2
・libv8 3.16.14.13
・therubyracer 0.12.2 with native extensions
という感じになった。

therubyracerはこのプロジェクトに本当に必要なのかしら?と考える事も大切

therubyracer considered harmful」こちらのブログで言及されている通り、そもそもtherubyracerとのお付き合いも考えないといけない。
そもそもなぜ最初にtherubyracerがコメントアウトされているかというと
Why is the gem therubyracer commented out by default in Gemfile?
もし他のJSライブラリがマシンに入っているならRails的にはそっちを使えるし、別に必須じゃないんだよっていう事らしい。

参考記事でも本番環境にExecJSインストールするのはよろしくないと記載されているし
asset pipelineに使っているだけだし、productionにはExecJS導入しないでrake assets:precompileなどしてコンパイル済のasset出しているよ〜って人が多いと思うし
developmentだけで使うなら別にtherubyracerじゃなくても、node.jsでいいかもしれない。

よくわかんないけどググったら出てきたgem追加したら解決したぞ、とか、コメントアウトしておくと平和だからそっとしておこう…みたいな場当たり対応をするよりも
gemのそれぞれが何をしていてどういう関係性であるかをしっかり把握しておかないと、とっても怖いなぁと改めて思うのでした。


Profile picture

ぴーやま
プログラミングを嗜んでします。中華料理で出てくるたまごふわふわのコーンスープが好きです。