Home > Engineer-Soul > HerokuでRuby1.9とRails3を使う

『HerokuでRuby1.9とRails3を使う』

Heroku はRubyで作成したWebアプリケーションをホスティングしてくれるサービスで、Ruby On Railsを無料で動作させることができます。

Heroku上に作成したGitのリポジトリに対してcommitを行うと、Heroku側で処理がフックされRailsがデプロイされるようになります。

2010年6月1日現在、Heroku上ではRuby1.8.6、Ruby1.8.7(beta)、Ruby1.9.1(beta)を使用することができます。また、Rails3で作成したWebアプリケーションを動作させることができるようになっています。

この記事では、Heroku上でRuby1.9.1(beta)を使用し、Rails3をデプロイするところまでの手順をメモしておきます。

※ Heroku 上で動くサンプルアプリ作りました。 Kaffetter(カフェッター) http://kaffetter.heroku.com/

Heroku でアプリを動かす手順

  • Herokuでアカウントを作成する
  • gemでheroku managment APIをインストールする
  • ローカルにRails環境を構築する
  • gitでHeroku上のリポジトリにコミットする
この記事を書くのに使った環境
  • Mac OSX 10.6.3 Snow Leopard
  • Git 1.7.0.3
  • Ruby1.9.1
  • Rails 3.0.0 beta3
  • gem 1.3.6

この記事に関連するキーワード




あらかじめ用意しておくもの

Git
Heroku上のリポジトリにコミットするためにGitを使用します。
SSL接続用の公開鍵
GitでHeroku上のリポジトリにコミットする際に、ホームディレクトリの.sshフォルダにid_rsa.pubという名前で公開鍵が必要になります($HOME/.ssh/id_rsa.pub)。あらかじめssh-keygenコマンド等を使用して作成しておきます。

Heroku上にRails3のWebアプリを公開する

Herokuでアカウントを作成する

http://heroku.com/ にアクセスし、画面上にある「Sign Up」からアカウントを作成します。

Sign Up

メールアドレスを入力すると確認メールが届くので、メール上の認証リンクをたどるとアカウントが作成されます。

gemでheroku managment APIをインストールする

gemを使ってHeroku管理用のAPIをインストールします。

> sudo gem install heroku
Successfully installed heroku-1.9.9
1 gem installed
Installing ri documentation for heroku-1.9.9...
Installing RDoc documentation for heroku-1.9.9...

ローカルにRails環境を構築する

railsコマンドを使用して、Railsの開発環境を作成します。

> rails new heroku-demo
      create
      create README
      create .gitignore
      create Rakefile
      create config.ru
      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/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
> cd heroku-demo

gitでHeroku上のリポジトリにコミットする

git initコマンドを使って、Rails環境をgitで管理し、Heroku上のリポジトリにコミットします。

まずは、git init で開発環境をgitで管理します。

> git init
Initialized empty Git repository in /Users/hamasyou/Documents/Works/work/heroku-demo/.git/

次に、作ったばかりのRails環境をすべてローカルのgitリポジトリにコミットします。

> git add .
> git commit -m "new app"
[master (root-commit) 7b21637] new app
 39 files changed, 9038 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 Gemfile
 create mode 100644 README
 create mode 100644 Rakefile
 create mode 100644 app/controllers/application_controller.rb
 create mode 100644 app/helpers/application_helper.rb
 create mode 100644 app/views/layouts/application.html.erb
 create mode 100644 config.ru
 create mode 100644 config/application.rb
 create mode 100644 config/boot.rb
 create mode 100644 config/database.yml
 create mode 100644 config/environment.rb
 create mode 100644 config/environments/development.rb
 create mode 100644 config/environments/production.rb
 create mode 100644 config/environments/test.rb
 create mode 100644 config/initializers/backtrace_silencers.rb
 create mode 100644 config/initializers/inflections.rb
 create mode 100644 config/initializers/mime_types.rb
 create mode 100644 config/initializers/secret_token.rb
 create mode 100644 config/initializers/session_store.rb
 create mode 100644 config/locales/en.yml
 create mode 100644 config/routes.rb
 create mode 100644 db/seeds.rb
 create mode 100644 doc/README_FOR_APP
 create mode 100644 lib/tasks/.gitkeep
 create mode 100644 public/404.html
 create mode 100644 public/422.html
 create mode 100644 public/500.html
 create mode 100644 public/favicon.ico
 create mode 100644 public/images/rails.png
 create mode 100644 public/index.html
 create mode 100644 public/javascripts/application.js
 create mode 100644 public/javascripts/controls.js
 create mode 100644 public/javascripts/dragdrop.js
 create mode 100644 public/javascripts/effects.js
 create mode 100644 public/javascripts/prototype.js
 create mode 100644 public/javascripts/rails.js
 create mode 100644 public/robots.txt
 create mode 100644 public/stylesheets/.gitkeep
 create mode 100755 script/rails
 create mode 100644 test/performance/browsing_test.rb
 create mode 100644 test/test_helper.rb
 create mode 100644 vendor/plugins/.gitkeep

次に、heroku createコマンドを使って、Heroku上にリポジトリを作成します。

> heroku create heroku-demo
Creating heroku-demo...... done
Created http://heroku-demo.heroku.com/ | git@heroku.com:heroku-demo.git
Git remote heroku added
Notice
最初にheroku createコマンドを実行すると、SSLで接続する旨のメッセージが表示されることがあります。ここであらかじめ用意しておいた$HOME/.ssh/id_rsa.pubが使われます。

