過去ログ

アフィリエイト広告を利用しています

windows de Ruby 1.8 and Rails 2.3

Rubyはすこし使ったことがあったんだけど今回は本格的に使ってみようかと思い環境Windows上で環境構築してみた。
最終的にはLinuxサーバ上で動作せることになると思うんだけどメインの環境がWindowsなんでなぁ。


環境構築方法は以下から。

Ruby setup

Ruby本体のインストールを行う

  • Ruby-mswin32 (ja)からruby-1.8.7-p72-i386-mswin32.zipをダウンロードする。
  • ruby-1.8.7-p72-i386-mswin32.zipを解凍する
  • 適当なフォルダにコピーする。今回は「C:\dev\ruby」とする
  • スタート→設定→コントロールパネル→システム→詳細設定→環境変数を開く
  • システム環境変数のPathを選択し「編集」ボタンを押す
  • 変数値の一番最後に「;C:\dev\ruby\bin」を追加する。
  • システム変数、環境変数システムプロパティのOKボタンを押し設定を確定させる。
  • スタート→プログラム→アクセサリ→コマンドプロンプトを選択する
  • rubyコマンドが使えるか確認する。バージョンか表示されればOK。

>ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-mswin32]

Dll setup

Rubyのバイナリを動かすときにあると便利なDLLの追加

  • gdbm-1.8.3-1-mingw32.zip、pdcurses-2.60-1-mswin32.zip、readline-4.3-2-mswin32.zip、zlib-1.1.4-1-mswin32.zipを「Index of /win32」からダウンロードする。
  • iconv-1.10-20060516-dll.tar.bz2をLibiconv DLL 1.10-20060516 for Windowsからダウンロードする
  • openssl-0.9.8k-win32-bin_dynamic.zipをwww.limber.jp - 主に IT関連情報を中心に更新中からダウンロードする
  • ダウンロードしたすべてのファイルを展開する。
  • 「gdbm-1.8.3-1-mswin32\bin」以下のファイルを「C:\dev\ruby\bin」にコピーする
  • 「pdcurses-2.60-1-mswin32\bin」以下のファイルを「C:\dev\ruby\bin」にコピーする
  • 「readline-4.3-2-mswin32\bin」以下のファイルを「C:\dev\ruby\bin」にコピーする
  • 「zlib-1.1.4-1-mswin32\bin」以下のファイルを「C:\dev\ruby\bin」にコピーする
  • 「iconv-1.10-20060516-dll\iconv.dll」を「C:\dev\ruby\bin」にコピーする
  • 「openssl-0.9.8k-win32-bin_dynamic」のlibeay32.dll、ssleay32.dllを「C:\dev\ruby\bin」にコピーする

rubygems install

