macOS に RMagick をインストールする

## Mac に RMagick をインストールする

Homebrew で ImageMagick 6 と pkg-config をインストールした後、

環境変数 PKG_CONFIG_PATH を指定して、 gem コマンドで rmagick をインストールする。

```sh
$ brew install imagemagick@6
$ brew install pkg-config
$ export PKG_CONFIG_PATH=/usr/local/opt/imagemagick@6/lib/pkgconfig
$ gem install rmagick
```

## 今回の動作確認環境

macOS Sierra + Ruby 2.4.0

```sh
$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.12.4
BuildVersion:	16E195

$ ruby -v
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin16]

$ /usr/local/opt/imagemagick@6/bin/convert --version
Version: ImageMagick 6.9.8-3 Q16 x86_64 2017-03-25 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

$ gem list | grep rmagick
rmagick (2.16.0)
```

## インストールに失敗するパターン

### ImageMagick 7 がインストールされている

現時点では RMagick は ImageMagick の最新バージョンである7系に対応していない。

ImageMagick が7系の場合、以下のように MagickWand.h が見つからないというエラーが発生する。

```sh
$ gem install rmagick
Building native extensions.  This could take a while...
ERROR:  Error installing rmagick:
	ERROR: Failed to build gem native extension.

    current directory: /Users/yourname/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rmagick-2.16.0/ext/RMagick
/Users/yourname/.rbenv/versions/2.4.0/bin/ruby -r ./siteconf20170418-43189-1kycjl8.rb extconf.rb
checking for clang... 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
	--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/yourname/.rbenv/versions/2.4.0/bin/$(RUBY_BASE_NAME)

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

  /Users/yourname/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/extensions/x86_64-darwin-16/2.4.0-static/rmagick-2.16.0/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/yourname/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rmagick-2.16.0 for inspection.
Results logged to /Users/yourname/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/extensions/x86_64-darwin-16/2.4.0-static/rmagick-2.16.0/gem_make.out
```

このエラーは ImageMagick 7 をアンインストールして、

```sh
$ brew uninstall imagemagick
```

ImageMagick 6 をインストールすることで解決する。

```sh
$ brew install imagemagick@6
==> Downloading https://homebrew.bintray.com/bottles/imagemagick@6-6.9.8-3.sierra.bottle.tar.gz
Already downloaded: /Users/yourname/Library/Caches/Homebrew/imagemagick@6-6.9.8-3.sierra.bottle.tar.gz
==> Pouring imagemagick@6-6.9.8-3.sierra.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

This is an alternate version of another formula.

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

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/imagemagick@6/lib
    CPPFLAGS: -I/usr/local/opt/imagemagick@6/include

==> Summary
🍺  /usr/local/Cellar/imagemagick@6/6.9.8-3: 1,469 files, 22.6MB
```

convert などの ImageMagick のコマンドを使うのであれば、~/.bash_profile などでパスを追加しておく。

```sh
$ echo 'export PATH="/usr/local/opt/imagemagick@6/bin:$PATH"' >> ~/.bash_profile

$ source ~/.bash_profile 

$ convert --version
Version: ImageMagick 6.9.8-3 Q16 x86_64 2017-03-25 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
```

### pkg-config がインストールされていない

pkg-config が無い場合は、以下のように pkg-config が見つからないというエラーが発生する。

```sh
$ gem install rmagick
Building native extensions.  This could take a while...
ERROR:  Error installing rmagick:
	ERROR: Failed to build gem native extension.

    current directory: /Users/yourname/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rmagick-2.16.0/ext/RMagick
/Users/yourname/.rbenv/versions/2.4.0/bin/ruby -r ./siteconf20170418-48774-fs17k4.rb extconf.rb
checking for clang... yes
checking for Magick-config... no
checking for pkg-config... no
Can't install RMagick 2.16.0. Can't find Magick-config or pkg-config in /Users/yourname/.rbenv/versions/2.4.0/bin:/usr/local/Cellar/rbenv/1.1.0/libexec:/Users/yourname/.rbenv/shims:/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/yourname/.rbenv/versions/2.4.0/bin/$(RUBY_BASE_NAME)

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

  /Users/yourname/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/extensions/x86_64-darwin-16/2.4.0-static/rmagick-2.16.0/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/yourname/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rmagick-2.16.0 for inspection.
Results logged to /Users/yourname/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/extensions/x86_64-darwin-16/2.4.0-static/rmagick-2.16.0/gem_make.out
```

pkg-config をインストールすれば解決する。

```sh
$ brew install pkg-config
```

### 環境変数 PKG_CONFIG_PATH が設定されていない

PKG_CONFIG_PATH が設定されていない場合は、以下のようなエラーが発生する。

```
$ gem install rmagick
Building native extensions.  This could take a while...
ERROR:  Error installing rmagick:
	ERROR: Failed to build gem native extension.

    current directory: /Users/yourname/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rmagick-2.16.0/ext/RMagick
/Users/yourname/.rbenv/versions/2.4.0/bin/ruby -r ./siteconf20170418-43866-9wvfhr.rb extconf.rb
checking for clang... yes
checking for Magick-config... no
checking for pkg-config... yes
Package MagickCore was not found in the pkg-config search path.
Perhaps you should add the directory containing `MagickCore.pc'
to the PKG_CONFIG_PATH environment variable
No package 'MagickCore' found
checking for outdated ImageMagick version (<= 6.4.9)... *** 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/yourname/.rbenv/versions/2.4.0/bin/$(RUBY_BASE_NAME)

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

  /Users/yourname/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/extensions/x86_64-darwin-16/2.4.0-static/rmagick-2.16.0/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/yourname/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rmagick-2.16.0 for inspection.
Results logged to /Users/yourname/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/extensions/x86_64-darwin-16/2.4.0-static/rmagick-2.16.0/gem_make.out
```

環境変数 PKG_CONFIG_PATH を設定して、rmagick をインストールすれば解決する。

```sh
$ export PKG_CONFIG_PATH=/usr/local/opt/imagemagick@6/lib/pkgconfig

$ gem install rmagick
Building native extensions.  This could take a while...
Successfully installed rmagick-2.16.0
Parsing documentation for rmagick-2.16.0
Installing ri documentation for rmagick-2.16.0
Done installing documentation for rmagick after 2 seconds
1 gem installed
```