読者です 読者をやめる 読者になる 読者になる

RVMでJRuby on Rails3

仕事でREST APIっぽいものを持ちつつ、スクレイピングしつつなサポートアプリを作ることになったのでRails使ってみようかなぁとか思い中。ただ運用面でRuby周りのAPサーバが云々とかになると監視だったりが若干厳しいので*1TomcatやらGlassfishやらで動かしたいなーということでJRubyでトライ。

構築して思ったけどやっぱりJVMの起動がある分コマンドが遅い。開発時はrubyで、本番はJRubyでなんてのもやればできるのかな?ちょっとテストを手厚く書くなどしてみて試してみようかな。以下、構築めも。

2011/6/25 追記

$ rvm install jruby-1.6.1

1.9.2APIを利用する為に環境変数を定義。rvm useした際のhookに仕掛けとく。
$HOME/.rvm/hooks/after_useを以下の内容で作成。

case "$GEM_HOME" in
  *jruby*)
    JRUBY_OPTS="--1.9" ; export JRUBY_OPTS
  ;;
esac

お試し用のgemsetを用意。

$ rvm use --create jruby-1.6.1@rails3-try

railsをインストール。3.0.7が最新っぽい?*2

$ gem install rails -v=3.0.7
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8
Fetching: activesupport-3.0.7.gem (100%)
Fetching: builder-2.1.2.gem (100%)
Fetching: i18n-0.5.0.gem (100%)
Fetching: activemodel-3.0.7.gem (100%)
Fetching: rack-1.2.3.gem (100%)
Fetching: rack-test-0.5.7.gem (100%)
Fetching: rack-mount-0.6.14.gem (100%)
Fetching: tzinfo-0.3.28.gem (100%)
Fetching: abstract-1.0.0.gem (100%)
Fetching: erubis-2.6.6.gem (100%)
Fetching: actionpack-3.0.7.gem (100%)
Fetching: arel-2.0.10.gem (100%)
Fetching: activerecord-3.0.7.gem (100%)
Fetching: activeresource-3.0.7.gem (100%)
Fetching: mime-types-1.16.gem (100%)
Fetching: polyglot-0.3.1.gem (100%)
Fetching: treetop-1.4.9.gem (100%)
Fetching: mail-2.2.19.gem (100%)
Fetching: actionmailer-3.0.7.gem (100%)
Fetching: rake-0.9.2.gem (100%)
Fetching: thor-0.14.6.gem (100%)
Fetching: railties-3.0.7.gem (100%)
Fetching: bundler-1.0.15.gem (100%)
Fetching: rails-3.0.7.gem (100%)
Successfully installed activesupport-3.0.7
Successfully installed builder-2.1.2
Successfully installed i18n-0.5.0
Successfully installed activemodel-3.0.7
Successfully installed rack-1.2.3
Successfully installed rack-test-0.5.7
Successfully installed rack-mount-0.6.14
Successfully installed tzinfo-0.3.28
Successfully installed abstract-1.0.0
Successfully installed erubis-2.6.6
Successfully installed actionpack-3.0.7
Successfully installed arel-2.0.10
Successfully installed activerecord-3.0.7
Successfully installed activeresource-3.0.7
Successfully installed mime-types-1.16
Successfully installed polyglot-0.3.1
Successfully installed treetop-1.4.9
Successfully installed mail-2.2.19
Successfully installed actionmailer-3.0.7
Successfully installed rake-0.9.2
Successfully installed thor-0.14.6
Successfully installed railties-3.0.7
Successfully installed bundler-1.0.15
Successfully installed rails-3.0.7
24 gems installed

sqlite辺りもいれておくのが良さそうなのでいれとく。

$ gem install activerecord-jdbcsqlite3-adapter
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8
Fetching: activerecord-jdbc-adapter-1.1.2.gem (100%)
Fetching: jdbc-sqlite3-3.7.2.gem (100%)
Fetching: activerecord-jdbcsqlite3-adapter-1.1.2.gem (100%)
Successfully installed activerecord-jdbc-adapter-1.1.2
Successfully installed jdbc-sqlite3-3.7.2
Successfully installed activerecord-jdbcsqlite3-adapter-1.1.2
3 gems installed

railsのお試しアプリを作る。JRubyで使う場合はhttp://jruby.org/rails3.rbを指定しないといけないらしい。

$ rails new rails3-try -m http://jruby.org/rails3.rb
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8
      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/mailers
      create  app/models
      create  app/views/layouts/application.html.erb
      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/fixtures
      create  test/functional
      create  test/integration
      create  test/performance/browsing_test.rb
      create  test/test_helper.rb
      create  test/unit
      create  tmp
      create  tmp/sessions
      create  tmp/sockets
      create  tmp/cache
      create  tmp/pids
      create  vendor/plugins
      create  vendor/plugins/.gitkeep
       apply  http://jruby.org/rails3.rb
       apply    http://jruby.org/templates/default.rb
        gsub      Gemfile

起動してみる。

$ cd rails3-try
$ script/rails server 
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8
=> Booting WEBrick
=> Rails 3.0.7 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2011-06-23 19:44:04] INFO  WEBrick 1.3.1
[2011-06-23 19:44:04] INFO  ruby 1.9.2 (2011-04-12) [java]
[2011-06-23 19:44:04] INFO  WEBrick::HTTPServer#start: pid=9236 port=3000

http://localhost:3000 にアクセス。して見えたらOK!

*1:ノウハウがないので

*2:3.0.9があったでござる