Mac OS Xにrmagickをインストール

Macでrmagickをbudle installする際にやったことを書いておきます。 エラーメッセージをよく読もうという教訓の話。

環境

やったこと

bundle installすると以下のエラーが発生

% bundle install
(略)

Installing rmagick 2.13.2 with native extensions

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

    /Users/yoshito/.rbenv/versions/2.1.1/bin/ruby extconf.rb
checking for Ruby version >= 1.8.5... yes
checking for clang... yes
checking for Magick-config... no
Can't install RMagick 2.13.2. Can't find Magick-config in /Users/yoshito/.rbenv/versions/2.1.1/bin:/Users/yoshito/.rbenv/libexec:/Users/yoshito/.rbenv/plugins/ruby-build/bin:/Users/yoshito/.nodebrew/current/bin:/Users/yoshito/.rbenv/shims:/Users/yoshito/.rbenv/bin:/opt/chefdk/bin:/usr/local/heroku/bin:/Users/yoshito/.nodebrew/current/bin:/Users/yoshito/.rbenv/shims:/Users/yoshito/.rbenv/bin:/opt/chefdk/bin:/usr/local/heroku/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

*** 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=/Users/yoshito/.rbenv/versions/2.1.1/bin/ruby

extconf failed, exit code 1

Mac rmagickで調べてみると、 ImageMagicのインストールが必要とのことで 以下を実施

brew install imagemagick

再度チャレンジすると状況は変わったが、まだエラーが起きている。

Installing rmagick 2.13.2 with native extensions

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

    /Users/yoshito/.rbenv/versions/2.1.1/bin/ruby extconf.rb
checking for Ruby version >= 1.8.5... yes
checking for clang... yes
checking for Magick-config... yes
checking for ImageMagick version >= 6.4.9... yes
checking for HDRI disabled version of ImageMagick... yes
/usr/local/bin/Magick-config: line 41: pkg-config: command not found
/usr/local/bin/Magick-config: line 47: pkg-config: command not found
/usr/local/bin/Magick-config: line 50: pkg-config: command not found
/usr/local/bin/Magick-config: line 53: pkg-config: command not found
checking for stdint.h... yes
checking for sys/types.h... yes
checking for wand/MagickWand.h... no

Can't install RMagick 2.13.2. Can't find MagickWand.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=/Users/yoshito/.rbenv/versions/2.1.1/bin/ruby

extconf failed, exit code 1

ググって調べた情報を元に 環境変数PKG_CONFIG_PATHを設定してみたりしたが状況は変わらず。

もう一度エラーメッセージをよく読んでみると、

/usr/local/bin/Magick-config: line 41: pkg-config: command not found

とででいる。 pkg-configコマンドが無いのが原因ぽい

% pkg-config
zsh: command not found: pkg-config

Mac pkg-config でググると次の記事を発見

この情報を参考に以下を実行

brew install pkg-config

これでbundle installが成功しました。

まとめ

結局のところ、以下のコマンドでうまくいくのではと思いますが、 本当にこれで十分かは確認できてません。 (アンインストールしてもう一回やらないと確証がもてない)

brew install imagemagick
brew install pkg-config
bundle install

やはりエラーメッセージをよく読むことが大切ですね。