LoginSignup
43
43

More than 5 years have passed since last update.

bundle exec rails console ができない, `require': cannot load such file -- bundler/setup (LoadError) の解決方法

Posted at

概要

$ bundle exec rails console
/Users/nii/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- bundler/setup (LoadError)
    from /Users/nii/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/nii/work/my-project/vendor/bundle/ruby/2.2.0/gems/spring-1.6.0/lib/spring/commands.rb:33:in `<module:Spring>'
    from /Users/nii/work/my-project/vendor/bundle/ruby/2.2.0/gems/spring-1.6.0/lib/spring/commands.rb:4:in `<top (required)>'
    from /Users/nii/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/nii/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/nii/work/my-project/vendor/bundle/ruby/2.2.0/gems/spring-1.6.0/lib/spring/application.rb:77:in `preload'
    from /Users/nii/work/my-project/vendor/bundle/ruby/2.2.0/gems/spring-1.6.0/lib/spring/application.rb:143:in `serve'
    from /Users/nii/work/my-project/vendor/bundle/ruby/2.2.0/gems/spring-1.6.0/lib/spring/application.rb:131:in `block in run'
    from /Users/nii/work/my-project/vendor/bundle/ruby/2.2.0/gems/spring-1.6.0/lib/spring/application.rb:125:in `loop'
    from /Users/nii/work/my-project/vendor/bundle/ruby/2.2.0/gems/spring-1.6.0/lib/spring/application.rb:125:in `run'
    from /Users/nii/work/my-project/vendor/bundle/ruby/2.2.0/gems/spring-1.6.0/lib/spring/application/boot.rb:18:in `<top (required)>'
    from /Users/nii/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/nii/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'

bundle exec rails console や bundle exec rails generate model hogehoge を実行すると上記のようなエラーになる場合の解決方法です.

解決方法

結構ハマってしまいましたが, 以下のリンクを参考にして解決することができました.

【Ruby on Rails】 bundler1.11.2に更新したら?rails g migration, rails cなどができなくなって困った

結論から言うと, どうやら bundler のバージョン違いによるエラーみたいです.
bundler 1.11.x だとダメで, 1.10.x だと大丈夫です.

# bundler のバージョンを確認する
$ gem list

*** LOCAL GEMS ***

... 略 ...
bundler (1.11.2)
... 略 ...

# bundlr の 1.10.6 をインストールする(1.10.x の最新が 1.10.6 なので)
$ rbenv exec gem install bundler -v 1.10.6
Fetching: bundler-1.10.6.gem (100%)
Successfully installed bundler-1.10.6
Parsing documentation for bundler-1.10.6
Installing ri documentation for bundler-1.10.6
Done installing documentation for bundler after 4 seconds
1 gem installed

# bundler のバージョンを再度確認する
$ gem list

*** LOCAL GEMS ***

... 略 ...
bundler (1.11.2, 1.10.6)
... 略 ...

# 以下のようにして bundler のバージョンを指定できる
$ bundle _1.10.6_ -v
Bundler version 1.10.6

# 指定しない場合は最新版が使われる
$ bundle -v
Bundler version 1.11.2

# ここで一旦 spring を停止させておく
# エラーメッセージに spring とあるように, spring と何か関係があるよう
$ bundle exec spring stop
Spring stopped.

# bundler のバージョンを指定して bundle exec rails c を実行すると, 実行できる
# ちなみに bundle exec rails c は bundle exec rails console と同じ
$ bundle _1.10.6_ exec rails c
Running via Spring preloader in process 21728
Loading development environment (Rails 4.2.0)
[1] pry(main)>

# bundler のバージョンは 1.11.2 のまま
$ bundle -v
Bundler version 1.11.2

# その後普通に bundler の指定をしなくても bundle exec rails c が実行できる
$ bundle exec rails c
Running via Spring preloader in process 21922
Loading development environment (Rails 4.2.0)
[1] pry(main)>

# spring のステータスを見ると起動している
# bundle exec rails c をすると spring が内部で起動するっぽい
$ bin/spring status
Spring is running:

21726 spring server | my-project | started 1 min ago
21727 spring app    | my-project | started 1 min ago | development mode

という感じで, 腑に落ちない感じではありますがエラーは出なくなります.
ちなみに bundler 1.10.6 を入れて 1.11.2 を消して bundle exec rails c を実行するだけでは解決しませんでした.
spring を一旦止めて 1.10.6 で実行するのがミソっぽいです.
何か調べて分かったら追記しようと思います.

関連

bundler 1.11.0 の変更で spring が起動しない件について

43
43
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
43
43