コレグレーデギネード

WindowsとかUbuntuとかRubyとかRailsとか

bundleのpath オプションを利用したプロジェクト作成手順

やりたいこと
ディレクトリ毎にRailsのバージョンを作成、管理したい。

条件
・rbenvの環境下でRuby Version 1.9.3-p448 と 2.0.0-p247 がインストールされていること。
 ※参考:rvm切腹www Rails 3.2.13 + Ruby 1.9.3 と Rails 4.0.0 + Ruby 2.0.0 の環境をrbenvで再構築するべ - コレグレーデギネード
PostgreSQLlibpq-dev)がインストールされていること。
 ※参考:PostgreSQL使いたいのにgem install pgできない。 - コレグレーデギネード

現在の環境

$ ruby --version
ruby 2.0.0p247 (2013-06-27 revision 41674) [i686-linux]

$ rbenv versions
  1.9.3-p448
* 2.0.0-p247 (set by /home/nikaido/.rbenv/version)

現在インストールできるRailsのVersionをgem search(完全一致)で調べておく。

$ gem search -r ^rails$ -a

*** REMOTE GEMS ***

rails (4.0.2, 4.0.1, 4.0.0, 3.2.16, 3.2.15, 3.2.14, 3.2.13, 3.2.12, 3.2.11, 3.2.10, 3.2.9, 3.2.8, 3.2.7, 3.2.6, 3.2.5, 3.2.4, 3.2.3, 3.2.2, 3.2.1, 3.2.0, 3.1.12, 3.1.11, 3.1.10, 3.1.9, 3.1.8, 3.1.7, 3.1.6, 3.1.5, 3.1.4, 3.1.3, 3.1.2, 3.1.1, 3.1.0, 3.0.20, 3.0.19, 3.0.18, 3.0.17, 3.0.16, 3.0.15, 3.0.14, 3.0.13, 3.0.12, 3.0.11, 3.0.10, 3.0.9, 3.0.8, 3.0.7, 3.0.6, 3.0.5, 3.0.4, 3.0.3, 3.0.2, 3.0.1, 3.0.0, 2.3.18, 2.3.17, 2.3.16, 2.3.15, 2.3.14, 2.3.12, 2.3.11, 2.3.10, 2.3.9, 2.3.8, 2.3.7, 2.3.6, 2.3.5, 2.3.4, 2.3.3, 2.3.2, 2.2.3, 2.2.2, 2.1.2, 2.1.1, 2.1.0, 2.0.5, 2.0.4, 2.0.2, 2.0.1, 2.0.0, 1.2.6, 1.2.5, 1.2.4, 1.2.3, 1.2.2, 1.2.1, 1.2.0, 1.1.6, 1.1.5, 1.1.4, 1.1.3, 1.1.2, 1.1.1, 1.1.0, 1.0.0, 0.14.4, 0.14.3, 0.14.2, 0.14.1, 0.13.1, 0.13.0, 0.12.1, 0.12.0, 0.11.1, 0.11.0, 0.10.1, 0.10.0, 0.9.5, 0.9.4.1, 0.9.4, 0.9.3, 0.9.2, 0.9.1, 0.9.0, 0.8.5, 0.8.0)

Railsのバージョンをディレクトリ毎に分ける準備。

Rails Verion 3.*.* プロジェクト保存用ディレクトリを作成。

$ mkdir 3rails

ディレクトリ 3rails 内に Ruby Version1.9.3-p448 をローカル指定。

$ cd 3rails
3rails$ rbenv local 1.9.3-p448

rbenvでBundlerをインストールしておく。

3rails$ rbenv exec gem install bundler
Fetching: bundler-1.3.5.gem (100%)
Successfully installed bundler-1.3.5
1 gem installed
Installing ri documentation for bundler-1.3.5...
Installing RDoc documentation for bundler-1.3.5...

Rails Verion 4.*.* プロジェクト保存用ディレクトリも同様に作成。

$ mkdir 4rails

ディレクトリ 4rails 内に Ruby Version1.9.3-p448 をローカル指定。

$ cd 4rails
4rails$ rbenv local 2.0.0-p247

同様にrbenvでBundlerをインストールする。

4rails$ rbenv exec gem install bundler
Fetching: bundler-1.3.5.gem (100%)
Successfully installed bundler-1.3.5
1 gem installed
Installing ri documentation for bundler-1.3.5...
Installing RDoc documentation for bundler-1.3.5...

bundle init でGemfileを生成。(Version3.*.*)

3rails$ bundle init
Writing new Gemfile to /home/user/3rails/Gemfile

生成された直後のGmefileの中身。

# A sample Gemfile
source "https://rubygems.org"

# gem "rails"

echoコマンドかviなどのエディタでインストールしたいバージョンを指定。

3rails$ echo 'gem "rails", "3.2.16"' >> Gemfile

編集後のGmefileの中身。

# A sample Gemfile
source "https://rubygems.org"

# gem "rails"
gem "rails", "3.2.16"

pathオプションでローカルにRailsをインストール。

