WindowsにRails3環境を構築(Ruby1.9.2)

目標

WindowsXPの環境にRuby1.9.2とRails3をインストールし、Rails3アプリケーションが動作することを確認する。

所要時間

5分ぐらい(RDocのインストール時間による)

目次

  • Ruby(とrubygems)のインストール
  • SQLite3のDLLのインストール
  • Ruby on Rails3のインストール
  • Railsプロジェクトの作成
  • サーバの起動

Ruby(とrubygems)のインストール

RubyInstallerというのを使います。
ホーム:http://rubyforge.org/projects/rubyinstaller/
ダウンロード:rubyinstaller-1.9.2-p0.exe(2010.9.3の最新版)

ダウンロードしたらインストールしてください。
インストールが終ったらRubyが正しくインストールされたかどうかを確認する。
コマンドプロンプトを起動して、で

C:\>ruby -v
ruby 1.9.2p0 (2010-08-18) [i386-mingw32]

となればOK(入力したのはruby -vの部分です。)

また、irbというRubyの対話型の実行環境もインストールされているのでそちらも確認

C:\>irb
irb(main):001:0> p 'Hello World!'
"Hello World!"
=> "Hello World!"
irb(main):002:0> 1 + 3
=> 4
irb(main):003:0>

irbでは1行ごとにプログラムを実行してくれます。
楽しいですね。

最後にrubygemsというパッケージ管理システムも同時にインストールされているはずなので、こちらも確認

C:\>gem -v
1.3.7

こんな感じになっていればよいはず。

SQLite3のDLLのインストール

次にRailsが標準でつかうデータベースであるSQLite3のためのDLLを予め用意しておきます。
公式サイト:http://www.sqlite.org/
ダウンロード:sqlitedll-3_7_2.zip

ダウンロードしたら、sqlite3.dllをRubyをインストールしたディレクトリのbinの中に配置します。
(デフォルトではC:\Ruby192\bin)

Ruby on Rails3のインストール

ではいよいよRailsをインストールします。
Railsはgemコマンドからインストールできます。

C:\>gem install rails
Successfully installed activesupport-3.0.0
Successfully installed builder-2.1.2
Successfully installed i18n-0.4.1
Successfully installed activemodel-3.0.0
Successfully installed rack-1.2.1
Successfully installed rack-test-0.5.4
Successfully installed rack-mount-0.6.13
Successfully installed tzinfo-0.3.23
Successfully installed abstract-1.0.0
Successfully installed erubis-2.6.6
Successfully installed actionpack-3.0.0
Successfully installed arel-1.0.1
Successfully installed activerecord-3.0.0
Successfully installed activeresource-3.0.0
Successfully installed mime-types-1.16
Successfully installed polyglot-0.3.1
Successfully installed treetop-1.4.8
Successfully installed mail-2.2.5
Successfully installed actionmailer-3.0.0
Successfully installed thor-0.14.0
Successfully installed railties-3.0.0
Successfully installed bundler-1.0.0
Successfully installed rails-3.0.0
23 gems installed
Installing ri documentation for activesupport-3.0.0...
Installing ri documentation for builder-2.1.2...
Installing ri documentation for i18n-0.4.1...
Installing ri documentation for activemodel-3.0.0...
Installing ri documentation for rack-1.2.1...
Installing ri documentation for rack-test-0.5.4...
Installing ri documentation for rack-mount-0.6.13...
Installing ri documentation for tzinfo-0.3.23...
Installing ri documentation for abstract-1.0.0...
Installing ri documentation for erubis-2.6.6...
Installing ri documentation for actionpack-3.0.0...
Installing ri documentation for arel-1.0.1...
Installing ri documentation for activerecord-3.0.0...
Installing ri documentation for activeresource-3.0.0...
Installing ri documentation for mime-types-1.16...
Installing ri documentation for polyglot-0.3.1...
Installing ri documentation for treetop-1.4.8...
Installing ri documentation for mail-2.2.5...
Installing ri documentation for actionmailer-3.0.0...
Installing ri documentation for thor-0.14.0...
Installing ri documentation for railties-3.0.0...
Installing ri documentation for bundler-1.0.0...
Installing ri documentation for rails-3.0.0...
Installing RDoc documentation for activesupport-3.0.0...
Installing RDoc documentation for builder-2.1.2...
Installing RDoc documentation for i18n-0.4.1...
Installing RDoc documentation for activemodel-3.0.0...
Installing RDoc documentation for rack-1.2.1...
Installing RDoc documentation for rack-test-0.5.4...
Installing RDoc documentation for rack-mount-0.6.13...
Installing RDoc documentation for tzinfo-0.3.23...
Installing RDoc documentation for abstract-1.0.0...
Installing RDoc documentation for erubis-2.6.6...
Installing RDoc documentation for actionpack-3.0.0...
Installing RDoc documentation for arel-1.0.1...
Installing RDoc documentation for activerecord-3.0.0...
Installing RDoc documentation for activeresource-3.0.0...
Installing RDoc documentation for mime-types-1.16...
Installing RDoc documentation for polyglot-0.3.1...
Installing RDoc documentation for treetop-1.4.8...
Installing RDoc documentation for mail-2.2.5...
Installing RDoc documentation for actionmailer-3.0.0...
Installing RDoc documentation for thor-0.14.0...
Installing RDoc documentation for railties-3.0.0...
Installing RDoc documentation for bundler-1.0.0...
Installing RDoc documentation for rails-3.0.0...