最後にHeroku上のリポジトリにローカルのコミットを反映させるのですが、その前に、Heroku上で使用するRubyのバージョンを変更しなければいけません。

heroku stackコマンドを実行すると現在Heroku上でどのRubyのバージョンが使われているのかがわかります。

> heroku stack
* aspen-mri-1.8.6
  bamboo-ree-1.8.7 (beta)
  bamboo-mri-1.9.1 (beta)

Rails3はRuby1.9.1で動かしますので、heroku stack:migrate bamboo-mri-1.9.1と入力してHeroku上のRubyの動作バージョンを変更します。

> heroku stack:migrate bamboo-mri-1.9.1
-----> Preparing to migrate heroku-demo
       aspen-mri-1.8.6 -> bamboo-mri-1.9.1
 
       NOTE: You must specify ALL gems (including Rails) in manifest
 
       Please read the migration guide:        http://docs.heroku.com/bamboo
 
-----> Migration prepared.
       Run 'git push heroku master' to execute migration.

最後に、git push heroku masterコマンドで、Herokuにローカルのコミットを反映させます。

> git push heroku master
Warning: Permanently added the RSA host key for IP address 'xx.xxx.xxx.xx' to the list of known hosts.
Counting objects: 62, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (47/47), done.
Writing objects: 100% (62/62), 80.12 KiB, done.
Total 62 (delta 2), reused 0 (delta 0)
 
-----> Heroku receiving push
-----> Migrating from aspen-mri-1.8.6 to bamboo-mri-1.9.1
 
-----> Gemfile detected, running Bundler
       Unresolved dependencies detected; Installing...
       Fetching source index from http://rubygems.org/
       Using rake (0.8.7) from system gems
       Installing abstract (1.0.0) from rubygems repository at http://rubygems.org/
       Installing builder (2.1.2) from rubygems repository at http://rubygems.org/
       Installing i18n (0.3.7) from rubygems repository at http://rubygems.org/
       Installing memcache-client (1.8.3) from rubygems repository at http://rubygems.org/
       Installing tzinfo (0.3.22) from rubygems repository at http://rubygems.org/
       Installing activesupport (3.0.0.beta3) from rubygems repository at http://rubygems.org/
       Installing activemodel (3.0.0.beta3) from rubygems repository at http://rubygems.org/
       Installing erubis (2.6.5) from rubygems repository at http://rubygems.org/
       Installing rack (1.1.0) from rubygems repository at http://rubygems.org/
       Installing rack-mount (0.6.3) from rubygems repository at http://rubygems.org/
       Installing rack-test (0.5.4) from rubygems repository at http://rubygems.org/
       Installing actionpack (3.0.0.beta3) from rubygems repository at http://rubygems.org/
       Using mime-types (1.16) from system gems
       Installing polyglot (0.3.1) from rubygems repository at http://rubygems.org/
       Installing treetop (1.4.8) from rubygems repository at http://rubygems.org/
       Installing mail (2.2.1) from rubygems repository at http://rubygems.org/
       Installing text-hyphen (1.0.0) from rubygems repository at http://rubygems.org/
       Installing text-format (1.0.0) from rubygems repository at http://rubygems.org/
       Installing actionmailer (3.0.0.beta3) from rubygems repository at http://rubygems.org/
       Installing arel (0.3.3) from rubygems repository at http://rubygems.org/
       Installing activerecord (3.0.0.beta3) from rubygems repository at http://rubygems.org/
       Installing activeresource (3.0.0.beta3) from rubygems repository at http://rubygems.org/
       Installing bundler (0.9.25) from rubygems repository at http://rubygems.org/
       Installing thor (0.13.6) from rubygems repository at http://rubygems.org/
       Installing railties (3.0.0.beta3) from rubygems repository at http://rubygems.org/
       Installing rails (3.0.0.beta3) from rubygems repository at http://rubygems.org/
       Installing sqlite3-ruby (1.2.5) from rubygems repository at http://rubygems.org/
       with native extensions Your bundle is complete! Use `bundle show gemname` to see where a bundled gem is installed.
       Locking environment
-----> Rails app detected
-----> Detected Rails is not set to serve static_assets
       Installing rails3_serve_static_assets... done
       Compiled slug size is 3.9MB
-----> Launching........... done
       http://heroku-demo.heroku.com deployed to Heroku
 
-----> Migration complete, your app is now running on bamboo-mri-1.9.1
 
To git@heroku.com:heroku-demo.git
* [new branch]      master -> master

http://heroku-demo.heroku.com/にアクセスして、Railsの画面が表示されれば完了です。あとは、ローカルで開発して、git push heroku masterでHeroku上にコミットを反映するたびにRailsがデプロイされます。

おすすめする本

Comments:0

Comment Form

Trackbacks:2

TrackBack URL for this entry
http://hamasyou.com/mt/mt-tb.cgi/349
Listed below are links to weblogs that reference
HerokuでRuby1.9とRails3を使う from それはBooks
herokuでgit pushした時のエラーの対処 from GoWest Lab 2010-09-15 (水) 23:05
Rails3のアプリケーションをクラウドサービスの<a href=”http://www.heroku.com”>He...
[ruby]Herokuを使って1日1回名言をツイートするTwitter Botの作り方 from アインシュタインの電話番号☎ 2011-02-09 (水) 09:15
ここ最近、Google App EngineやHerokuを使ってTwitter Botを作ろうと、いろいろ実験していた。以下はその関連記事。 Goog...
Search
Feeds
access

access counter

Return to page top