2015-03-21

[Ruby] Windows環境に Mechanize をインストールする

特定のウェブページへのアクセスをエミュレートして自動化するためのライブラリに Mechanize があります。  インストール方法をまとめます。

  1. 2015/03/20 現在、Windows 環境では、OneClickInstaller 32bit版(Mingw32系) でのみ動作します。 OneClickInstaller 64bit版 及び ActiveScriptRuby (mswin32系) 32bit/64bit では、インストール時にエラーが発生するようです。

    OS が 64bit の場合も、32bit 版の Ruby を使用してください。

     
  2. 念のため、Development Kit もインストールします。
    こちらから 32bit 版の Development Kit をダウンロードします。 展開したフォルダに移動して下記のコマンドを入力します。


     C:\ruby215\devkit>ruby dk.rb init
    
     [INFO] found RubyInstaller v1.8.7 at C:/Ruby187
     [INFO] found RubyInstaller v1.9.3 at C:/Ruby193
     [INFO] found RubyInstaller v2.1.5 at C:/Ruby215
    
     Initialization complete! Please review and modify the auto-generated
     'config.yml' file to ensure it contains the root directories to all
     of the installed Rubies you want enhanced by the DevKit.
    


    複数のバージョンの Ruby をインストールしている場合は、上記のように複数のインストール先が検出されます。 私の場合は、1.8 と 1.9 は、mswin32系で整合がとれていないので、'config.yml' を開いて、C:/Ruby215 以外の2行を消去してから、インストールコマンドを入力します。


     C:\ruby215\devkit>ruby dk.rb install
    
     [INFO] Updating convenience notice gem override for 'C:/ruby215'
     [INFO] Installing 'C:/ruby215/lib/ruby/site_ruby/devkit.rb'
    


     
  3. gem install mechanize で下の様なエラーが出る場合は、SSL の証明書を設定します。


     C:\ruby215> gem install mechanize
    
     ERROR:  Could not find a valid gem 'mechanize' (>= 0), here is why:
               Unable to download data from https://rubygems.org/ - SSL_connect retur
     ned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (
     https://api.rubygems.org/latest_specs.4.8.gz)
    


    こちらから証明書ファイル AddTrustExternalCARoot-2048.pem をダウンロードして、rubygemsフォルダ下の ssl_certs フォルダにコピーします。 rubygems フォルダの場所は下のように確認します。


     C:\ruby215>gem which rubygems
     
     C:/ruby215/lib/ruby/2.1.0/rubygems.rb
    
    
     C:\ruby215>copy AddTrustExternalCARoot-2048.pem C:\ruby215\lib\ruby\2.1.0\rubygems\ssl_certs
    
            1 個のファイルをコピーしました。
    



     
  4. 再度、インストールコマンドを入力します。


     C:\ruby215>gem install mechanize
    
     Fetching: net-http-digest_auth-1.4.gem (100%)
     Successfully installed net-http-digest_auth-1.4
     Fetching: net-http-persistent-2.9.4.gem (100%)
     Successfully installed net-http-persistent-2.9.4
         :
         :
     12 gems installed