これでRuby on Railsのインストールが終りました。RDocのインストールには何分もかかる場合がありますが、あきらめないで下さい。
正しくインストールできたことを確認するためにバージョンをみてみます

c:\>rails -v
Rails 3.0.0

これで下準備は終わりです。いよいよアプリケーション(プロジェクト)を作っていきましょう。

Railsのプロジェクトを作る

まずはプロジェクトのベースとなるファイル群を自動生成させます。
今回はmallという名前のプロジェクトを作成します。

C:\>rails new mall
      create
      create  README
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/views/layouts/application.html.erb
      create  app/mailers
      create  app/models
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  doc
      create  doc/README_FOR_APP
      create  lib
      create  lib/tasks
      create  lib/tasks/.gitkeep
      create  log
      create  log/server.log
      create  log/production.log
      create  log/development.log
      create  log/test.log
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/index.html
      create  public/robots.txt
      create  public/images
      create  public/images/rails.png
      create  public/stylesheets
      create  public/stylesheets/.gitkeep
      create  public/javascripts
      create  public/javascripts/application.js
      create  public/javascripts/controls.js
      create  public/javascripts/dragdrop.js
      create  public/javascripts/effects.js
      create  public/javascripts/prototype.js
      create  public/javascripts/rails.js
      create  script
      create  script/rails
      create  test
      create  test/performance/browsing_test.rb
      create  test/test_helper.rb
      create  test/fixtures
      create  test/functional
      create  test/integration
      create  test/unit
      create  tmp
      create  tmp/sessions
      create  tmp/sockets
      create  tmp/cache
      create  tmp/pids
      create  vendor/plugins
      create  vendor/plugins/.gitkeep

いろいろファイルができましたね。
次に、このプロジェクトに不足しているgemパッケージを自動で取得してもらいます。

C:\>cd mall
C:\mall>bundle install
Fetching source index for http://rubygems.org/
Using rake (0.8.7)
Using abstract (1.0.0)
Using activesupport (3.0.0)
Using builder (2.1.2)
Using i18n (0.4.1)
Using activemodel (3.0.0)
Using erubis (2.6.6)
Using rack (1.2.1)
Using rack-mount (0.6.13)
Using rack-test (0.5.4)
Using tzinfo (0.3.23)
Using actionpack (3.0.0)
Using mime-types (1.16)
Using polyglot (0.3.1)
Using treetop (1.4.8)
Using mail (2.2.5)
Using actionmailer (3.0.0)
Using arel (1.0.1)
Using activerecord (3.0.0)
Using activeresource (3.0.0)
Using bundler (1.0.0)
Using thor (0.14.0)
Using railties (3.0.0)
Using rails (3.0.0)
Installing sqlite3-ruby (1.3.1)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem
is installed.

今回はsqlite3-rubyが足りなかったので、自動でインストールされました。

サーバの起動

まだなにもないアプリケーションですが、とりあえずサーバを起動してみましょう

C:\mall\>rails s
=> Booting WEBrick
=> Rails 3.0.0 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-09-02 09:49:33] INFO  WEBrick 1.3.1
[2010-09-02 09:49:34] INFO  ruby 1.9.2 (2010-08-18) [i386-mingw32]
[2010-09-02 09:49:34] INFO  WEBrick::HTTPServer#start: pid=1816 port=3000

これでWebサーバが起動しましたので
http://localhost:3000/
にアクセスするとRailsが用意したスタートページが表示されるはずです


今日学んだこと

  • WindowsでのRails3+Ruby1.9.2環境の構築は簡単(になった)
  • プロジェクトの作成はRails new project_name
  • bundlerコマンドで必要なgemの管理ができる
  • サーバの起動はRails s