如何“捆绑安装”时,你的 Gemfile 需要一个旧版本的捆绑包?

我在一个较老的 Rails 项目,有一个 Gemfile。我试图在 Gemfile 和 bundle install中添加一个 gem,但是得到了一个错误:

Bundler could not find compatible versions for gem "bundler":
In Gemfile:
rails (= 3.0.0) ruby depends on
bundler (~> 1.0.0) ruby


Current Bundler version:
bundler (1.1.5)


This Gemfile requires a different version of Bundler.

它所使用的 Rails 版本需要 bundler ~ > 1.0.0,但是我已经安装了1.1.5版本,并且正在将其用于我的其他项目。通常我会使用 bundle exec ...,但是因为这是我们正在讨论的捆绑包,所以它比那个稍微复杂一点。如何添加一个 gem 到我的 Gemfile 并运行 bundle install,同时使用它所需要的捆绑包的版本?

68961 次浏览

First you need to install the appropriate version of bundler:

% gem install bundler -v '~> 1.0.0'
Successfully installed bundler-1.0.22

Then force rubygems to use the version you want (see this post):

% bundle _1.0.22_ install

I had the same issue on macOS Mojave. I installed the different version of the bundler gem and uninstall the current version.

gem install bundler -i '2.0.1'

gem uninstall bundler

Then gives me the option to choose the version to uninstall and I choose the one which is creating the problem.

The error message In Gemfile: bundler (~> 1.16) is a bit inaccurate, since the version number requirement can come from other places, such as the .gemspec file, which was the case for me:

spec.add_development_dependency "bundler", "~> 1.16"

Removing the version number from the .gemspec file solved the issue for me:

spec.add_development_dependency "bundler"

This is what I had to do to get it to work to install with a previous version (2.2.11) of bundler:

gem install bundler:2.2.11
bundle _2.2.11_ install