Rubyのライブラリをダウンロードするためツールをインストールする。(参考

>cd c:\dev\ruby\rubygems-1.3.3
>ruby setup.rb
Installing RubyGems
Installing gem executable
Removing old source_cache files
Removing old RubyGems RDoc and ri
Installing rubygems-1.3.3 ri into C:/dev/ruby/lib/ruby/gems/1.8/doc/rubygems-1.3
.3/ri
Installing rubygems-1.3.3 rdoc into C:/dev/ruby/lib/ruby/gems/1.8/doc/rubygems-1
.3.3/rdoc
〜中略〜

RubyGems installed the following executables:
C:/dev/ruby/bin/gem

  • 「gem -v」を実行してインストールされているか確認する

>gem -v
1.3.3

  • 確認が出来た時点で「c:\dev\ruby\rubygems-1.3.3」は削除してしまっても構わない

Rails 2.3 install

WebアプリケーションフレームワークRailsをインストールする。DBはsqliteを使用する。(参考1参考2

  • 「gem install rails -y」コマンドでRailsをインストールする

C:\dev\ruby>gem install rails -y
INFO: `gem install -y` is now default and will be removed
INFO: use --ignore-dependencies to install only the gems you list
Successfully installed rake-0.8.7
Successfully installed activesupport-2.3.2
Successfully installed activerecord-2.3.2
Successfully installed actionpack-2.3.2
Successfully installed actionmailer-2.3.2
Successfully installed activeresource-2.3.2
Successfully installed rails-2.3.2
7 gems installed
Installing ri documentation for rake-0.8.7...
Installing ri documentation for activesupport-2.3.2...
Installing ri documentation for activerecord-2.3.2...
Installing ri documentation for actionpack-2.3.2...
Installing ri documentation for actionmailer-2.3.2...
Installing ri documentation for activeresource-2.3.2...
Installing ri documentation for rails-2.3.2...
Installing RDoc documentation for rake-0.8.7...
Installing RDoc documentation for activesupport-2.3.2...
Installing RDoc documentation for activerecord-2.3.2...
Installing RDoc documentation for actionpack-2.3.2...
Installing RDoc documentation for actionmailer-2.3.2...
Installing RDoc documentation for activeresource-2.3.2...
Installing RDoc documentation for rails-2.3.2...

C:\dev\ruby>

  • Railsがインストールされているか確認する

C:\dev\ruby>rails -v
Rails 2.3.2

C:\dev\ruby>gem install sqlite3-ruby -v 1.2.3.2.3'
ERROR: could not find gem sqlite3-ruby locally or in a repository

C:\dev\ruby>gem install sqlite3-ruby -v 1.2.3
Successfully installed sqlite3-ruby-1.2.3-x86-mswin32
1 gem installed
Installing ri documentation for sqlite3-ruby-1.2.3-x86-mswin32...
Installing RDoc documentation for sqlite3-ruby-1.2.3-x86-mswin32...

C:\dev\ruby>

  • SqliteのDllインストール。SQLite Download PageのPrecompiled Binaries For Windowsからsqlite-3_6_14_1.zip*2とsqlitedll-3_6_14_1.zipをダウンロードする
  • ダウンロードしたファイルを解凍する
  • sqlite3.exeとsqlite3.dllを「C:\dev\ruby\bin」へコピーする
  • Proxyがある場合はProxy設定をする。*3

set HTTP_PROXY=http://プロキシサーバ名:ポート番号

Rails Test

RailsでTodoアプリを作ってみる。(参考

  • テストするフォルダは「C:\dev\ruby\test」以下とする。
  • rails -d sqlite3 todo」で設定ファイルを作る

C:\dev\ruby\test>rails -d sqlite3 todo
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create config/initializers
create config/locales
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
create test/fixtures
create test/functional
create test/integration
create test/performance
create test/unit
create vendor
create vendor/plugins
create tmp/sessions
create tmp/sockets
create tmp/cache
create tmp/pids
create Rakefile
create README
create app/controllers/application_controller.r
create app/helpers/application_helper.rb
create config/database.yml
create config/routes.rb
create config/locales/en.yml
create config/initializers/backtrace_silencers.
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/new_rails_defaults.r
create config/initializers/session_store.rb
create config/environment.rb
create config/boot.rb
create config/environments/production.rb
create config/environments/development.rb
create config/environments/test.rb
create script/about
create script/console
create script/dbconsole
create script/destroy
create script/generate
create script/runner
create script/server
create script/plugin
create script/performance/benchmarker
create script/performance/profiler
create test/test_helper.rb
create test/performance/browsing_test.rb
create public/404.html
create public/422.html
create public/500.html
create public/index.html
create public/favicon.ico
create public/robots.txt
create public/images/rails.png
create public/javascripts/prototype.js
create public/javascripts/effects.js
create public/javascripts/dragdrop.js
create public/javascripts/controls.js
create public/javascripts/application.js
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log

C:\dev\ruby\test>

  • rakeコマンドでデータベースを構築する

C:\dev\ruby\test>cd todo

C:\dev\ruby\test\todo>rake db:create:all
(in C:/dev/ruby/test/todo)

  • generateを使用して設定を構築する

C:\dev\ruby\test\todo>ruby script/generate scaffold Todo title:string body:text
done:boolean due:datetime
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/todos
exists app/views/layouts/
exists test/functional/
exists test/unit/
create test/unit/helpers/
exists public/stylesheets/
create app/views/todos/index.html.erb
create app/views/todos/show.html.erb
create app/views/todos/new.html.erb
create app/views/todos/edit.html.erb
create app/views/layouts/todos.html.erb
create public/stylesheets/scaffold.css
create app/controllers/todos_controller.rb
create test/functional/todos_controller_test.rb
create app/helpers/todos_helper.rb
create test/unit/helpers/todos_helper_test.rb
route map.resources :todos
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/todo.rb
create test/unit/todo_test.rb
create test/fixtures/todos.yml
create db/migrate
create db/migrate/20090522145906_create_todos.rb

C:\dev\ruby\test\todo>

  • DBにmigrateする

C:\dev\ruby\test\todo>rake db:migrate
(in C:/dev/ruby/test/todo)
== CreateTodos: migrating ====================================================

    • create_table(:todos)

-> 0.0000s
== CreateTodos: migrated (0.0000s) ===========================================

  • Railsの組み込みWebサーバを立ち上げて動作テスト*4

C:\dev\ruby\test\todo>ruby script/server
=> Booting WEBrick
=> Rails 2.3.2 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2009-05-23 00:01:01] INFO WEBrick 1.3.1
[2009-05-23 00:01:01] INFO ruby 1.8.7 (2008-08-11) [i386-mswin32]
[2009-05-23 00:01:01] INFO WEBrick::HTTPServer#start: pid=196 port=3000

Listing todos
Title Body Done Due

New todo

おわりに

ということでこれで最低限の使える環境ができたと。
とりあえず、作りたいものがあるので後はがしがしコード書くだけのはず。ww
参考になるサイトを作ってくださった方に感謝。

*1:最新版は1.2.4だがWindows用のバイナリが準備されていないので1.2.3をインストールする

*2:コマンドラインでメンテナンスしたい場合

*3:常に使う場合は環境変数に追加したほうがいいかも

*4:ファイアウォールなどがインストールされているとブロックされるので注意