読者です 読者をやめる 読者になる 読者になる

gem インストール時に発生したエラーとその解決方法まとめ

Ruby gem

gemのインストール時に発生したエラーとその解決方法をまとめておきます。
とりあえずメモしておいたのはこれぐらい。

  • mysql2
  • therubyracer
  • kakasi, mecab
  • sqlite
  • libxml-ruby
  • nokogiri
  • capybara-webkit
  • thrift

mysql2

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /usr/local/bin/ruby extconf.rb
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
-----
Using mysql_config at /usr/bin/mysql_config
-----
checking for mysql.h... no
checking for mysql/mysql.h... no
-----
mysql.h is missing.  please check your installation of mysql and try again.
-----
*** 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/local/bin/ruby
        --with-mysql-dir
        --without-mysql-dir
        --with-mysql-include
        --without-mysql-include=${mysql-dir}/include
        --with-mysql-lib
        --without-mysql-lib=${mysql-dir}/lib
        --with-mysql-config
        --without-mysql-config

extconf failed, exit code 1

Gem files will remain installed in /var/www/demo/vendor/bundle/ruby/2.1.0/gems/mysql2-0.3.16 for inspection.
Results logged to /var/www/demo/vendor/bundle/ruby/2.1.0/extensions/x86_64-linux/2.1.0-static/mysql2-0.3.16/gem_make.out
An error occurred while installing mysql2 (0.3.16), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.3.16'` succeeds before bundling.

解決方法

$ sudo yum -y install mysql-devel

therubyracer

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /usr/local/bin/ruby extconf.rb
checking for main() in -lpthread... yes
creating Makefile

make "DESTDIR=" clean

make "DESTDIR="
compiling template.cc
make: g++: コマンドが見つかりませんでした
make: *** [template.o] エラー 127

make failed, exit code 2

Gem files will remain installed in /var/www/demo/vendor/bundle/ruby/2.1.0/gems/therubyracer-0.12.1 for inspection.
Results logged to /var/www/demo/vendor/bundle/ruby/2.1.0/extensions/x86_64-linux/2.1.0-static/therubyracer-0.12.1/gem_make.out
An error occurred while installing therubyracer (0.12.1), and Bundler cannot
continue.
Make sure that `gem install therubyracer -v '0.12.1'` succeeds before bundling.

解決方法

$ sudo yum install gcc-c++

kakasi, mecab

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /usr/local/bin/ruby extconf.rb
checking fiddle... no (cannot load such file -- fiddle/import)
checking dl... DL is deprecated, please use Fiddle
yes
checking for kakasi... *** 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/local/bin/ruby
        --with-kakasi-dir
        --without-kakasi-dir
        --with-kakasi-include
        --without-kakasi-include=${kakasi-dir}/include
        --with-kakasi-lib
        --without-kakasi-lib=${kakasi-dir}/lib
