bundle/gem install rmagick でエラー Can't find MagickWand.h. の解決方法

Tadashi Shigeoka ·  Thu, November 30, 2017

Ruby の 画像処理ライブラリ RMagick のインストール gem install rmagick でエラーが発生したときの解決方法をご紹介します。

Ruby | ルビー

原因は、ImageMagick のバージョンが 7 と新しすぎたことでした。これを 6 にダウングレードすれば無事に RMagick をインストールできました。

# cd to_rails_app_dir 
$ bundle install
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/your_user/works/your_app/vendor/bundle/ruby/2.4.0/gems/rmagick-2.16.0/ext/RMagick
/Users/your_user/.rvm/rubies/ruby-2.4.1/bin/ruby -r ./siteconf20171130-66060-5yh4gp.rb extconf.rb
checking for gcc... yes
checking for Magick-config... no
checking for pkg-config... yes
checking for outdated ImageMagick version (<= 6.4.9)... no
checking for presence of MagickWand API (ImageMagick version >= 6.9.0)... no
checking for Ruby version >= 1.8.5... yes
checking for stdint.h... yes
checking for sys/types.h... yes
checking for wand/MagickWand.h... no

Can't install RMagick 2.16.0. 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
  --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/your_user/.rvm/rubies/ruby-2.4.1/bin/$(RUBY_BASE_NAME)

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /Users/your_user/works/your_app/vendor/bundle/ruby/2.4.0/extensions/x86_64-darwin-15/2.4.0/rmagick-2.16.0/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/your_user/works/your_app/vendor/bundle/ruby/2.4.0/gems/rmagick-2.16.0 for inspection.
Results logged to /Users/your_user/works/your_app/vendor/bundle/ruby/2.4.0/extensions/x86_64-darwin-15/2.4.0/rmagick-2.16.0/gem_make.out

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

In Gemfile:
  rmagick

このエラーが発生した時点でインストールしていた ImageMagick のバージョンは 7 でした。

$ identify -version
Version: ImageMagick 7.0.7-5 Q16 x86_64 2017-10-03 http://www.imagemagick.org
Copyright: © 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules 
Delegates (built-in): bzlib freetype jng jpeg ltdl lzma png tiff xml zlib

ImageMagick 6 インストール

MacOS の Homebrew で ImageMagick 6 を別途インストールしていきます。

$ brew install imagemagick@6
$ brew link --overwrite --force imagemagick@6
Linking /usr/local/Cellar/imagemagick@6/6.9.9-24... 309 symlinks created

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/imagemagick@6/bin:$PATH"' >> ~/.zshrc
$ echo 'export PATH="/usr/local/opt/imagemagick@6/bin:$PATH"' >> ~/.zshrc

brew link と export PATH を終えたら、改めてバージョンを確認して ImageMagick 6 が確認できれば完了です。

$ identify -version
Version: ImageMagick 6.9.9-24 Q16 x86_64 2017-11-30 http://www.imagemagick.org
Copyright: © 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules 
Delegates (built-in): bzlib freetype jng jpeg ltdl lzma png tiff xml zlib

色々と警告がでる brew link の例

$ brew link --force imagemagick@6
Linking /usr/local/Cellar/imagemagick@6/6.9.9-24... 
Error: Could not symlink bin/Magick++-config
Target /usr/local/bin/Magick++-config
is a symlink belonging to imagemagick. You can unlink it:
  brew unlink imagemagick

To force the link and overwrite all conflicting files:
  brew link --overwrite imagemagick@6

To list all files that would be deleted:
  brew link --overwrite --dry-run imagemagick@6
$ brew link --overwrite imagemagick@6
Warning: imagemagick@6 is keg-only and must be linked with --force
Note that doing so can interfere with building software.

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/imagemagick@6/bin:$PATH"' >> ~/.zshrc

再び bundle install

ImageMagick 6 を利用する設定が完了したら、最後に bundle install して無事にインストールできれば作業完了です。

# cd to_rails_app_dir 
$ bundle install

以上、Ruby on Rails で RMagick をインストールしたい現場からお送りしました。

参考情報