如何使用 Ruby 的正确版本?

How do I use the correct version of Ruby?

我已经 运行 bundle.

C:\Users\Chloe\workspace>rails -v
Your Ruby version is 2.3.1, but your Gemfile specified ~> 2.4.0

C:\Users\Chloe\workspace>ruby -v
ruby 2.4.2p198 (2017-09-14 revision 59899) [x64-mingw32]

C:\Users\Chloe\workspace>path
PATH=C:\ruby24\bin;...

我的道路上还有 C:\ruby23。我将其删除,关闭并重新打开 shell,但现在找不到 railsrails 一定一直在使用 2.3。我再次 运行 bundle 并注意到

MSYS2 could not be found. Please run 'ridk install'
or download and install MSYS2 manually from https://msys2.github.io/

几次。我 运行 安装程序 ridk install (即使我已经安装了 DevKit)。我又运行bundle了。但这导致了错误

Gem::InstallError: nokogiri requires Ruby version < 2.4, >= 2.1.0.
An error occurred while installing nokogiri (1.7.0.1), and Bundler cannot
continue.
Make sure that `gem install nokogiri -v '1.7.0.1'` succeeds before bundling.

我 运行 gem install rails -v 5.0.2(与我的 Gemfile 相同的版本)并且安装正常,使用 Nokogiri 1.8.1

Fetching: nokogiri-1.8.1-x64-mingw32.gem (100%)
Nokogiri is built with the packaged libraries: libxml2-2.9.5, libxslt-1.1.30, zlib-1.2.11, libiconv-1.15.
Successfully installed nokogiri-1.8.1-x64-mingw32

但是 bundle 仍然失败并且仍然尝试安装 Nokogiri 1.7.0.1,并且 rails -v 仍然失败并显示找不到 Nokogiri 1.7.0.1 的错误。

现在这是一个新问题,所以我将创建一个新的 post。

我会使用 asdf 或 rvm 控制您的 ruby 版本。这些是编程语言版本控制程序,允许您在系统上拥有多种语言版本,并控制哪个版本适用于哪个项目。以下是链接:

https://github.com/asdf-vm/asdf

https://rvm.io

它们实际上在短时间后使用起来非常简单。我更喜欢 asdf 但如果你只使用 ruby rvm 就足够了。

 cd project/dir
 touch .ruby-version
 echo "ruby-x.x.x" > .ruby-version

在您的项目目录中创建 .ruby-version 文件,并在该文件中复制实际的 ruby 版本。

一旦您"cd' into the "project/dir”,就会选择此版本。

仅在 mac 上使用 rvm 作为版本管理器进行了尝试和测试。

您还可以在 Gemfile 中指定 ruby 版本,如下所示(未测试)。

  ruby 2.x.x