/usr/local/bin/ruby/2.1.0/dl/import.rb:68:in `rescue in block in dlload': can't load libkakasi.so (DL::DLError)
        from /usr/local/rbenv/versions/2.1.1/lib/ruby/2.1.0/dl/import.rb:65:in `block in dlload'
        from /usr/local/rbenv/versions/2.1.1/lib/ruby/2.1.0/dl/import.rb:56:in `collect'
        from /usr/local/rbenv/versions/2.1.1/lib/ruby/2.1.0/dl/import.rb:56:in `dlload'
        from extconf.rb:49:in `block in <module:KakasiExtConf>'
        from /var/www/demo/vendor/bundle/ruby/2.1.0/gems/kakasi-1.0.1/lib/kakasi/platform.rb:55:in `try_load'
        from extconf.rb:46:in `<module:KakasiExtConf>'
        from extconf.rb:8:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in /var/www/demo/vendor/bundle/ruby/2.1.0/gems/kakasi-1.0.1 for inspection.
Results logged to /var/www/demo/vendor/bundle/ruby/2.1.0/extensions/x86_64-linux/2.1.0-static/kakasi-1.0.1/gem_make.out
An error occurred while installing kakasi (1.0.1), and Bundler cannot continue.
Make sure that `gem install kakasi -v '1.0.1'` succeeds before bundling.

解決方法

# /etc/ld.so.conf.d/usrlocal.conf

/usr/local/lib
$ sudo ldconfig

sqlite

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /usr/local/bin/ruby extconf.rb
checking for sqlite3.h... no
sqlite3.h is missing. Try 'port install sqlite3 +universal',
'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
and check your shared library search path (the
location where your sqlite3 shared library is located).
*** 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/local/bin/ruby
  --with-sqlite3-dir
  --without-sqlite3-dir
  --with-sqlite3-include
  --without-sqlite3-include=${sqlite3-dir}/include
  --with-sqlite3-lib
  --without-sqlite3-lib=${sqlite3-dir}/lib
  --enable-local
  --disable-local

extconf failed, exit code 1

Gem files will remain installed in /var/www/demo/vendor/bundle/ruby/2.1.0/gems/sqlite3-1.3.9 for inspection.
Results logged to /var/www/demo/vendor/bundle/ruby/2.1.0/extensions/x86_64-linux/2.1.0-static/sqlite3-1.3.9/gem_make.out
An error occurred while installing sqlite3 (1.3.9), and Bundler cannot continue.
Make sure that `gem install sqlite3 -v '1.3.9'` succeeds before bundling.

解決方法

$ sudo yum install -y sqlite sqlite-devel

libxml-ruby

$ gem install libxml-ruby
Fetching: libxml-ruby-2.8.0.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing libxml-ruby:
    ERROR: Failed to build gem native extension.

    /usr/local/rbenv/versions/2.1.5/bin/ruby extconf.rb
checking for socket() in -lsocket... no
checking for gethostbyname() in -lnsl... yes
checking for atan() in -lm... yes
checking for inflate() in -lz... yes
checking for iconv_open() in -liconv... no
checking for libiconv_open() in -liconv... no
checking for libiconv_open() in -llibiconv... no
checking for iconv_open() in -llibiconv... no
checking for iconv_open() in -lc... yes
checking for xmlParseDoc() in -lxml2... no
checking for xmlParseDoc() in -llibxml2... no
checking for xmlParseDoc() in -lxml2... 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=/usr/local/rbenv/versions/2.1.5/bin/ruby
    --with-iconv-dir
    --without-iconv-dir
    --with-iconv-include
    --without-iconv-include=${iconv-dir}/include
    --with-iconv-lib
    --without-iconv-lib=${iconv-dir}/lib
    --with-zlib-dir
    --without-zlib-dir
    --with-zlib-include
    --without-zlib-include=${zlib-dir}/include
    --with-zlib-lib
    --without-zlib-lib=${zlib-dir}/lib
    --with-socketlib
    --without-socketlib
    --with-nsllib
    --without-nsllib
    --with-mlib
    --without-mlib
    --with-zlib
    --without-zlib
    --with-iconvlib
    --without-iconvlib
    --with-iconvlib
    --without-iconvlib
    --with-libiconvlib
    --without-libiconvlib
    --with-libiconvlib
    --without-libiconvlib
    --with-clib
    --without-clib
    --with-xml2-config
    --without-xml2-config
    --with-xml2-dir
    --without-xml2-dir
    --with-xml2-include
    --without-xml2-include=${xml2-dir}/include
    --with-xml2-lib
    --without-xml2-lib=${xml2-dir}/lib
    --with-xml2lib
    --without-xml2lib
    --with-libxml2lib
    --without-libxml2lib
    --with-xml2lib
    --without-xml2lib
 extconf failure: need libxml2.

    Install the library or try one of the following options to extconf.rb:

      --with-xml2-config=/path/to/xml2-config
      --with-xml2-dir=/path/to/libxml2
      --with-xml2-lib=/path/to/libxml2/lib
      --with-xml2-include=/path/to/libxml2/include


extconf failed, exit code 1

Gem files will remain installed in /usr/local/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/libxml-ruby-2.8.0 for inspection.
Results logged to /usr/local/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/extensions/x86_64-linux/2.1.0-static/libxml-ruby-2.8.0/gem_make.out

解決方法

$ sudo yum install libxml2-devel

nokogiri

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /usr/local/rbenv/versions/2.2.0/bin/ruby -r ./siteconf20150305-16219-z0f8rq.rb extconf.rb
checking if the C compiler accepts ... yes
Building nokogiri using packaged libraries.
checking for gzdopen() in -lz... yes
checking for iconv... yes
************************************************************************
IMPORTANT NOTICE:

Building Nokogiri with a packaged version of libxml2-2.9.2
with the following patches applied:
  - 0001-Revert-Missing-initialization-for-the-catalog-module.patch
  - 0002-Fix-missing-entities-after-CVE-2014-3660-fix.patch

Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:

    gem install nokogiri -- --use-system-libraries
        [--with-xml2-config=/path/to/xml2-config]
        [--with-xslt-config=/path/to/xslt-config]

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install

Note, however, that nokogiri is not fully compatible with arbitrary
versions of libxml2 provided by OS/package vendors.
************************************************************************
Extracting libxml2-2.9.2.tar.gz into tmp/x86_64-unknown-linux-gnu/ports/libxml2/2.9.2... OK
Running patch with /var/www/demo/vendor/bundle/ruby/2.2.0/gems/nokogiri-1.6.6.2/ports/patches/libxml2/0001-Revert-Missing-initialization-for-the-catalog-module.patch...
Running 'patch' for libxml2 2.9.2... ERROR, review '/var/www/demo/vendor/bundle/ruby/2.2.0/gems/nokogiri-1.6.6.2/ext/nokogiri/tmp/x86_64-unknown-linux-gnu/ports/libxml2/2.9.2/patch.log' to see what happened.
*** 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/local/rbenv/versions/2.2.0/bin/$(RUBY_BASE_NAME)
  --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
/var/www/demo/vendor/bundle/ruby/2.2.0/gems/mini_portile-0.6.2/lib/mini_portile.rb:279:in `block in execute': Failed to complete patch task (RuntimeError)
  from /var/www/demo/vendor/bundle/ruby/2.2.0/gems/mini_portile-0.6.2/lib/mini_portile.rb:271:in `chdir'
  from /var/www/demo/vendor/bundle/ruby/2.2.0/gems/mini_portile-0.6.2/lib/mini_portile.rb:271:in `execute'
  from extconf.rb:311:in `block in patch'
  from extconf.rb:308:in `each'
  from extconf.rb:308:in `patch'
  from /var/www/demo/vendor/bundle/ruby/2.2.0/gems/mini_portile-0.6.2/lib/mini_portile.rb:108:in `cook'
  from extconf.rb:278:in `block in process_recipe'
  from extconf.rb:177:in `tap'
  from extconf.rb:177:in `process_recipe'
  from extconf.rb:475:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in /var/www/demo/vendor/bundle/ruby/2.2.0/gems/nokogiri-1.6.6.2 for inspection.
Results logged to /var/www/demo/vendor/bundle/ruby/2.2.0/extensions/x86_64-linux/2.2.0-static/nokogiri-1.6.6.2/gem_make.out
An error occurred while installing nokogiri (1.6.6.2), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.6.6.2'` succeeds before bundling.

