如何更改我使用的 Ruby 版本

How to change which version of Ruby I am using

我需要 运行 2.5.3。我使用 brew 来管理我的 ruby 安装(因为我无法让 rvm 在我的机器上工作)。当我运行

$ruby -v

我明白了

ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]

然而,当我尝试使用

更新它时
brew upgrade ruby

我明白了

Error: ruby 2.6.1 already installed

为什么我真正安装的 ruby 版本如此不一致?

您应该使用 Ruby 版本管理器来管理 Ruby 的多个版本。我更喜欢使用 rbenv。以下是在 mac 上安装它的步骤(它们详细解释了正在做什么以及为什么这样做;如果您想要快捷方式,请尝试 运行 按顺序执行所有命令,但我会仍然坚持你通读这些步骤)。

rbenv

在进行实际安装之前,请记住以下几点:

  1. rbenv 本身不包括安装 ruby 版本的能力。它只改变每个目录的 ruby 版本。要安装红宝石,您需要安装 ruby-build 工具(它是 rbenv 项目的一部分)。 chruby 也是一样,它使用另一个工具来构建 ruby。 编辑(2021 年 6 月):看起来 rbenv 安装现在带有 ruby-build 并且能够在您的 machine 上编译 ruby .我将保留原样的答案,以便对使用较旧设置的人有意义(或者以防他们稍后恢复该决定)。
  2. ruby-build 必须作为 rbenv 的插件安装。
  3. 如果您需要详细了解这些工具,请点击此处 rbenv and ruby-build 的链接。
  4. 项目的自述文件中提供了这两个工具(以及许多其他帮助)的安装过程。如果事情不适合你,请参考那些。

正在安装 rbenv

运行 以下命令将 rbenv repo 克隆到主目录中的 .rbenv 目录中。

$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv

您的系统仍然不知道 rbenv 在哪里。通过 运行ning:

将其添加到您的路径
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile

要初始化 rbenv 以便它可以在您更改目录时帮助您更改 rubies,运行 这个:

~/.rbenv/bin/rbenv init

这应该告诉你这样的事情:

# Load rbenv automatically by appending
# the following to ~/.bash_profile:

eval "$(rbenv init -)"

所以运行这个:

echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

此时应该安装了 rbenv。当你在命令行上 运行 rbenv 时,你应该得到这样的东西:

$ rbenv
rbenv 1.1.1-39-g59785f6
Usage: rbenv <command> [<args>]

Some useful rbenv commands are:
   commands    List all available rbenv commands
   local       Set or show the local application-specific Ruby version
   global      Set or show the global Ruby version
   shell       Set or show the shell-specific Ruby version
   rehash      Rehash rbenv shims (run this after installing executables)
   version     Show the current Ruby version and its origin
   versions    List all Ruby versions available to rbenv
   which       Display the full path to an executable
   whence      List all Ruby versions that contain the given executable

See `rbenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/rbenv/rbenv#readme

注意:如果您收到警告说 rbenv 未安装,只需 运行 source ~/.bash_profile。这将重新运行 ~/.bash_profile 脚本并在您的路径中获取rbenv。在那之后你应该能够 运行 rbenv 没有问题。

请注意 rbenv 还没有提供安装或卸载 rubies 的选项。为此我们需要安装 ruby-build.

正在安装 ruby-build

我们需要添加 ruby-build 包作为 rbenv 插件,这样我们就可以输入 rbenv install <ruby version> 来安装 rubies。您需要做的就是创建插件目录并在插件目录中检出 ruby-build 的 git 存储库。 运行 以下:

$ mkdir -p "$(rbenv root)"/plugins
$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build

正在测试是否安装了 rbenv 和 ruby-build

运行 终端上没有任何参数的 rbenv 现在应该显示可用的安装和卸载命令。像这样:

$ rbenv
rbenv 1.1.1-39-g59785f6
Usage: rbenv <command> [<args>]

Some useful rbenv commands are:
   commands    List all available rbenv commands
   local       Set or show the local application-specific Ruby version
   global      Set or show the global Ruby version
   shell       Set or show the shell-specific Ruby version
   install     Install a Ruby version using ruby-build
   uninstall   Uninstall a specific Ruby version
   rehash      Rehash rbenv shims (run this after installing executables)
   version     Show the current Ruby version and its origin
   versions    List all Ruby versions available to rbenv
   which       Display the full path to an executable
   whence      List all Ruby versions that contain the given executable

See `rbenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/rbenv/rbenv#readme

如果您看到该输出,则您的 rbenv 已正确安装。

正在安装ruby

要安装 ruby 2.5.3,您可以 运行(等等,还没有 运行):

rbenv install 2.5.3

应该会输出几行,花一些时间然后告诉你安装了2.5.3版本。但是,有一个问题——如果安装失败,特别是在编译时,有时,终端会卡住,终端没有任何输出。它似乎安装了很长时间(永远)。要获得有关正在发生的事情的更多信息,运行 以下内容:

rbenv install -f -v 2.5.3

-f 参数告诉 rbenv 强制安装给定的版本。所以如果它已经安装,rbenv 将重新安装(基本上覆盖)给定的版本。因此,如果安装失败,-f 将确保安装。

-v 参数告诉 rbenv 输出详细信息。所以 ruby-build 所做的一切(包括编译过程)都会显示给你。不要被这里的编译这个词吓到。它通常可以毫无问题地正常编译,并且不会改变您的系统 ruby(在 Linux 上安装了 sudo apt install ruby 的系统,或者您在 mac[=131= 上默认获得的系统]) 无论是成功还是失败。

测试安装

安装成功后,您可以运行下面的命令来检查安装了哪些版本(输出包含在下面的代码片段中):

$ rbenv versions
  system
* 2.5.3 (set by /home/ubuntu/.rbenv/version)

注意:在mac上,新安装的ruby会有不同的路径。

前面有*的那个就是现在活跃的那个。如果你 运行 which ruby,你应该得到一个带有 ruby 垫片的路径。如果您好奇,请阅读 rbenv documentation to know what shims are,尽管您不必担心它们。

$ which ruby 
/home/ubuntu/.rbenv/shims/ruby

配置并忘记

rbenv 是一个很酷的东西,但是每次都写 rbenv shell 2.5.3rbenv shell 2.4.5 是个问题。您应该做的是为目录设置一个 ruby 版本,而忘记 rbenv.

您可以只创建一个名为 .ruby-version 的文件,其中包含一行 - 您要用于此目录(和子目录)中所有 ruby 脚本的 ruby 的版本号。只需 cd 到所需的目录和 运行:

echo "2.5.3" > .ruby-version

该目录和子目录中的所有 ruby 脚本将使用版本 2.5.3。

感谢 Vaibhav 提供的信息丰富的回答。我还没有尝试安装 rbenv,但我绝对会尝试的。现在我可以通过不在 gem 文件中指定 ruby 版本来解决这个问题。这是一个短期的解决方法,但它奏效了!