【问题标题】:How to change which version of Ruby I am using如何更改我使用的 Ruby 版本
【发布时间】:2019-03-04 05:21:51
【问题描述】:

我需要运行 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​​ 版本如此不一致?

【问题讨论】:

  • 显然你两者都有。 which rubyls -l $(which ruby)echo $PATH 应该提供一些见解。

标签: ruby homebrew


【解决方案1】:

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

rbenv

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

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

安装 rbenv

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

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

您的系统仍然不知道 rbenv 在哪里。通过运行将其添加到您的路径中:

$ 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 &lt;ruby version&gt; 来安装 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​​ 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 安装的系统或在 macOS 上默认安装的系统)。

测试安装

一旦安装成功,你可以运行下面的命令来检查安装了哪些版本(输出包含在下面的sn-p中):

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

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

前面带有* 的那个是当前处于活动状态的那个。如果你运行which 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 版。

【讨论】:

  • 感谢您的详细解答,能否加一段关于rbenv和rvm的区别?
  • @OrtomalaLokni 有很多文章解释了细节,还有一些我不能在一段中解释的较低级别的细节。这没有任何意义。也许我会写一篇文章并将其链接在这里。但是我还没有详细写出不同之处。会及时做到的。 :-)
  • 说不定你能用一句话来区分rbenv和rvm?
  • 有 3 个主要区别:1) RVM 可以为您提供预构建的二进制文件,而 chruby 和 rbenv 在您的系统上编译 Ruby。 2) RVM 覆盖 shell 并基本上替换了本身可能导致问题的“cd”命令。 Rbenv 使用路径垫片(链接已经在答案中)。 3) RVM 版本文件 (.rvmrc) 是完整的脚本并被执行,而 rbenv 版本文件 (.ruby-version) 是包含要在目录中使用的 ruby​​ 版本的简单文本文件。
  • 上一个答案的第 (3) 点的补充:Ruby 版本可以为当前 shell、当前目录和全局系统切换。这是一篇很棒的文章:makandracards.com/makandra/…
【解决方案2】:

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

【讨论】:

  • 您可以在答案下方添加评论作为评论。无论如何,如果答案对您有帮助/对您有用,您可以给它投票或将其设为正确答案,而不是说谢谢
  • 我确实投了赞成票,不幸的是我没有足够的“业力”来公开注册。
  • 谢谢@Andrew :-) 以防万一您有一天正在寻找 Rails 部署,我已经写了详细的Rails Deployment Guide。我发布的答案实际上是该指南的一部分。需要的时候可以参考一下。我希望它会有所帮助。
猜你喜欢
  • 2012-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多