akishin999の日記

調べた事などを書いて行きます。

Windows の Ruby で native extension を使う

以前 Windows 環境で Cassandra を触った時に書いた RubyInstaller で native extension を使うための Development Kit 導入方法が変わったようなのでメモです。

RubyInstaller のインストール

まずは RubyInstaller のインストールからです。


Downloads
http://rubyinstaller.org/downloads/


上記の公式サイトダウンロードページ内の「7-Zip Archives」カテゴリより、本体をダウンロードし、適当なディレクトリに展開します。
ここでは、以下の場所に展開しました。

C:\Programs\ruby-1.8.7-p302-i386-mingw32


以降の説明は上記に配置したものとして書いています。
お使いの環境に合わせてディレクトリのパス等は適宜読み替えて下さい。


配置が完了したら「Ruby をインストールしたディレクトリ\bin」を環境変数 PATH に追加します。
が、ここでは後々の複数バージョン導入を考慮して、環境変数設定済みのコマンドプロンプトショートカットを用意することにしました。


インストールしたディレクトリ直下に、以下のようなバッチファイルを作成します。

@set RUBY_HOME=C:\Programs\ruby-1.8.7-p302-i386-mingw32
@set PATH=%PATH%;%RUBY_HOME%\bin
@cd C:\src\ruby
@ruby -v


次に、コマンドプロンプトのショートカットをコピーして、適当な名前に変更します。
コピーしたショートカットのプロパティを表示し、その中の「リンク先」を以下のように変更します。

%SystemRoot%\system32\cmd.exe /k call C:\Programs\ruby-1.8.7-p302-i386-mingw32\ruby_env.bat


これで、このショートカットから起動した場合に、今回導入した Ruby に PATH が通った状態でコマンドプロンプトが起動してくれます。
起動してバージョンが正常に表示される事を確認してみましょう。

ruby 1.8.7 (2010-08-16 patchlevel 302) [i386-mingw32]


ちゃんと表示されていれば OK です。


コマンドプロンプトを起動したついでに、以下も実行してみます。

C:\src\ruby>gem -v
1.3.7


RubyGems もインストールされています。

Development Kit のインストール


次に同じくダウンロードページの「Development Kit」カテゴリより、Development Kit をダウンロードします。
現時点では「DevKit-4.5.0-20100819-1536-sfx.exe」という自己解凍形式のファイルが配布されていたので、そちらをダウンロードしました。


ダウンロードした 7-Zip 書庫を適当なディレクトリに展開して下さい。
以下のようなファイル・フォルダが出てくると思います。

DevKit-4.5.0-20100819-1536
├─devkitvars.bat
├─dk.rb
├─m.ico
├─msys.bat
├─msys.ico
├─perllocal.pod
├─bin 
├─etc
├─include
├─lib
├─mingw
├─postinstall
├─sbin
└─share


次に、先ほど作成したバッチファイルより、 Ruby に PATH が通った状態のコマンドプロンプトを起動し、Development Kit を展開したディレクトリに移動します。

ディレクトリの移動が完了したら、以下を実行します。

C:\Programs\DevKit-4.5.0-20100819-1536>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.


上記のようなメッセージが表示され、Development Kit を展開したディレクトリ内に「config.yml」が作成されます。
「config.yml」をテキストエディタで開き、以下のように先ほどインストールした RubyInstaller のディレクトリを記述します。

# 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:/Programs/ruby-1.8.7-p302-i386-mingw32


記述が完了したら、正しく記述できている事を確認するため、「ruby dk.rb review」を実行します。

C:\Programs\DevKit-4.5.0-20100819-1536>ruby dk.rb review
Based upon the settings in the 'config.yml' file generated
from running 'ruby dk.rb init' and any of your customizations,
DevKit functionality will be injected into the following Rubies
when you run 'ruby dk.rb install'.

C:/Programs/ruby-1.8.7-p302-i386-mingw32


上記のように表示されていれば記述した Ruby の PATH が正しく認識されています。
確認できたら「ruby dk.rb install」を実行し、インストールを行います。

C:\Programs\DevKit-4.5.0-20100819-1536>ruby dk.rb install
[INFO] Installing C:/Programs/ruby-1.8.7-p302-i386-mingw32/lib/ruby/site_ruby/1.8/rubygems/defaults/operating_system.rb
[INFO] Installing C:/Programs/ruby-1.8.7-p302-i386-mingw32/lib/ruby/site_ruby/devkit.rb


これで RubyInstaller と Development Kit のインストールは完了です。
それでは、 native extension がインストールできるかどうかを確認してみましょう。


「gem install rdiscount --platform=ruby」を実行し、native extension をインストールしてみます。

C:\Programs\ruby-1.8.7-p302-i386-mingw32>gem install rdiscount --platform=ruby
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
Successfully installed rdiscount-1.6.5
1 gem installed
Installing ri documentation for rdiscount-1.6.5...
Installing RDoc documentation for rdiscount-1.6.5...


「Temporarily enhancing PATH to include DevKit...」と表示され、 Development Kit が使用されて native extension がインストールされた事が分かります。


上記のライブラリが正常にインストールできたかを以下で確認します。

C:\Programs\ruby-1.8.7-p302-i386-mingw32>ruby -rubygems -e "require 'rdiscount'; puts RDiscount.new('**Hello RubyInstaller**').to_html"
<p><strong>Hello RubyInstaller</strong></p>


上記のように HTML タグで囲まれた「Hello RubyInstaller」が出力されればインストールが成功しています。

以上で、Windows 環境でも Ruby の native extension をインストールすることができるようになりました。