Gem 列表在 "Successfully installed bundler" 之后不显示捆绑器

Gem list doesn't show bundler after "Successfully installed bundler"

我正在尝试安装第二个版本的捆绑器。安装输出成功消息,但新的捆绑器安装似乎根本不存在。我该如何更正此安装?

# Use sudo because of *system* rbenv installation
sudo gem install bundler:2.1.4
# => Successfully installed bundler-2.1.4

# Check for existence of new installation, but only the old version is available
gem list bundler
# => bundler (default: 1.17.2)
bundle _2.1.4_ -v
# => can't find gem bundler (= 2.1.4) with executable bundle (Gem::GemNotFoundException)
ls /usr/local/rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/
# => [shows bundler-1.17.2 but not any other bundler directories]

我正在使用 rbenv(不是 rvm)的系统安装。我不在 bundle 中工作(也不在 gemset 中工作,因为我没有使用 rvm)。

root 用户的 PATH 可能与您当前用户的 PATH 不同。因此,root 加载的 gem 命令将与您作为普通用户加载的 gem 命令不同。这使得 sudo gem install 将 gem 保存到不同的位置(在 root 用户的 PATH 中找到的 Ruby 安装位置)。

要解决此问题,最直接的解决方案是通过为它提供完整路径来强制 root 使用相同的 gem 命令:

sudo `which gem` install ...

注意反引号的使用。以这种方式使用反引号,该命令实际上将扩展为类似以下内容:

sudo /some/path/to/the/user/ruby/installation/bin/gem install ...

要弄清楚 root 的 gem 命令是否与您默认的 gem 命令不同,您可以这样做:

# As normal user check output of this command
which gem

# ..and compare it to the output of this command
sudo which gem

问题出在 sudo。它没有 运行 /etc/profile.d/rbenv.sh,那是具有以下内容的文件:

export RBENV_ROOT=/usr/local/rbenv
export PATH="$RBENV_ROOT/bin:$PATH"
eval "$(rbenv init -)"

要解决问题,运行 sudo -i,然后 运行 新 shell 中的 gem install ... 命令。