您应该使用 Ruby 版本管理器来管理 Ruby 的多个版本。我更喜欢使用 rbenv。以下是在 mac 上安装它的步骤(它们详细解释了正在做什么以及为什么;如果您想要一个快捷方式,请尝试按顺序运行所有命令,但我仍然坚持您通读这些步骤)。
rbenv
在进行实际安装之前,请记住以下几点:
-
rbenv 本身不包括安装 ruby 版本的能力。它只会更改每个目录的 ruby 版本。要安装 rubies,您需要安装 ruby-build 工具(它是 rbenv 项目的一部分)。 chruby 也是如此,它使用另一个工具来构建 ruby。 编辑(2021 年 6 月):看起来 rbenv 安装现在带有 ruby-build 并且能够直接在您的机器上编译 ruby。我将按原样保留答案,以便对使用较旧设置的人有意义(或者以防他们稍后恢复该决定)。
-
ruby-build 必须作为插件安装到 rbenv。
- 如果您需要详细了解这些工具,这里是rbenv 和ruby-build 的链接。
- 项目的 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 <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 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.3 和rbenv shell 2.4.5 是个问题。你应该做的是为目录设置一个 ruby 版本,忘记 rbenv。
您可以只创建一个名为.ruby-version 的文件,其中包含一行 - 您要用于此目录(和子目录)中所有 ruby 脚本的 ruby 版本号。只需 cd 到所需目录并运行:
echo "2.5.3" > .ruby-version
该目录和子目录中的所有 ruby 脚本都将使用 2.5.3 版。