解決方法

$ bundle config build.nokogiri --use-system-libraries

nokogiriは libxml2-devel, libxslt-devel がインストールされていない場合もコンパイルに失敗するのでインストールしておきましょう。

Capistrano の場合は以下を設定する

set :bundle_env_variables, { nokogiri_use_system_libraries: 1 }

capybara-webkit

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /usr/local/rbenv/versions/2.2.3/bin/ruby -r ./siteconf20160323-81763-l3fo97.rb extconf.rb
*** 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/local/rbenv/versions/2.2.3/bin/$(RUBY_BASE_NAME)
        --with-gl-dir
        --without-gl-dir
        --with-gl-include
        --without-gl-include=${gl-dir}/include
        --with-gl-lib
        --without-gl-lib=${gl-dir}/lib
        --with-zlib-dir
        --without-zlib-dir
        --with-zlib-include
        --without-zlib-include=${zlib-dir}/include
        --with-zlib-lib
        --without-zlib-lib=${zlib-dir}/lib
Command 'qmake ' not available

extconf failed, exit code 1

Gem files will remain installed in /usr/local/rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/capybara-webkit-1.8.0 for inspection.
Results logged to /usr/local/rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-14/2.2.0-static/capybara-webkit-1.8.0/gem_make.out

解決方法

