How to install gem "rmagick" with MacOS.

# rmagick のインストールに失敗するヒトいませんか
いますよね

```
% brew install imagemagick
```

```
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
awscli                                   rhash

==> Downloading https://homebrew.bintray.com/bottles/imagemagick-7.0.6-7.sierra.
Already downloaded: /Users/maangie/Library/Caches/Homebrew/imagemagick-7.0.6-7.sierra.bottle.tar.gz
==> Pouring imagemagick-7.0.6-7.sierra.bottle.tar.gz
🍺  /usr/local/Cellar/imagemagick/7.0.6-7: 1,522 files, 22.7MB
```

```
% gem install rmagick
```

```
Fetching: rmagick-2.16.0.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing rmagick:
	ERROR: Failed to build gem native extension.

    current directory: /Users/maangie/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rmagick-2.16.0/ext/RMagick
/Users/maangie/.rbenv/versions/2.4.1/bin/ruby -r ./siteconf20170814-39517-krzuh4.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/maangie/.rbenv/versions/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/maangie/.rbenv/versions/2.4.1/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/maangie/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rmagick-2.16.0 for inspection.
Results logged to /Users/maangie/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/extensions/x86_64-darwin-16/2.4.0-static/rmagick-2.16.0/gem_make.out
```

# ImageMagick 7 が悪いのだ？

上記エラーに `wand/MagickWand.h` が無いとあります。
`/usr/local/Cellar/imagemagick/7.0.6-7/include/ImageMagick-7` を見ればわかりますが、ImageMagick 7 からは、ディレクトリ `wand` が `MagickWand` に変更になったようなのです。

僕あソースをあまりいじりたくないので、ImageMagick 6 にダウングレードすることにしました。

```
% brew uninstall imagemagick
```

```
Uninstalling /usr/local/Cellar/imagemagick/7.0.6-7... (1,522 files, 22.7MB)
```

```
% brew install imagemagick@6
```

```
==> Downloading https://homebrew.bintray.com/bottles/imagemagick@6-6.9.9-7.sierr
Already downloaded: /Users/maangie/Library/Caches/Homebrew/imagemagick@6-6.9.9-7.sierra.bottle.tar.gz
==> Pouring imagemagick@6-6.9.9-7.sierra.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local,
because 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"' >> ~/.zshrc

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
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/imagemagick@6/lib/pkgconfig

==> Summary
🍺  /usr/local/Cellar/imagemagick@6/6.9.9-7: 1,469 files, 22.4MB
```

# インストール！
`imagemagick@6` インストール時に指定されたとおりにオプションを設定してレッツゴー。

```
% PKG_CONFIG_PATH=/usr/local/opt/imagemagick@6/lib/pkgconfig \
  gem install rmagick -- \
  --with-cppflags=-I/usr/local/opt/imagemagick@6/include \
  --with-ldflags=-L/usr/local/opt/imagemagick@6/lib
```

```
Building native extensions with: '--with-cppflags=-I/usr/local/opt/imagemagick@6/include --with-ldflags=-L/usr/local/opt/imagemagick@6/lib'
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
```

うまく行きました。

# 参考にしたページ
- [ruby - gem install append CPPFLAGS / LDFLAGS - Server Fault](https://serverfault.com/questions/577944/gem-install-append-cppflags-ldflags)
- [gem install rmagickで失敗する。 - Qiita](http://qiita.com/KTakata/items/f4a632207af0a8078c06)