はじめに
このやり方でやると、私の環境ではrails generate がうまく動きませんでした!
まずbundleをスキップしてnewして、それからbundle install するとうまくいきました。
インストールログなどが含まれる長い記事です。
Ruby on Railsの 初見殺しというか慣れてないと気持ち悪く感じるところは、「なんだかわからないけど色々なものが勝手にインストールされてる」と感じることだと思う。
ユーザ名は仮に username とします。
こちらの検証
システムのgemにrailsをインストールせずrails newする
http://qiita.com/youcune/items/222777415f00d19cccb4
言いたかったことはこのページに近いけど。
http://kozo002.blogspot.jp/2012/01/rubybundler.html
車輪の再発明のような感じだが、どういう仕組みになっているかは抑えたかったので。
こういうことだ。
- 先にbundleでGemfileを作成し、修正する。
- bundleで、位置を指定してこのプロジェクト限定で使うRuby on Rails と依存gemをインストールする。
- Gemfileが上書きされている。gemは所定の位置に用意されている。
先にbundleでGemfileを作成し、修正する。
$ bundle init
Writing new Gemfile to /Users/username/Documents/ruby/rails_dev/hello_rails/Gemfile
このプロジェクトではどのgemを使うかを記述する、Gemfileファイルを生成します。
# A sample Gemfile
source "https://rubygems.org"
# gem "rails"
生成されてすぐの状態では、gemとしてrailsを読み込み記述のみが記載されていますが、このコメントアウトを外し、「このプロジェクトではrailsだけ、gemとして読み込む」ようにします。
bundleで、位置を指定してこのプロジェクト限定で使うRuby on Rails と依存gemをインストールする。
bundleで所定の位置にGemlistに記載されているもの(ここではrailsだけ)をインストールするのですが、依存するGemがいっぱい。
$ bundle install --path vendor/bundle
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies...
Using rake 10.4.2
Installing i18n 0.7.0
Installing json 1.8.3 with native extensions
Installing minitest 5.8.3
Installing thread_safe 0.3.5
Installing builder 3.2.2
Installing erubis 2.7.0
Installing mini_portile2 2.0.0
Installing rack 1.6.4
Installing mime-types 2.99
Installing arel 6.0.3
Using bundler 1.11.2
Installing concurrent-ruby 1.0.0
Installing thor 0.19.1
Installing tzinfo 1.2.2
Installing nokogiri 1.6.7.1 with native extensions
Installing rack-test 0.6.3
Installing mail 2.6.3
Installing sprockets 3.5.2
Installing activesupport 4.2.5
Installing loofah 2.0.3
Installing rails-deprecated_sanitizer 1.0.3
Installing globalid 0.3.6
Installing activemodel 4.2.5
Installing rails-html-sanitizer 1.0.2
Installing rails-dom-testing 1.0.7
Installing activejob 4.2.5
Installing activerecord 4.2.5
Installing actionview 4.2.5
Installing actionpack 4.2.5
Installing actionmailer 4.2.5
Installing railties 4.2.5
Installing sprockets-rails 3.0.0
Installing rails 4.2.5
Bundle complete! 1 Gemfile dependency, 34 gems now installed.
Bundled gems are installed into ./vendor/bundle.
1件のGemファイルと34件の依存するgemをインストール。
確かに、慣れないとこの大量インストールは気持ち悪い。
前項でインストールしたrails(Gem)を使ってRailsプロジェクトを新規作成する
むちゃくちゃ長いですが、何をしたのかというと、
- bundleでインストールしたRails(のGem)を使ってこの位置でRailsプロジェクトを新規作成する命令を実行。
- ファイルを生成していたら、Gemfileがもうある。上書きしますか →はい
- その他のRailsプロジェクトのベースとなるファイルを一気に生成
- bundle使って、必要なgemをまとめてインストール。だがほとんどインストール済み
$ bundle exec rails new .
exist
create README.rdoc
create Rakefile
create config.ru
create .gitignore
conflict Gemfile
Overwrite /Users/username/Documents/ruby/rails_dev/hello_rails/Gemfile? (enter "h" for help) [Ynaqdh] Y
force Gemfile
create app
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/views/layouts/application.html.erb
create app/assets/images/.keep
create app/mailers/.keep
create app/models/.keep
create app/controllers/concerns/.keep
create app/models/concerns/.keep
create bin
create bin/bundle
create bin/rails
create bin/rake
create bin/setup
create config
create config/routes.rb
create config/application.rb
create config/environment.rb
create config/secrets.yml
create config/environments
create config/environments/development.rb
create config/environments/production.rb
create config/environments/test.rb
create config/initializers
create config/initializers/assets.rb
create config/initializers/backtrace_silencers.rb
create config/initializers/cookies_serializer.rb
create config/initializers/filter_parameter_logging.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.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 lib
create lib/tasks
create lib/tasks/.keep
create lib/assets
create lib/assets/.keep
create log
create log/.keep
create public
create public/404.html
create public/422.html
create public/500.html
create public/favicon.ico
create public/robots.txt
create test/fixtures
create test/fixtures/.keep
create test/controllers
create test/controllers/.keep
create test/mailers
create test/mailers/.keep
create test/models
create test/models/.keep
create test/helpers
create test/helpers/.keep
create test/integration
create test/integration/.keep
create test/test_helper.rb
create tmp/cache
create tmp/cache/assets
create vendor/assets/javascripts
create vendor/assets/javascripts/.keep
create vendor/assets/stylesheets
create vendor/assets/stylesheets/.keep
run bundle install
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies...
Using rake 10.4.2
Using i18n 0.7.0
Using json 1.8.3
Using minitest 5.8.3
Using thread_safe 0.3.5
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile2 2.0.0
Using rack 1.6.4
Using mime-types 2.99
Using arel 6.0.3
Installing debug_inspector 0.0.2 with native extensions
Using bundler 1.11.2
Installing byebug 8.2.1 with native extensions
Installing coffee-script-source 1.10.0
Installing execjs 2.6.0
Using thor 0.19.1
Using concurrent-ruby 1.0.0
Installing multi_json 1.11.2
Installing sass 3.4.20
Installing tilt 2.0.1
Installing spring 1.6.1
Installing sqlite3 1.3.11 with native extensions
Installing rdoc 4.2.1
Using tzinfo 1.2.2
Using nokogiri 1.6.7.1
Using rack-test 0.6.3
Using mail 2.6.3
Installing binding_of_caller 0.7.2 with native extensions
Installing coffee-script 2.4.1
Installing uglifier 2.7.2
Using sprockets 3.5.2
Installing sdoc 0.4.1
Using activesupport 4.2.5
Using loofah 2.0.3
Using rails-deprecated_sanitizer 1.0.3
Using globalid 0.3.6
Using activemodel 4.2.5
Installing jbuilder 2.4.0
Using rails-html-sanitizer 1.0.2
Using rails-dom-testing 1.0.7
Using activejob 4.2.5
Using activerecord 4.2.5
Using actionview 4.2.5
Using actionpack 4.2.5
Using actionmailer 4.2.5
Using railties 4.2.5
Using sprockets-rails 3.0.0
Installing coffee-rails 4.1.1
Installing jquery-rails 4.0.5
Using rails 4.2.5
Installing sass-rails 5.0.4
Installing web-console 2.2.1
Installing turbolinks 2.5.3
Bundle complete! 12 Gemfile dependencies, 54 gems now installed.
Bundled gems are 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!
run bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rails: spring inserted
今度は、12件のGEMを
Gemfileが上書きされている。gemは所定の位置に用意されている。
gem fileはバッチリ更新されて、Rails動かすのに必要なものが一通り記述されています。
$ cat Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.5'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
rails new する前にGemlistを記述し、所定のディレクトリ内にgemを入れてることになる。
$ ls ./vendor/bundle/ruby/2.2.0/gems
actionmailer-4.2.5 coffee-script-source-1.10.0 mini_portile2-2.0.0 sass-rails-5.0.4
actionpack-4.2.5 concurrent-ruby-1.0.0 minitest-5.8.3 sdoc-0.4.1
actionview-4.2.5 debug_inspector-0.0.2 multi_json-1.11.2 spring-1.6.1
activejob-4.2.5 erubis-2.7.0 nokogiri-1.6.7.1 sprockets-3.5.2
activemodel-4.2.5 execjs-2.6.0 rack-1.6.4 sprockets-rails-3.0.0
activerecord-4.2.5 globalid-0.3.6 rack-test-0.6.3 sqlite3-1.3.11
activesupport-4.2.5 i18n-0.7.0 rails-4.2.5 thor-0.19.1
arel-6.0.3 jbuilder-2.4.0 rails-deprecated_sanitizer-1.0.3 thread_safe-0.3.5
binding_of_caller-0.7.2 jquery-rails-4.0.5 rails-dom-testing-1.0.7 tilt-2.0.1
builder-3.2.2 json-1.8.3 rails-html-sanitizer-1.0.2 turbolinks-2.5.3
byebug-8.2.1 loofah-2.0.3 railties-4.2.5 tzinfo-1.2.2
coffee-rails-4.1.1 mail-2.6.3 rdoc-4.2.1 uglifier-2.7.2
coffee-script-2.4.1 mime-types-2.99 sass-3.4.20 web-console-2.2.1
何が言いたかったかというと