動かざることバグの如し

近づきたいよ 君の理想に

UbuntuでNokogiriをインストールエラーになったときの対処法

環境

発端

わりと頻出ライブラリのNokogiriだが、 bundle install nokogiri ができない。

以下のような長いエラーが出る。結局のところ「You have to install development tools first.」と言われているが、build-essential入れてるんだよな〜〜〜

Building native extensions. This could take a while...
ERROR: Error installing nokogiri:
ERROR: Failed to build gem native extension.

current directory: /var/lib/gems/2.3.0/gems/ffi-1.9.18/ext/ffi_c
/usr/bin/ruby2.3 -r ./siteconf20171120-16712-g2o2td.rb extconf.rb
checking for ffi.h... *** 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=/usr/bin/$(RUBY_BASE_NAME)2.3
--with-ffi_c-dir
--without-ffi_c-dir
--with-ffi_c-include
--without-ffi_c-include=${ffi_c-dir}/include
--with-ffi_c-lib
--without-ffi_c-lib=${ffi_c-dir}/lib
--with-libffi-config
--without-libffi-config
--with-pkg-config
--without-pkg-config
/usr/lib/ruby/2.3.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /usr/lib/ruby/2.3.0/mkmf.rb:587:in `try_cpp'
from /usr/lib/ruby/2.3.0/mkmf.rb:1091:in `block in have_header'
from /usr/lib/ruby/2.3.0/mkmf.rb:942:in `block in checking_for'
from /usr/lib/ruby/2.3.0/mkmf.rb:350:in `block (2 levels) in postpone'
from /usr/lib/ruby/2.3.0/mkmf.rb:320:in `open'
from /usr/lib/ruby/2.3.0/mkmf.rb:350:in `block in postpone'
from /usr/lib/ruby/2.3.0/mkmf.rb:320:in `open'
from /usr/lib/ruby/2.3.0/mkmf.rb:346:in `postpone'
from /usr/lib/ruby/2.3.0/mkmf.rb:941:in `checking_for'
from /usr/lib/ruby/2.3.0/mkmf.rb:1090:in `have_header'
from extconf.rb:16:in `'

extconf failed, exit code 1

原因

悲しいことに他にもインストールしなきゃいけないライブラリがあった

ちゃんとそれは古事記、ではなく、Nokogiriの公式ドキュメントに書いてある。

Ubuntu/Debian では

apt-get install build-essential patch ruby-dev zlib1g-dev liblzma-dev libxslt1-dev libxml2-dev

が必要とのこと。実際に zlib1g-dev が不足していた。。。

今回の環境ではrbenvでRubyの環境を構築していたので、ruby-devを抜いた

apt-get install build-essential patch zlib1g-dev liblzma-dev libxslt1-dev libxml2-dev

をインストールした