Rails cannot load such file -- bundler/setup (LoadError),求助

kee · 2015年08月10日 · 最后由 torvaldsdb 回复于 2018年07月03日 · 13321 次阅读

猜测是 gemsets 路径不统一导致的 cannot load such file -- bundler/setup (LoadError)

**gem env**
- GEM PATHS:
     - /home/vagrant/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0
     - /home/vagrant/.gem/ruby/2.2.0

运行 gem list 只有最基本几个 gem(/home/vagrant/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0),但是 bundle list 确有 rails 需要的所有 gem(/home/vagrant/.gem/ruby/2.2.0),执行 bundle install 也没有什么可安装了

浏览器访问,提示错误找不到 bundler/setup 需要怎么搞呢??在线等

执行命令的前面加 bundle exec,例如 bundle exec rails c,如果没有错误,那么 bundle 管理的 gem 安装到了独立目录,bundle config path 可以看到配置到了哪。

@rei [vagrant@localhost ruby]$ bundle config path Settings for path in order of priority. The top value will be used You have not configured a value for path 是 passenger 认为的 gems path 根 bundle install gempath 不同到导致出错的吗??

#2 楼 @kee 错误页面的内容贴出来看看。

@rei

cannot load such file -- bundler/setup (LoadError)
  /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
  /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
  /home/vagrant/.gem/ruby/gems/passenger-5.0.15/lib/phusion_passenger/loader_shared_helpers.rb:406:in `activate_gem'
  /home/vagrant/.gem/ruby/gems/passenger-5.0.15/lib/phusion_passenger/loader_shared_helpers.rb:288:in `block in run_load_path_setup_code'
  /home/vagrant/.gem/ruby/gems/passenger-5.0.15/lib/phusion_passenger/loader_shared_helpers.rb:411:in `running_bundler'
  /home/vagrant/.gem/ruby/gems/passenger-5.0.15/lib/phusion_passenger/loader_shared_helpers.rb:287:in `run_load_path_setup_code'
  /home/vagrant/.gem/ruby/gems/passenger-5.0.15/helper-scripts/rack-preloader.rb:99:in `preload_app'
  /home/vagrant/.gem/ruby/gems/passenger-5.0.15/helper-scripts/rack-preloader.rb:153:in `<module:App>'
  /home/vagrant/.gem/ruby/gems/passenger-5.0.15/helper-scripts/rack-preloader.rb:29:in `<module:PhusionPassenger>'
  /home/vagrant/.gem/ruby/gems/passenger-5.0.15/helper-scripts/rack-preloader.rb:28:in `<main>'

#4 楼 @kee 最后两行日志显示调用了系统 rubygems 的 require,似乎没有正确配置 passenger 告诉它 ruby 在哪里。

如果 passenger 是编译到 nginx,应该在 /etc/nginx/nginx.conf 有这一行配置:

passenger_ruby /home/vagrant/.rbenv/shims/ruby;

可以参考一篇写得很完善的 rbenv + passenger 部署文章 https://gorails.com/deploy/ubuntu/14.04

感谢 @rei 之前配置的是系统自带的 ruby(/usr/bin/ruby),改为 rvenv 安装的 ruby 后提示 bundle install 了😄

It looks like Bundler could not find a gem. Maybe you didn't install all the gems that this application needs. To install your gems, please run:

bundle install

有点疑问,passenger 是从哪里加载 gems 的呢?另外我查看 bundle(bin)也有两个

#6 楼 @kee passenger 读取 config.ru,config.ru 里面 require 了 config/application.rb,application.rb require 了 config/boot.rb, boot.rb require 了 bundler/setup,bundler/setup 会按照项目下的配置(或者默认配置)设置 gem_path。

系统自带的 2.0.0,我用 rvm 安装 2.2 失败(提取文件有错误),就自己下载 ruby2.2.2 编译安装了,然后就出现上边的问题了 现在改用 rbenv 还是上边的问题,大概是什么原因呢?

bundle 也有两个,不知道执行 bundle install 有没有影响

bunle:[vagrant@localhost Internal]$ whereis bundle
bundle: /home/vagrant/.rbenv/shims/bundle /home/vagrant/bin/bundle

#9 楼 @kee rvm 和 rbenv 不要混用。看起来 bundle 用的是 rbenv 的。搞不清楚就先删掉其它 ruby,或者重建 vagrant。

@rei 再次感谢,bundle install 后跑起来了😄 最后一个小问题 gem list 怎么看到的只有(bundle 安装的没有列出来)

*** LOCAL GEMS ***

bigdecimal (1.2.6)
bundle (0.0.1)
bundler (1.10.6)
io-console (0.4.3)
json (1.8.1)
lumberjack (1.0.9)
mini_portile (0.6.2)
minitest (5.4.3)
nokogiri (1.6.6.2)
power_assert (0.2.2)
psych (2.0.8)
rake (10.4.2)
rdoc (4.2.0)
test-unit (3.0.8)

#11 楼 @kee 不太清楚,看看 which gemwhich bundle,在项目目录下 bundle config path

#11 楼 @kee 有种情况是 mac 下 gem install bundler 和 sudo gem install bundler 是安装到了不同的路径,个人经验经供参考

问题如下:

» c                                                                                                                                                                                                                                                                                                                                                [ruby-2.4.0][10:00:54]
/Users/dubing/.rvm/rubies/ruby-2.4.0/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- bundler/setup (LoadError)
        from /Users/dubing/.rvm/rubies/ruby-2.4.0/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /Users/dubing/workspace/vcooline/code_event_back_service/config/boot.rb:3:in `<top (required)>'
        from bin/rails:3:in `require_relative'
        from bin/rails:3:in `<main>'

  1. 重新安装bundler: gem install bundler

    Fetching: bundler-1.16.2.gem (100%)
    Successfully installed bundler-1.16.2
    1 gem installed
    
  2. 然后bundle install

然后世界顺畅了。

» c                                                                                                                                                                                                                                                                                                                                                [ruby-2.4.0][10:04:32]
Loading development environment (Rails 5.0.2)
gem install awesome_print  # <-- highly recommended
2.4.0 (main):0 >
  1. gen install bundler
  2. bundle install
需要 登录 后方可回复, 如果你还没有账号请 注册新账号