Shred IT!!!!

IT全般について試したこと・勉強したことを綴ったり、趣味について語るブログ

Capistrano からの bundle install で rmagick のインストールが失敗するときの対処方法

概要

cap コマンドからの bundle install が失敗する件。

前回記事で Capistrano 2 系で踏み台経由でデプロイって記事を書いたのだけど、 cap コマンド内で実行される bundle install が失敗してしまうので、 今回それを解決する方法を記事にする。
↓前回記事 jetglass.hatenablog.jp

これも関連記事。
jetglass.hatenablog.jp

環境は ローカルPC -> 踏み台 -> テストサーバ。
ちなみにテストサーバに ImageMagick はインストール済み。

エラーの内容と原因

$ bundle exec cap deploy

上記を実行で下記エラー。

 ** [out :: test_server] Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
 ** [out :: test_server]
 ** [out :: test_server] /usr/local/bin/ruby extconf.rb
 ** [out :: test_server] checking for Ruby version >= 1.8.5... yes
 ** [out :: test_server] checking for gcc... yes
 ** [out :: test_server] checking for Magick-config... yes
 ** [out :: test_server]
 ** [out :: test_server] Warning: Found a partial ImageMagick installation. Your operating system likely has some built-in ImageMagick libraries but not all of ImageMagick. This will most likely cause problems at both compile and runtime.
 ** [out :: test_server] Found partial installation at: /usr/local
 ** [out :: test_server] checking for ImageMagick version >= 6.4.9... yes
 ** [out :: test_server] Package MagickCore was not found in the pkg-config search path.
 ** [out :: test_server] Perhaps you should add the directory containing `MagickCore.pc'
 ** [out :: test_server] to the PKG_CONFIG_PATH environment variable
 ** [out :: test_server] No package 'MagickCore' found
 ** [out :: test_server] Package MagickCore was not found in the pkg-config search path.
 ** [out :: test_server] Perhaps you should add the directory containing `MagickCore.pc'
 ** [out :: test_server] to the PKG_CONFIG_PATH environment variable
 ** [out :: test_server] No package 'MagickCore' found
 ** [out :: test_server] Package MagickCore was not found in the pkg-config search path.
 ** [out :: test_server] Perhaps you should add the directory containing `MagickCore.pc'
 ** [out :: test_server] to the PKG_CONFIG_PATH environment variable
 ** [out :: test_server] No package 'MagickCore' found
 ** [out :: test_server] Package MagickCore was not found in the pkg-config search path.
 ** [out :: test_server] Perhaps you should add the directory containing `MagickCore.pc'
 ** [out :: test_server] to the PKG_CONFIG_PATH environment variable
 ** [out :: test_server] No package 'MagickCore' found
 ** [out :: test_server] checking for stdint.h... yes
 ** [out :: test_server] checking for sys/types.h... yes
 ** [out :: test_server] checking for wand/MagickWand.h... no
 ** [out :: test_server]
 ** [out :: test_server] Can't install RMagick 0.0.0. Can't find MagickWand.h.
 ** [out :: test_server] *** extconf.rb failed ***
 ** [out :: test_server] Could not create Makefile due to some reason, probably lack of
 ** [out :: test_server] necessary libraries and/or headers.  Check the mkmf.log file for more
 ** [out :: test_server] details.  You may need configuration options.
 ** [out :: test_server]
 ** [out :: test_server] Provided configuration options:
 ** [out :: test_server] --with-opt-dir
 ** [out :: test_server] --without-opt-dir
 ** [out :: test_server] --with-opt-include
 ** [out :: test_server] --without-opt-include=${opt-dir}/include
 ** [out :: test_server] --with-opt-lib
 ** [out :: test_server] --without-opt-lib=${opt-dir}/lib
 ** [out :: test_server] --with-make-prog
 ** [out :: test_server] --without-make-prog
 ** [out :: test_server] --srcdir=.
 ** [out :: test_server] --curdir
 ** [out :: test_server] --ruby=/usr/local/bin/ruby
 ** [out :: test_server]
 ** [out :: test_server]
 ** [out :: test_server] Gem files will remain installed in /home/deployer/qupio_web_admin/shared/bundle/ru
 ** [out :: test_server] by/1.9.1/gems/rmagick-2.13.3 for inspection.
 ** [out :: test_server] Results logged to /home/deployer/qupio_web_admin/shared/bundle/ruby/1.9.1/gems/rmagick-2.13.3/ext/RMagick/gem_make.out
 ** [out :: test_server] An error occurred while installing rmagick (2.13.3), and Bundler cannot
 ** [out :: test_server] continue.
 ** [out :: test_server] Make sure that `gem install rmagick -v '2.13.3'` succeeds before bundling.

qiita.com

同じように苦しんだ人の記事を発見。

エラーログに出ている通り、PKG_CONFIG_PATH の環境変数の設定がおかしいようだ。

前回設定した deployer ユーザで ssh して調べる。

$ ssh test_server
$ env | grep PKG_CONFIG_PATH
PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig

普通に設定されてる。
となると cap コマンド経由で環境変数が設定されていないのが問題か。

cap コマンド実行時の環境変数の調べ方、 Capistrano: Can I set an environment variable for the whole cap session? - Stack Overflow
↑を参考にコマンド実行。

$ bundle exec cap COMMAND=printenv invoke

PKG_CONFIG_PATH は設定されてない。

解決方法:環境変数を追加

deploy.rb に下記を追加。

set :default_environment, { 
  'PKG_CONFIG_PATH' => '/usr/lib/pkgconfig:/usr/local/lib/pkgconfig'
}

もう一度、環境変数を確認。

$ bundle exec cap COMMAND=printenv invoke

PKG_CONFIG_PATH が追加されていることを確認できた。

cap コマンド実行。

$ bundle exec cap deploy

今度こそ成功!

まとめ

環境変数が問題で cap に失敗する場合があるかもしれない。

cap 実行時の環境変数は下記で確認

$ bundle exec cap COMMAND=printenv invoke

cap 実行時の環境変数を追加したい場合は deploy.rb に下記を追加

set :default_environment, { 
  'HOGE_PATH' => '/hoge_path'
}