cannot load such file -- bundler/setup (LoadError) エラー

Railsアプリケーションでdevise:installをしようとしたら、エラーになりました。

$ bundle exec rails g devise:install
/Users/cobachie/.rbenv/versions/2.2.4/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- bundler/setup (LoadError)
    from /Users/cobachie/.rbenv/versions/2.2.4/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        ...

環境

  • Mac OS X El Capitan
  • rbenvでruby 2.2.4をインストール

解決方法

$ bundle install --binstubs
$ bundle exec rails g devise:install
Looks like your app's ./bin/rails is a stub that was generated by Bundler.

In Rails 4, your app's bin/ directory contains executables that are versioned
like any other source code, rather than stubs that are generated on demand.

Here's how to upgrade:

  bundle config --delete bin    # Turn off Bundler's stub generator
  rake rails:update:bin         # Use the new Rails 4 executables
  git add bin                   # Add bin/ to source control

You may need to remove bin/ from your .gitignore as well.

When you install a gem whose executable you want to use in your app,
generate it and add it to source control:

  bundle binstubs some-gem-name
  git add bin/new-executable
  ...

これでエラーはなくなったけど、 そもそもbundle install --binstubsって使わないほうがいいんじゃないの?

それで、以下を実行。

$ bundle config --delete bin # .bundle/config からbinの記述が削除されます。
$ bundle exec rake rails:update:bin

いちおうメッセージは出なくなったけど、これでいいのかもうすこし勉強が必要...