rails test で nokogiri のエラーが発生するときの対処

$ rails testを実行すると以下のエラーログがでるときの対処メモ。
なお、$ bundle install $ rails serverは通ります。

error_log
Ignoring bindex-0.5.0 because its extensions are not built.  Try: gem pristine bindex --version 0.5.0
Ignoring byebug-10.0.0 because its extensions are not built.  Try: gem pristine byebug --version 10.0.0
Ignoring byebug-9.0.6 because its extensions are not built.  Try: gem pristine byebug --version 9.0.6
Ignoring ffi-1.9.23 because its extensions are not built.  Try: gem pristine ffi --version 1.9.23
Ignoring nio4r-2.2.0 because its extensions are not built.  Try: gem pristine nio4r --version 2.2.0
Ignoring nokogiri-1.8.2 because its extensions are not built.  Try: gem pristine nokogiri --version 1.8.2
Ignoring puma-3.11.3 because its extensions are not built.  Try: gem pristine puma --version 3.11.3
Ignoring puma-3.9.1 because its extensions are not built.  Try: gem pristine puma --version 3.9.1
Ignoring sqlite3-1.3.13 because its extensions are not built.  Try: gem pristine sqlite3 --version 1.3.13
Ignoring websocket-driver-0.6.5 because its extensions are not built.  Try: gem pristine websocket-driver --version 0.6.5
/var/lib/gems/2.3.0/gems/bundler-1.16.1/lib/bundler/spec_set.rb:88:in `block in materialize': Could not find nokogiri-1.8.2 in any of the sources (Bundler::GemNotFound)
        from /var/lib/gems/2.3.0/gems/bundler-1.16.1/lib/bundler/spec_set.rb:82:in `map!'
        from /var/lib/gems/2.3.0/gems/bundler-1.16.1/lib/bundler/spec_set.rb:82:in `materialize'
        from /var/lib/gems/2.3.0/gems/bundler-1.16.1/lib/bundler/definition.rb:170:in `specs'
        from /var/lib/gems/2.3.0/gems/bundler-1.16.1/lib/bundler/definition.rb:237:in `specs_for'
        from /var/lib/gems/2.3.0/gems/bundler-1.16.1/lib/bundler/definition.rb:226:in `requested_specs'
        from /var/lib/gems/2.3.0/gems/bundler-1.16.1/lib/bundler/runtime.rb:108:in `block in definition_method'
        from /var/lib/gems/2.3.0/gems/bundler-1.16.1/lib/bundler/runtime.rb:20:in `setup'
        from /var/lib/gems/2.3.0/gems/bundler-1.16.1/lib/bundler.rb:107:in `setup'
        from /var/lib/gems/2.3.0/gems/bundler-1.16.1/lib/bundler/setup.rb:20:in `<top (required)>'
        from /home/(username)/.rbenv/versions/2.4.3/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/(username)/.rbenv/versions/2.4.3/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /var/lib/gems/2.3.0/gems/spring-2.0.2/lib/spring/commands.rb:33:in `<module:Spring>'
        from /var/lib/gems/2.3.0/gems/spring-2.0.2/lib/spring/commands.rb:4:in `<top (required)>'
        from /home/(username)/.rbenv/versions/2.4.3/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/(username)/.rbenv/versions/2.4.3/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /var/lib/gems/2.3.0/gems/spring-2.0.2/lib/spring/server.rb:9:in `<top (required)>'
        from /home/(username)/.rbenv/versions/2.4.3/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/(username)/.rbenv/versions/2.4.3/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /var/lib/gems/2.3.0/gems/spring-2.0.2/lib/spring/client/server.rb:9:in `call'
        from /var/lib/gems/2.3.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
        from /var/lib/gems/2.3.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
        from /var/lib/gems/2.3.0/gems/spring-2.0.2/bin/spring:49:in `<main>'

環境

  • Ubuntu 16.04.4 LTS
  • rbenv 1.1.1-30-gc8ba27f
  • ruby 2.4.3p205
  • gem 2.6.14

対処方法

エラーログ前半の $ gem pristine 各種を試しても結果は変わらず

後半のログを頼りにしてみましょう。

error_log
Could not find nokogiri-1.8.2 in any of the sources (Bundler::GemNotFound)

そこで$ gem install nokogiriを実行しますが、相変わらずエラーは出力されます

結論

rbenv に nokogiri がうまく紐づけできていなかったようです。
以下のコマンドを実行することで解決できました。

console
$ rbenv exec gem install bundler
$ rbenv rehash
$ bundle install

$ rbenv exec gemで明示的にrbenv環境のgemを呼び出します。

参考

以下のサイトを参考にしました。ありがとうございます。
【Ruby on Rails】`block in materialize’: Could not find nokogiri-1.8.2 in any of the sources 解決方法 | 東京エンジニア

Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.