3rails$ bundle install --path vendor/bundle
Fetching gem metadata from https://rubygems.org/...........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Installing rake (10.1.0) 
Installing i18n (0.6.9) 
Installing multi_json (1.8.2) 
Installing activesupport (3.2.16) 
Installing builder (3.0.4) 
Installing activemodel (3.2.16) 
Installing erubis (2.7.0) 
Installing journey (1.0.4) 
Installing rack (1.4.5) 
Installing rack-cache (1.2) 
Installing rack-test (0.6.2) 
Installing hike (1.2.3) 
Installing tilt (1.4.1) 
Installing sprockets (2.2.2) 
Installing actionpack (3.2.16) 
Installing mime-types (1.25.1) 
Installing polyglot (0.3.3) 
Installing treetop (1.4.15) 
Installing mail (2.5.4) 
Installing actionmailer (3.2.16) 
Installing arel (3.0.3) 
Installing tzinfo (0.3.38) 
Installing activerecord (3.2.16) 
Installing activeresource (3.2.16) 
Using bundler (1.3.5) 
Installing json (1.8.1) 
Installing rack-ssl (1.3.3) 
Installing rdoc (3.12.2) 
Installing thor (0.18.1) 
Installing railties (3.2.16) 
Installing rails (3.2.16) 
Your bundle is complete!
It was installed into ./vendor/bundle
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:

<= 1.8.6 : unsupported
 = 1.8.7 : gem install rdoc-data; rdoc-data --install
 = 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!

ローカルにインストールしたRailsでプロジェクトを生成する。
(グローバル環境から切り離してインストールする場合は --skip-bundle を忘れずに。)
PostgreSQLを使用するため-dオプションで指定。

rails3$ bundle exec rails new samplev3216 -d postgresql --skip-bundle
      create  
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/images/rails.png
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      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  app/mailers/.gitkeep
      create  app/models/.gitkeep
      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/initializers/wrap_parameters.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  lib/assets
      create  lib/assets/.gitkeep
      create  log
      create  log/.gitkeep
      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  script
      create  script/rails
      create  test/fixtures
      create  test/fixtures/.gitkeep
      create  test/functional
      create  test/functional/.gitkeep
      create  test/integration
      create  test/integration/.gitkeep
      create  test/unit
      create  test/unit/.gitkeep
      create  test/performance/browsing_test.rb
      create  test/test_helper.rb
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/javascripts
      create  vendor/assets/javascripts/.gitkeep
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.gitkeep
      create  vendor/plugins
      create  vendor/plugins/.gitkeep

不要なローカルファイルを削除。

3rails$ rm -rf Gemfile* .bundle vendor

Gemfile内のtherubyracerのコメントアウトを解除。
Rails serverをWEBrickからThinに指定。
参考:http://d.hatena.ne.jp/namtcerid/20120513/1336916040

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  gem 'therubyracer', :platforms => :ruby

  # Thin web server
  group :development do
    gem 'thin'
  end

依存関係を解決するため生成されたプロジェクト内でbundle installを実行。

3rails/samplev3216$ bundle install --path vendor/bundle
Fetching gem metadata from https://rubygems.org/...........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Installing rake (10.1.0) 
Installing i18n (0.6.9) 
Installing multi_json (1.8.2) 
Installing activesupport (3.2.16) 
Installing builder (3.0.4) 
Installing activemodel (3.2.16) 
Installing erubis (2.7.0) 
Installing journey (1.0.4) 
Installing rack (1.4.5) 
Installing rack-cache (1.2) 
Installing rack-test (0.6.2) 
Installing hike (1.2.3) 
Installing tilt (1.4.1) 
Installing sprockets (2.2.2) 
Installing actionpack (3.2.16) 
Installing mime-types (1.25.1) 
Installing polyglot (0.3.3) 
Installing treetop (1.4.15) 
Installing mail (2.5.4) 
Installing actionmailer (3.2.16) 
Installing arel (3.0.3) 
Installing tzinfo (0.3.38) 
Installing activerecord (3.2.16) 
Installing activeresource (3.2.16) 
Using bundler (1.3.5) 
Installing coffee-script-source (1.6.3) 
Installing execjs (2.0.2) 
Installing coffee-script (2.2.0) 
Installing rack-ssl (1.3.3) 
Installing json (1.8.1) 
Installing rdoc (3.12.2) 
Installing thor (0.18.1) 
Installing railties (3.2.16) 
Installing coffee-rails (3.2.2) 
Installing jquery-rails (3.0.4) 
Installing pg (0.17.0) 
Installing rails (3.2.16) 
Installing sass (3.2.12) 
Installing sass-rails (3.2.6)
Installing therubyracer (0.12.0) 
Installing uglifier (2.3.2) 
Your bundle is complete!
It was installed into ./vendor/bundle
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:

<= 1.8.6 : unsupported
 = 1.8.7 : gem install rdoc-data; rdoc-data --install
 = 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!

Development用データベースを作成。

$ createdb samplev3216_development

Thin web server の起動が確認できればOK。

3rails/samplev3216$ bundle exec rails server
=> Booting Thin
=> Rails 3.2.16 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Thin web server (v1.6.1 codename Death Proof)
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop

assetのログが気になる場合は、下記ページを参照。
参考:サーバーログのStarted GET "/assets/***.css?body=1"とServed asset /***.css - 304 Not Modifiedを何とかしたい。 - コレグレーデギネード

Started GET "/assets/rails.png" for 127.0.0.1 at 2013-12-10 09:25:45 +0900
Served asset /rails.png - 304 Not Modified (0ms)