ほめどころ

がうがう

windows7にrubygemのmechanizeがインストールできない

自宅で使ってるwin7をテスト機にしようと思ってruby環境を整えようとしたが何かと苦労した。とりあえずrubyとgemはインストーラーから無事入れられて、次に割とよく使うmechanizeを入れようとしたらエラーでだいぶ時間かかった。後学のためにメモ。



まず、インストール。
これはエラー文をググったらすぐ出てきた。

C:\Ruby200-x64>gem install mechanize
ERROR:  Error installing mechanize:
        The 'unf_ext' native gem requires installed build tools.

コンパイラがないかららしい。
ということでインストーラーと同じところからダウンロード。

http://rubyinstaller.org/downloads/
ここの
DEVELOPMENT KIT
から
For use with Ruby 2.0 (x64 – 64bits only)
DevKit-mingw64-64-4.7.2-20130224-1432-sfx.exe
こちらをダウンロード

展開したものを、rubyをインストールしたフォルダに丸ごと上書きしてコマンドの実行。
この辺の流れもググれば出てきた。

C:\Ruby200-x64>ruby dk.rb init

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.

Initialization complete!かーらーのーインストールでエラー!

C:\Ruby200-x64>ruby dk.rb install
Invalid configuration or no Rubies listed. Please fix 'config.yml'
and rerun 'ruby dk.rb install'

config.ymlは自動で書き込まれるものじゃなかったのか・・・
ということでconfig.ymlを編集。

# This configuration file contains the absolute path locations of all
# installed Rubies to be enhanced to work with the DevKit. This config
# file is generated by the 'ruby dk.rb init' step and may be modified
# before running the 'ruby dk.rb install' step. To include any installed
# Rubies that were not automagically discovered, simply add a line below
# the triple hyphens with the absolute path to the Ruby root directory.
#
# Example:
#
# ---
# - C:/ruby19trunk
# - C:/ruby192dev
#
---
 - C:/Ruby200-x64

ファイルの一番最後に
- C:/Ruby200-x64
を追加

再びインストールしようとしたら、また別のエラー

C:\Ruby200-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:/Ruby200-x64/bin/ruby.exe extconf.rb
checking for libxml/parser.h... no
-----
libxml2 is missing.  please visit http://nokogiri.org/tutorials/installing_nokog
iri.html for help with installing dependencies.
-----
*** 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:/Ruby200-x64/bin/ruby
        --with-zlib-dir
        --without-zlib-dir
        --with-zlib-include
        --without-zlib-include=${zlib-dir}/include
        --with-zlib-lib
        --without-zlib-lib=${zlib-dir}/lib
        --with-iconv-dir
        --without-iconv-dir
        --with-iconv-include
        --without-iconv-include=${iconv-dir}/include
        --with-iconv-lib
        --without-iconv-lib=${iconv-dir}/lib
        --with-xml2-dir
        --without-xml2-dir
        --with-xml2-include
        --without-xml2-include=${xml2-dir}/include
        --with-xml2-lib
        --without-xml2-lib=${xml2-dir}/lib
        --with-xslt-dir
        --without-xslt-dir
        --with-xslt-include
        --without-xslt-include=${xslt-dir}/include
        --with-xslt-lib
        --without-xslt-lib=${xslt-dir}/lib
        --with-libxslt-config
        --without-libxslt-config
        --with-pkg-config
        --without-pkg-config
        --with-libxml-2.0-config
        --without-libxml-2.0-config
        --with-pkg-config
        --without-pkg-config
        --with-libiconv-config
        --without-libiconv-config
        --with-pkg-config
        --without-pkg-config


Gem files will remain installed in C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/nokog
iri-1.6.1 for inspection.
Results logged to C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.1/ext/nok
ogiri/gem_make.out

mechanizeの内部で使ってるnokogiriがうまくインストールできないせいらしい

libxml2
libxslt
がないとダメということなんだけど、windowsではそれのインストールすら一苦労。
全部バンドルしたやつがあるだろうと適当にググったらそれっぽいのがあった。

こちらで言及されているgemファイルを直接インストールした
https://github.com/sparklemotion/nokogiri/pull/989#issuecomment-29111842
here for downloadのやつね

gem install -l D:\ruby\nokogiri-1.6.1.beta.1.mingw.1-x86-mingw32.gem

