動かざることバグの如し

近づきたいよ 君の理想に

bundle installでインストールに失敗した場合の対処法

特定のパッケージをインストールしようとするとエラーになってインストールできない。

例えばmysql2の場合

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

    /home/thr3a/.rvm/rubies/ruby-2.1.2/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
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... 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=/home/thr3a/.rvm/rubies/ruby-2.1.2/bin/ruby
        --with-mysql-dir
        --without-mysql-dir
        (略)
        --without-mysqlclientlib

extconf failed, exit code 1

Gem files will remain installed in /home/thr3a/ruby/apitest1/vendor/bundle/ruby/2.1.0/gems/mysql2-0.3.16 for inspection.
Results logged to /home/thr3a/ruby/apitest1/vendor/bundle/ruby/2.1.0/extensions/x86_64-linux/2.1.0/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.

インストールでコケるときは「特定のパッケージ」ってやつがnative extensionってやつの場合。でライブラリが足りないだけみたいなので追加インストールしてあげればいいっぽい。

mysql2のインストールでエラーになる場合

gem "mysql2"で失敗する場合は

# Ubuntu
sudo apt-get install libmysqld-dev

# CentOS
sudo yum install mysql-devel

MariaDBをインストールしていた場合はコケるので以下をインストール

sudo apt-get install libmariadbclient-dev

or 

yum install MariaDB-devel

rmagickのインストールでエラーになる場合

gem "rmagick" で失敗する場合は

# ubuntu
sudo apt-get install libmagickwand-dev

# centos
yum install ImageMagick ImageMagick-devel

sqlite3のインストールでエラーになる場合

gem "sqlite3"で失敗する場合は

sudo apt-get install libsqlite3-dev

pgのインストールでエラーになる場合

sudo apt-get install libpq-dev

万が一それでも無理ならbundle install前に以下実行

bundle config build.pg --with-pg-lib=/usr/lib

idn-rubyのインストールでエラーになる場合

Centos

sudo yum install libidn-devel

Ubuntu

sudo apt -y install libidn11-dev

gem install curbのインストールでエラーになる場合

Ubuntu

sudo apt-get install libcurl4-openssl-dev

CentOS

 sudo yum install ruby-devel libcurl-devel openssl-devel

gem install nokogiriのインストールでエラーになる場合

公式ドキュメント参照

Ubuntu

sudo apt-get install build-essential patch ruby-dev zlib1g-dev liblzma-dev

CentOS

sudo yum install -y gcc ruby-devel zlib-devel