wancoro blog

学んだことをアウトプットしていきたいと思います

bundle installでGemfileが作成されない

やりたいこと

GemfileにGemを追加し、Gemfile.lockを更新したい。

事象

Gemfileを更新した後 bundle install を実行すると以下のエラーが表示され、Gemfileが更新されない。

 You are trying to install in deployment mode after changing
 your Gemfile. Run `bundle install` elsewhere and add the
 updated Gemfile.lock to version control.
 
 If this is a development machine, remove the Gemfile freeze 
 by running `bundle config unset deployment`.

bundler -v を実行したところエラーが出た。

 Traceback (most recent call last):
         2: from /home/vagrant/.rbenv/versions/2.6.5/bin/bundler:23:in `<main>'
         1: from /home/vagrant/.rbenv/versions/2.6.5/lib/ruby/site_ruby/2.6.0/rubygems.rb:296:in `activate_bin_path'
 /home/vagrant/.rbenv/versions/2.6.5/lib/ruby/site_ruby/2.6.0/rubygems.rb:277:in `find_spec_for_exe': Could not find 'bundler' (2.0.1) required by your Gemfile.lock. (Gem::GemNotFoundException)
 To update to the latest version installed on your system, run `bundle update --bundler`.
 To install the missing version, run `gem install bundler:2.0.1`

解決策

エラーメッセージ2 に書かれている通り、 gem install bundler:2.0.1 を実行。

再度 bundle install をすると、同じようなエラーが出た。

 You are trying to install in deployment mode after changing
 your Gemfile. Run `bundle install` elsewhere and add the
 updated Gemfile.lock to version control.
 
 If this is a development machine, remove the Gemfile freeze 
 by running `bundle config --delete deployment`.

.bundle/config を見てみる。

 $ cat .bundle/config
 ---
 BUNDLE_DEPLOYMENT: "true"
 BUNDLE_PATH: "./vendor/bundle"
 BUNDLE_FROZEN: "true"
 BUNDLE_WITHOUT: "development:test"

エラーメッセージ書かれている通りbundle config --delete deployment を実行。
また、 BUNDLE_FROZENtrue の場合、Gemfileを変更できなくなるようなので
bundle config set frozen false も実行しておく。
http://ruby.studio-kingdom.com/bundler/bundle_config/

コマンド実行後の .bundle/config

 $ cat .bundle/config
 ---
 BUNDLE_PATH: "./vendor/bundle"
 BUNDLE_WITHOUT: "development:test"

これで bundle instal が成功するようになった!

しかし、そもそもなぜ bundler -vでエラーが出たのかは謎。