LoginSignup
21
7

More than 3 years have passed since last update.

Ruby 2.7 の bundle install で Could not find 'bundler' エラーが出たときの対応

Posted at

Ruby 2.7.0 の bundle installCould not find 'bundler' エラーが出たら BUNDLER_VERSION=2.1.2 bundle install を実行すれば直る。

このとき Gemfile.lock の BUNDLED WITH セクションが2.1.2に書き換わるので、必要に応じて git commit などをする。

解説

Bundler は Gemfile.lock の BUNDLED WITH セクションに Gemfile.lock を生成したバージョンを記録している。

Gemfile.lock
(略)
BUNDLED WITH
   1.17.3

bundle コマンドには Bundler Trampolining という機構があり、複数バージョンの bundler gem がインストールされていれば BUNDLED WITH セクションで指定されたバージョンの実行ファイルに処理を移譲する。指定されたバージョンがインストールされていないとき、メジャーバージョンが同じ(=マイナーバージョン以下だけ異なる)バージョンがあればそれに移譲する。それもなければ上記のエラーが出る。

bundler-trampolining
# bundler gem 1.17.3, 2.1.2 がインストールされているとき……

# Gemfile.lock がないディレクトリでは最新の2.1.2を使う
$ bundle --version
Bundler version 2.1.2

# Gemfile.lock があって BUNDLED WITH 2.1.2 ならそのまま 2.1.2 を使う
$ tail -n2 Gemfile.lock
BUNDLED WITH
   2.1.2
$ bundle --version
Bundler version 2.1.2

# Gemfile.lock があって BUNDLED WITH 1.17.3 なら 1.17.3 に移譲する
$ tail -n2 Gemfile.lock
BUNDLED WITH
   1.17.3
$ bundle --version
Bundler version 1.17.3

# Gemfile.lock があって BUNDLED WITH 1.17.0 なら同じ 1.x 系の 1.17.3 に移譲する
$ tail -n2 Gemfile.lock
BUNDLED WITH
   1.17.0
$ bundle --version
Bundler version 1.17.3

# bundler gem 2.1.2 だけがインストールされているとき……

# Gemfile.lock があって BUNDLED WITH 1.17.3 なら 2.x 系には移譲せずエラーを出す
$ tail -n2 Gemfile.lock
BUNDLED WITH
   1.17.3
$ bundle install
Traceback (most recent call last):
        2: from /Users/uasi/.rbenv/versions/2.7/bin/bundle:23:in `<main>'
        1: from /Users/uasi/.rbenv/versions/2.7.0/lib/ruby/2.7.0/rubygems.rb:294:in `activate_bin_path'
/Users/uasi/.rbenv/versions/2.7.0/lib/ruby/2.7.0/rubygems.rb:275:in `find_spec_for_exe': Could not find 'bundler' (1.17.3) required by your /path/to/Gemfile.lock. (Gem::GemNotFoundException)

BUNDLER_VERSION 環境変数をセットすると trampolining を無効化して特定のバージョンの bundler gem の使用を強制できる。 Ruby 2.7 に同梱された Bundler 2.1.2 で bundle install して Gemfile.lock を更新すればエラーは解決する。

21
7
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
21
7