からの最新版にしてー

gem update

テスト!!

require 'rubygems'
require 'mechanize'

agent = Mechanize.new
page = agent.get('http://bestgems.org/')
p page
#<Mechanize::Page
 {url #<URI::HTTP:0x00000003334d68 URL:http://bestgems.org/>}
 {meta_refresh}
 {title "BestGems -- Ruby Gems Download Ranking"}
 {iframes}
 {frames}
 {links
  #<Mechanize::Page::Link "Top" "/">
  #<Mechanize::Page::Link "Total" "/total">
  #<Mechanize::Page::Link "Daily" "/daily">
  #<Mechanize::Page::Link "Featured" "/featured">
  #<Mechanize::Page::Link "About" "/about">
  #<Mechanize::Page::Link "BestGems" "/">
  #<Mechanize::Page::Link "Total Downloads Ranking" "/total">
  #<Mechanize::Page::Link "rake" "/gems/rake">
  #<Mechanize::Page::Link "rack" "/gems/rack">
  #<Mechanize::Page::Link "thor" "/gems/thor">
  #<Mechanize::Page::Link "activesupport" "/gems/activesupport">
  #<Mechanize::Page::Link "json" "/gems/json">
  #<Mechanize::Page::Link "multi_json" "/gems/multi_json">
  #<Mechanize::Page::Link "mime-types" "/gems/mime-types">
  #<Mechanize::Page::Link "rails" "/gems/rails">
  #<Mechanize::Page::Link "i18n" "/gems/i18n">
  #<Mechanize::Page::Link "builder" "/gems/builder">
  #<Mechanize::Page::Link "more..." "/total">
  #<Mechanize::Page::Link "Daily Downloads Ranking" "/daily">
  #<Mechanize::Page::Link "rake" "/gems/rake">
  #<Mechanize::Page::Link "multi_json" "/gems/multi_json">
  #<Mechanize::Page::Link "rack" "/gems/rack">
  #<Mechanize::Page::Link "json" "/gems/json">
  #<Mechanize::Page::Link "activesupport" "/gems/activesupport">
  #<Mechanize::Page::Link "mime-types" "/gems/mime-types">
  #<Mechanize::Page::Link "builder" "/gems/builder">
  #<Mechanize::Page::Link "i18n" "/gems/i18n">
  #<Mechanize::Page::Link "thor" "/gems/thor">
  #<Mechanize::Page::Link "tzinfo" "/gems/tzinfo">
  #<Mechanize::Page::Link "more..." "/daily">
  #<Mechanize::Page::Link "Featured Gems Ranking" "/featured">
  #<Mechanize::Page::Link "lite_config" "/gems/lite_config">
  #<Mechanize::Page::Link "padrino-support" "/gems/padrino-support">
  #<Mechanize::Page::Link "subprocess" "/gems/subprocess">
  #<Mechanize::Page::Link "dotenv-deployment" "/gems/dotenv-deployment">
  #<Mechanize::Page::Link
   "dep-selector-libgecode"
   "/gems/dep-selector-libgecode">
  #<Mechanize::Page::Link "arvados" "/gems/arvados">
  #<Mechanize::Page::Link "semverse" "/gems/semverse">
  #<Mechanize::Page::Link "omniauth-campus" "/gems/omniauth-campus">
  #<Mechanize::Page::Link "slack-notifier" "/gems/slack-notifier">
  #<Mechanize::Page::Link "uber" "/gems/uber">
  #<Mechanize::Page::Link "more..." "/featured">
  #<Mechanize::Page::Link "Number of All Gems" "/stat/gems">
  #<Mechanize::Page::Link "Download Trends of All Gems" "/stat/download">
  #<Mechanize::Page::Link "H2, 2013" "/reports/2013H2">
  #<Mechanize::Page::Link "Q4, 2013" "/reports/2013Q4">
  #<Mechanize::Page::Link "Q3, 2013" "/reports/2013Q3">}
 {forms
  #<Mechanize::Form
   {name nil}
   {method "GET"}
   {action "/search"}
   {fields [text:0x1a9a99c type: text name: q value: ]}
   {radiobuttons}
   {checkboxes}
   {file_uploads}
   {buttons [button:0x1a9e5ec type: submit name:  value: ]}>}>

やったー!

windowsrubyは荊の道・・・