【问题标题】:Why does my environment require I run bundle exec?为什么我的环境需要我运行 bundle exec?
【发布时间】:2011-12-27 17:04:52
【问题描述】:

每当我运行 rake 命令(即rake routes)时,我都会收到此错误:

You have already activated rake 0.9.2.2, but your Gemfile requires rake 0.9.2. Using bundle exec may solve this.

如果我运行bundle exec rake routes,它会起作用。

我希望能够简单地 rake routes 而不必运行 bundle exec rake routes

我查看了其他具有类似错误的问题,并尝试了各种解决方案(例如运行bundle update),但均无济于事。

另外,在我的gemfile 中,我指定了gem 'rake', '0.9.2'

有什么建议吗?

【问题讨论】:

  • 快速更新。最近从 RailsTutorial 中发现了这个关于需要使用 bundle exec 的解释:“正如第 3.2.1 节中简要提到的,通常有必要在 bundle exec 中添加诸如 rake 或 rspec 之类的命令前缀,以便程序在确切的 gem 中运行由 Gemfile 指定的环境。(出于技术原因,唯一的例外是 rails 命令本身。)”此外,“只要 RVM 的版本号为 1.11.x 或更高,安装的 gem 将自动在适当的 Bundler 中执行环境”,因此不需要 bundle exec 前缀。

标签: ruby-on-rails ruby-on-rails-3 rake gemfile


【解决方案1】:

尝试执行:

gem list

您可能会看到为 rake 安装了几个版本。顺便说一句,bundle exec 是在 Rails 应用程序上下文中执行代码的正确方法。因此,您可以使用别名来键入 less。

【讨论】:

  • 您也可以使用export RUBYOPT=-rbundler/setup 开始您的shell 会话,这就是bundle exec 所做的一切。
  • 这给了我:ruby:没有要加载的文件--bundler/setup (LoadError)
【解决方案2】:

如果您的 gemfile 中的某些 gem 需要您安装的 Rake 的旧版本,您可能会看到此消息。也许你已经更新了 rake。您通常可以通过更新 gem 来修复它。运行:

bundle update

这将使用您 gemfile 中最新的 gem 更新您的包。这可能会解决 rake 不兼容问题。

【讨论】:

    【解决方案3】:

    正如@lucapette 所说,您可能有多个版本的 rake。假设您确实想使用 0.9.2,您可以删除 0.9.2.2 版本以消除警告,然后运行 ​​bundle install 以确保您拥有所需版本的所有正确 gem 版本(在您的情况下为 0.9.2,在下面的示例中为 0.8.7)。

    步骤如下:

    $ gem list
    
    *** LOCAL GEMS ***
    
    ...
    rake (0.9.2.2, 0.8.7)
    ...
    
    $ gem uninstall rake
    
    Select gem to uninstall:
     1. rake-0.8.7
     2. rake-0.9.2.2
     3. All versions
    > 2
    
    You have requested to uninstall the gem:
            rake-0.9.2.2
    addressable-2.2.6 depends on [rake (>= 0.7.3)]
    ...
    If you remove this gems, one or more dependencies will not be met.
    Continue with Uninstall? [Yn]  Y
    Successfully uninstalled rake-0.9.2.2
    INFO:  gem "0.9.2.2" is not installed
    
    $ bundle install
    

    【讨论】:

      【解决方案4】:

      如果你使用 rvm。你可以试试关注

      rvm gem list
      

      【讨论】:

        猜你喜欢
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 2012-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多