$ brew install qt

thrift

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

...

In file included from strlcpy.c:20:
./strlcpy.h:28:15: error: expected parameter declarator
extern size_t strlcpy(char *, const char *, size_t);
              ^
/usr/include/secure/_string.h:105:44: note: expanded from macro 'strlcpy'
  __builtin___strlcpy_chk (dest, src, len, __darwin_obsz (dest))
                                           ^
/usr/include/secure/_common.h:39:62: note: expanded from macro '__darwin_obsz'
#define __darwin_obsz(object) __builtin_object_size (object, _USE_FORTIFY_LEVEL > 1 ? 1 : 0)
                                                             ^
/usr/include/secure/_common.h:30:32: note: expanded from macro '_USE_FORTIFY_LEVEL'
#    define _USE_FORTIFY_LEVEL 2
                               ^
In file included from strlcpy.c:20:
./strlcpy.h:28:15: error: expected ')'
/usr/include/secure/_string.h:105:44: note: expanded from macro 'strlcpy'
  __builtin___strlcpy_chk (dest, src, len, __darwin_obsz (dest))
                                           ^
/usr/include/secure/_common.h:39:62: note: expanded from macro '__darwin_obsz'
#define __darwin_obsz(object) __builtin_object_size (object, _USE_FORTIFY_LEVEL > 1 ? 1 : 0)
                                                             ^
/usr/include/secure/_common.h:30:32: note: expanded from macro '_USE_FORTIFY_LEVEL'
#    define _USE_FORTIFY_LEVEL 2
                               ^
./strlcpy.h:28:15: note: to match this '('
/usr/include/secure/_string.h:105:44: note: expanded from macro 'strlcpy'
  __builtin___strlcpy_chk (dest, src, len, __darwin_obsz (dest))
                                           ^
/usr/include/secure/_common.h:39:53: note: expanded from macro '__darwin_obsz'
#define __darwin_obsz(object) __builtin_object_size (object, _USE_FORTIFY_LEVEL > 1 ? 1 : 0)
                                                    ^
In file included from strlcpy.c:20:
./strlcpy.h:28:15: error: type specifier missing, defaults to 'int' [-Werror,-Wimplicit-int]
extern size_t strlcpy(char *, const char *, size_t);
              ^
/usr/include/secure/_string.h:105:44: note: expanded from macro 'strlcpy'
  __builtin___strlcpy_chk (dest, src, len, __darwin_obsz (dest))
                                           ^
/usr/include/secure/_common.h:39:31: note: expanded from macro '__darwin_obsz'
#define __darwin_obsz(object) __builtin_object_size (object, _USE_FORTIFY_LEVEL > 1 ? 1 : 0)
                              ^
In file included from strlcpy.c:20:
./strlcpy.h:28:15: error: conflicting types for '__builtin___strlcpy_chk'
/usr/include/secure/_string.h:105:3: note: expanded from macro 'strlcpy'
  __builtin___strlcpy_chk (dest, src, len, __darwin_obsz (dest))
  ^
./strlcpy.h:28:15: note: '__builtin___strlcpy_chk' is a builtin with type 'unsigned long (char *, const char *, unsigned long, unsigned long)'
/usr/include/secure/_string.h:105:3: note: expanded from macro 'strlcpy'
  __builtin___strlcpy_chk (dest, src, len, __darwin_obsz (dest))
  ^
4 errors generated.
make: *** [strlcpy.o] Error 1

make failed, exit code 2

Gem files will remain installed in /var/www/demo/vendor/bundle/ruby/2.3.0/gems/thrift-0.8.0 for inspection.
Results logged to /var/www/demo/vendor/bundle/ruby/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/thrift-0.8.0/gem_make.out

An error occurred while installing thrift (0.8.0), and Bundler cannot continue.
Make sure that `gem install thrift -v '0.8.0'` succeeds before bundling.

解決方法

$ bundle config build.thrift --with-cppflags='-D_FORTIFY_SOURCE=0'