完全版 : nokogiri ビルドエラー時の対処法

# 追記情報
- **`2017/01/30`**
    - Bundler を利用している場合の対処法を追記しました

# ケース
- `gem intall github-pages` で依存 `nokogiri` のビルドエラーが発生した場合。
- **`2017/01/30`** 追記
    - もしくは `bundle install` など。

# 環境

```shell-session:macOS
$ sw_vers -productVersion
10.11.6
```

```shell-session:homebrew
$ brew --version
Homebrew 0.9.9 (git revision aaf8c; last commit 2016-09-09)
Homebrew/homebrew-core (git revision 0bcc; last commit 2016-09-09)
```

```shell-session:rbenv
$ rbenv --version
rbenv 1.0.0-30-g0c7224a
```

```shell-session:ruby
$ ruby --version
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
```

```shell-session:path
$ echo $PATH | tr ':' '\n'
/Users/tdkn/bin/go_appengine
/Users/tdkn/Downloads/google-cloud-sdk/bin
/Users/tdkn/.rbenv/shims
/Users/tdkn/.rbenv/bin
/usr/local/opt/coreutils/libexec/gnubin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/Users/tdkn/Projects/scripts
/usr/local/heroku/bin
/Users/tdkn/bin
/Users/tdkn/.fzf/bin
```

# インストールエラー

