Mac にRMagick がインストールできなかったのを解決したメモ

- | -
---- | ----
OS | macOS Sierra 10.12.4

```:bundle_install 
$ bundle install

(略)

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

   current directory: /Users/xxxx/vendor/bundle/ruby/2.3.0/gems/rmagick-2.15.4/ext/RMagick
/Users/xxxx/.rbenv/versions/2.3.1/bin/ruby -r ./siteconf20170418-5047-90k3lz.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/xxxx/.rbenv/versions/2.3.1/bin/$(RUBY_BASE_NAME)

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

 /Users/xxxx/vendor/bundle/ruby/2.3.0/extensions/x86_64-darwin-16/2.3.0-static/rmagick-2.15.4/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/xxxx/vendor/bundle/ruby/2.3.0/gems/rmagick-2.15.4 for inspection.
Results logged to /Users/xxxx/vendor/bundle/ruby/2.3.0/extensions/x86_64-darwin-16/2.3.0-static/rmagick-2.15.4/gem_make.out

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

`gem install rmagick -v '2.15.4'` をしろって書いてあるのでやってみる

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

   current directory: /Users/xxxx/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rmagick-2.15.4/ext/RMagick
/Users/xxxx/.rbenv/versions/2.3.1/bin/ruby -r ./siteconf20170418-10432-2mfpv1.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/xxxx/.rbenv/versions/2.3.1/bin/$(RUBY_BASE_NAME)

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

 /Users/xxxx/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-16/2.3.0-static/rmagick-2.15.4/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/xxxx/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rmagick-2.15.4 for inspection.
Results logged to /Users/xxxx/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-16/2.3.0-static/rmagick-2.15.4/gem_make.out
$
```
MagickCore.pc がないとのことなので、 ImageMagick を入れる。

brew install imagemagick をしようとしたが、 RMagick の 2.15 とかは、どうやら 7 系ではダメらしいので、6系の最新を入れたい

```:バージョンを検索
$ brew search imagemagick # 右の方に @6 が出てくる
imagemagick                                                                                                             imagemagick@6
$
```

```:install
$ brew install imagemagick@6
==> Downloading https://homebrew.bintray.com/bottles/imagemagick@6-6.9.8-3.sierra.bottle.tar.gz
######################################################################## 100.0%
==> 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
For pkg-config to find this software you may need to set:
   PKG_CONFIG_PATH: /usr/local/opt/imagemagick@6/lib/pkgconfig

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

でも結果が変わらない

`brew install` の結果を見るに、  `/usr/local/Cellar/imagemagick@6/6.9.8-3/lib/pkgconfig` にありそう

```:lsしてみる
$ ls /usr/local/Cellar/imagemagick@6/6.9.8-3/lib/pkgconfig
ImageMagick++-6.Q16.pc  ImageMagick-6.Q16.pc    Magick++-6.Q16.pc   MagickCore-6.Q16.pc MagickWand-6.Q16.pc Wand-6.Q16.pc
ImageMagick++.pc    ImageMagick.pc      Magick++.pc     MagickCore.pc       MagickWand.pc       Wand.pc
$
```
MagickCore あった！


```:パスを環境変数PKG_CONFIG_PATHに設定
$ echo "export PKG_CONFIG_PATH=/usr/local/Cellar/imagemagick@6/6.9.8-3/lib/pkgconfig" >> ~/.bash_profile
$ source ~/.bash_profile
$
```

```:確認
$ echo $PKG_CONFIG_PATH
/usr/local/Cellar/imagemagick@6/6.9.8-3/lib/pkgconfig
$ ls $(echo $PKG_CONFIG_PATH)
ImageMagick++-6.Q16.pc  ImageMagick-6.Q16.pc    Magick++-6.Q16.pc   MagickCore-6.Q16.pc MagickWand-6.Q16.pc Wand-6.Q16.pc
ImageMagick++.pc    ImageMagick.pc      Magick++.pc     MagickCore.pc       MagickWand.pc       Wand.pc
$
```

これで設定できたので、 sudo に環境変数を引き継ぐように -E をつけてインストール

```:install
$ sudo -E gem install rmagick -v '2.15.4'
Building native extensions.  This could take a while...
Successfully installed rmagick-2.15.4
Parsing documentation for rmagick-2.15.4
Installing ri documentation for rmagick-2.15.4
Done installing documentation for rmagick after 5 seconds
1 gem installed
$
```