64bit版ruby2.1でmechanizeがインストールできない

超絶面倒だったのでメモ。

64bit版でRuby2.1をインストールし、そこからmechanizeをインストールしようとして失敗しました。
結論から言うと、32bit版を使用することでお茶を濁しています。
ので、解決策を探索してきた人はごめんなさい。

1. SSLの証明書エラー

Ruby2.1をインストール後、(最近はrubygemsがデフォルトでパッケージングされているので)gem を使って

gem install mechanize

と気持ちよく打つと、

Gem::RemoteFetcher::FetchError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

と気持ち悪く返されます。これは、Ruby側の不具合で、Ruby2.1.x系+Windowsでだけ出る模様。
詳細は以下のサイトを参照ですが、要は対応する証明書が入っていないことが原因。
AddTrustExternalCARoot-2048.pem を lib\ruby\2.1.0\rubygems\ssl_certs にブチ込んで解決。
 http://qiita.com/shimoju/items/394818b4989b94680aaf

2. devkit入っていないエラー

気を取り直して再度 gem install mechanize すると、
devkitをインストールしてから出直してこいやコラと言われる。
まぁ、これはRuby使ってる人ならよくある話で、本家から対応するDevkitパッケージをダウンロード、
展開、Rubyディレクトリに上書きしてから対象ディレクトリ上で、

ruby dk.rb init
ruby dk.rb install

をやる。どうも昔は init の後で config.ymlを手動書き換えする必要があったようだが、今では自動生成でちゃんとうまいことやってくれる。
ちなみに複数バージョンのRubyをインストールしている諸氏は、複数バージョンに適用できるようにconfig.yml自体は生成される。
が、コピー上書きしたバージョンは1つなので、installがスムーズにいくかは甚だ怪しい……

3. unf_ext問題

さてここから本番だ。

更に気を取り直して三度目の正直をすると、以下のようになる。これは困っている所なので、助け募集の意味も込めてちゃんと全部載せる。

C:\Ruby21-x64>gem install mechanize
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
ERROR:  Error installing mechanize:
        ERROR: Failed to build gem native extension.

    C:/Ruby21-x64/bin/ruby.exe -r ./siteconf20150125-532-1n8ym3z.rb extconf.rb
checking for main() in -lstdc++... *** 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=C:/Ruby21-x64/bin/ruby
        --with-stdc++lib
        --without-stdc++lib
C:/Ruby21-x64/lib/ruby/2.1.0/mkmf.rb:456:in `try_do': The compiler failed to gen
erate an executable file. (RuntimeError)
You have to install development tools first.
        from C:/Ruby21-x64/lib/ruby/2.1.0/mkmf.rb:541:in `try_link0'
        from C:/Ruby21-x64/lib/ruby/2.1.0/mkmf.rb:556:in `try_link'
        from C:/Ruby21-x64/lib/ruby/2.1.0/mkmf.rb:742:in `try_func'
        from C:/Ruby21-x64/lib/ruby/2.1.0/mkmf.rb:973:in `block in have_library'

        from C:/Ruby21-x64/lib/ruby/2.1.0/mkmf.rb:918:in `block in checking_for'

        from C:/Ruby21-x64/lib/ruby/2.1.0/mkmf.rb:351:in `block (2 levels) in po
stpone'
        from C:/Ruby21-x64/lib/ruby/2.1.0/mkmf.rb:321:in `open'
        from C:/Ruby21-x64/lib/ruby/2.1.0/mkmf.rb:351:in `block in postpone'
        from C:/Ruby21-x64/lib/ruby/2.1.0/mkmf.rb:321:in `open'
        from C:/Ruby21-x64/lib/ruby/2.1.0/mkmf.rb:347:in `postpone'
        from C:/Ruby21-x64/lib/ruby/2.1.0/mkmf.rb:917:in `checking_for'
        from C:/Ruby21-x64/lib/ruby/2.1.0/mkmf.rb:968:in `have_library'
        from extconf.rb:2:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/unf_ex
t-0.0.6 for inspection.
Results logged to C:/Ruby21-x64/lib/ruby/gems/2.1.0/extensions/x64-mingw32/2.1.0
/unf_ext-0.0.6/gem_make.out

ちなみに、gem_make.out は上記と似たようなものが出ているだけだが、mkmf.log はこんなものが出てる。

"x86_64-w64-mingw32-gcc -o conftest.exe -IC:/Ruby21-x64/include/ruby-2.1.0/x64-mingw32 -IC:/Ruby21-x64/include/ruby-2.1.0/ruby/backward -IC:/Ruby21-x64/include/ruby-2.1.0 -I. -DFD_SETSIZE=2048 -D_WIN32_WINNT=0x0501 -D__MINGW_USE_VC2005_COMPAT -D_FILE_OFFSET_BITS=64   -O3 -fno-omit-frame-pointer -fno-fast-math -g -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration conftest.c  -L. -LC:/Ruby21-x64/lib -L.      -lx64-msvcrt-ruby210  -lshell32 -lws2_32 -liphlpapi -limagehlp -lshlwapi  "
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: #include <winsock2.h>
4: #include <windows.h>
5: int main(int argc, char **argv)
6: {
7:   return 0;
8: }
/* end */

さっぱりわからない。
ライブラリが足りないと思う、というのはいいんだが、何が足りないかくらいは教えてくれてもバチが当たらないと思う。

ということで色々ネットをさまよったのだが、どうやら mechanize (nokogiri) が64bit環境では怪しいかも、という記載に行きついた。
 http://www.ownway.info/Ruby/index.php?mechanize%2Fabout
 「インストール」の項目

ということで、ダメモトで32bit版を再インストールして上記手順をなぞると、なんとうまくいくではないか。
今日半日返して……