- `github-pages` をインストールしようとすると、依存する `nokogiri` のビルドでエラー。

    ```shell-session:github-pages
    $ gem install github-pages
    Fetching: listen-3.0.6.gem (100%)
    Successfully installed listen-3.0.6
    Fetching: jekyll-sass-converter-1.3.0.gem (100%)
    Successfully installed jekyll-sass-converter-1.3.0
    Fetching: kramdown-1.11.1.gem (100%)
    Successfully installed kramdown-1.11.1
    Fetching: minima-1.1.0.gem (100%)
    Successfully installed minima-1.1.0
    Fetching: net-dns-0.8.0.gem (100%)
    Successfully installed net-dns-0.8.0
    Fetching: public_suffix-1.5.3.gem (100%)
    Successfully installed public_suffix-1.5.3
    Fetching: ethon-0.9.0.gem (100%)
    Successfully installed ethon-0.9.0
    Fetching: typhoeus-0.8.0.gem (100%)
    Successfully installed typhoeus-0.8.0
    Fetching: addressable-2.4.0.gem (100%)
    Successfully installed addressable-2.4.0
    Fetching: multipart-post-2.0.0.gem (100%)
    Successfully installed multipart-post-2.0.0
    Fetching: faraday-0.9.2.gem (100%)
    Successfully installed faraday-0.9.2
    Fetching: sawyer-0.7.0.gem (100%)
    Successfully installed sawyer-0.7.0
    Fetching: octokit-4.3.0.gem (100%)
    Successfully installed octokit-4.3.0
    Fetching: github-pages-health-check-1.2.0.gem (100%)
    Successfully installed github-pages-health-check-1.2.0
    Fetching: mini_portile2-2.1.0.gem (100%)
    Successfully installed mini_portile2-2.1.0
    Fetching: pkg-config-1.1.7.gem (100%)
    Successfully installed pkg-config-1.1.7
    Fetching: nokogiri-1.6.8.gem (100%)
    Building native extensions.  This could take a while...
    ERROR:  Error installing github-pages:
            ERROR: Failed to build gem native extension.

        current directory: /Users/tdkn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/nokogiri-1.6.8/ext/nokogiri
    /Users/tdkn/.rbenv/versions/2.3.1/bin/ruby -r ./siteconf20160910-24696-d6zj5a.rb extconf.rb
    Using pkg-config version 1.1.7
    checking if the C compiler accepts ... yes
    checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
    Building nokogiri using packaged libraries.
    Using mini_portile version 2.1.0
    checking for iconv.h... yes
    checking for gzdopen() in -lz... yes
    checking for iconv... yes
    ************************************************************************
    IMPORTANT NOTICE:

    Building Nokogiri with a packaged version of libxml2-2.9.4.

    Team Nokogiri will keep on doing their best to provide security
    updates in a timely manner, but if this is a concern for you and want
    to use the system library instead; abort this installation process and
    reinstall nokogiri as follows:

        gem install nokogiri -- --use-system-libraries
            [--with-xml2-config=/path/to/xml2-config]
            [--with-xslt-config=/path/to/xslt-config]

    If you are using Bundler, tell it to use the option:

        bundle config build.nokogiri --use-system-libraries
        bundle install

    Note, however, that nokogiri is not fully compatible with arbitrary
    versions of libxml2 provided by OS/package vendors.
    ************************************************************************
    Extracting libxml2-2.9.4.tar.gz into tmp/x86_64-apple-darwin15.6.0/ports/libxml2/2.9.4... OK
    Running 'configure' for libxml2 2.9.4... OK
    Running 'compile' for libxml2 2.9.4... ERROR, review '/Users/tdkn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/nokogiri-1.6.8/ext/nokogiri/tmp/x86_64-apple-darwin15.6.0/ports/libxml2/2.9.4/compile.log' to see what happened. Last lines are:
    ========================================================================
        unsigned short* in = (unsigned short*) inb;
                             ^~~~~~~~~~~~~~~~~~~~~
    encoding.c:815:27: warning: cast from 'unsigned char *' to 'unsigned short *' increases required alignment from 1 to 2 [-Wcast-align]
        unsigned short* out = (unsigned short*) outb;
                              ^~~~~~~~~~~~~~~~~~~~~~
    4 warnings generated.
      CC       error.lo
      CC       parserInternals.lo
      CC       parser.lo
      CC       tree.lo
      CC       hash.lo
      CC       list.lo
      CC       xmlIO.lo
    xmlIO.c:1450:52: error: use of undeclared identifier 'LZMA_OK'
        ret =  (__libxml2_xzclose((xzFile) context) == LZMA_OK ) ? 0 : -1;
                                                       ^
    1 error generated.
    make[2]: *** [xmlIO.lo] Error 1
    make[1]: *** [all-recursive] Error 1
    make: *** [all] Error 2
    ========================================================================
    *** 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/tdkn/.rbenv/versions/2.3.1/bin/$(RUBY_BASE_NAME)
            --help
            --clean
            --use-system-libraries
            --enable-static
            --disable-static
            --with-zlib-dir
            --without-zlib-dir
            --with-zlib-include
            --without-zlib-include=${zlib-dir}/include
            --with-zlib-lib
            --without-zlib-lib=${zlib-dir}/lib
            --enable-cross-build
            --disable-cross-build
    /Users/tdkn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mini_portile2-2.1.0/lib/mini_portile2/mini_portile.rb:366:in `block in execute': Failed to complete compile task (RuntimeError)
            from /Users/tdkn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mini_portile2-2.1.0/lib/mini_portile2/mini_portile.rb:337:in `chdir'
            from /Users/tdkn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mini_portile2-2.1.0/lib/mini_portile2/mini_portile.rb:337:in `execute'
            from /Users/tdkn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mini_portile2-2.1.0/lib/mini_portile2/mini_portile.rb:111:in `compile'
            from /Users/tdkn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mini_portile2-2.1.0/lib/mini_portile2/mini_portile.rb:150:in `cook'
            from extconf.rb:364:in `block (2 levels) in process_recipe'
            from extconf.rb:257:in `block in chdir_for_build'
            from extconf.rb:256:in `chdir'
            from extconf.rb:256:in `chdir_for_build'
            from extconf.rb:363:in `block in process_recipe'
            from extconf.rb:262:in `tap'
            from extconf.rb:262:in `process_recipe'
            from extconf.rb:555:in `<main>'

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

      /Users/tdkn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/nokogiri-1.6.8/mkmf.log

    extconf failed, exit code 1

    Gem files will remain installed in /Users/tdkn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/nokogiri-1.6.8 for inspection.
    Results logged to /Users/tdkn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/nokogiri-1.6.8/gem_make.out
    ```

# 対処

- 依存ライブラリをインストールする

    ```shell-session:libraries
    $ brew install libxml2 libxslt libiconv
    ==> Auto-updated Homebrew!
    Updated 1 tap (homebrew/core).
    No changes to formulae.

    ==> Downloading https://homebrew.bintray.com/bottles/libxml2-2.9.4.el_capitan.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Pouring libxml2-2.9.4.el_capitan.bottle.tar.gz
    ==> Caveats
    This formula is keg-only, which means it was not symlinked into /usr/local.

    OS X already provides this software and installing another version in
    parallel can cause all kinds of trouble.

    Generally there are no consequences of this for you. If you build your
    own software and it requires this formula, you'll need to add to your
    build variables:

        LDFLAGS:  -L/usr/local/opt/libxml2/lib
        CPPFLAGS: -I/usr/local/opt/libxml2/include

    ==> Summary
    🍺  /usr/local/Cellar/libxml2/2.9.4: 276 files, 9.8M
    ==> Downloading https://homebrew.bintray.com/bottles/libxslt-1.1.28_1.el_capitan.bottle.1.tar.gz
    ######################################################################## 100.0%
    ==> Pouring libxslt-1.1.28_1.el_capitan.bottle.1.tar.gz
    ==> Caveats
    To allow the nokogiri gem to link against this libxslt run:
      gem install nokogiri -- --with-xslt-dir=/usr/local/opt/libxslt

    This formula is keg-only, which means it was not symlinked into /usr/local.

    OS X already provides this software and installing another version in
    parallel can cause all kinds of trouble.

    Generally there are no consequences of this for you. If you build your
    own software and it requires this formula, you'll need to add to your
    build variables:

        LDFLAGS:  -L/usr/local/opt/libxslt/lib
        CPPFLAGS: -I/usr/local/opt/libxslt/include


    If you need Python to find bindings for this keg-only formula, run:
      echo /usr/local/opt/libxslt/lib/python2.7/site-packages >> /usr/local/lib/python2.7/site-packages/libxslt.pth
    ==> Summary
    🍺  /usr/local/Cellar/libxslt/1.1.28_1: 147 files, 3.0M
    ==> Installing libiconv from homebrew/dupes
    ==> Downloading https://homebrew.bintray.com/bottles-dupes/libiconv-1.14.el_capitan.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Pouring libiconv-1.14.el_capitan.bottle.tar.gz
    ==> Caveats
    This formula is keg-only, which means it was not symlinked into /usr/local.

    OS X already provides this software and installing another version in
    parallel can cause all kinds of trouble.

    Generally there are no consequences of this for you. If you build your
    own software and it requires this formula, you'll need to add to your
    build variables:

        LDFLAGS:  -L/usr/local/opt/libiconv/lib
        CPPFLAGS: -I/usr/local/opt/libiconv/include

    ==> Summary
    🍺  /usr/local/Cellar/libiconv/1.14: 28 files, 1.4M
    ```

- `/usr/local/opt` にシンボリックリンクを作成

    ```shell-session:linking
    $ brew link --force libxml2 libxslt libiconv
    Linking /usr/local/Cellar/libxml2/2.9.4... 17 symlinks created
    Linking /usr/local/Cellar/libxslt/1.1.28_1... 22 symlinks created
    Linking /usr/local/Cellar/libiconv/1.14... 16 symlinks created
    ```

- 念のためシェルを再起動

    ```shell-session:zsh
    $ exec zsh -l
    ```

- 再インストール

    ```shell-session:nokogiri
    $ gem install nokogiri -- --use-system-libraries\
      --with-iconv-dir="$(brew --prefix libiconv)"\
      --with-xml2-config="$(brew --prefix libxml2)/bin/xml2-config"\
      --with-xslt-config="$(brew --prefix libxslt)/bin/xslt-config"
    Building native extensions with: '--use-system-libraries --with-iconv-dir=/usr/local/opt/libiconv --with-xml2-config=/usr/local/opt/libxml2/bin/xml2-config --with-xslt-config=/usr/local/opt/libxslt/bin/xslt-config'
    This could take a while...
    Successfully installed nokogiri-1.6.8
    1 gem installed
    ```

- **`2017/01/30`** 追記
    - Bundler を利用している場合

    ```shell-session:bundle&nbsp;install
    $ bundle config build.nokogiri -- --use-system-libraries\
      --with-iconv-dir="$(brew --prefix libiconv)"\
      --with-xml2-config="$(brew --prefix libxml2)/bin/xml2-config"\
      --with-xslt-config="$(brew --prefix libxslt)/bin/xslt-config"
    $ bundle install
    ```

気持ちよく成功。
それでは良い nokogiri ライフを :laughing: