简体   繁体   English

mac 上有两个不同的 ruby​​ 版本?

[英]Two different ruby versions on mac?

I am setting up a simple ruby on rails app locally.我正在本地设置一个简单的 ruby​​ on rails 应用程序。 It seems I have two different versions of ruby on mac and I would like to only use one.看来我在 mac 上有两个不同版本的 ruby​​,我只想使用一个。 When I ran bundle install , it says Your Ruby version is 2.7.1, but your Gemfile specified 2.6.3 .当我运行bundle install ,它说Your Ruby version is 2.7.1, but your Gemfile specified 2.6.3 Then, I changed the line ruby '2.6.3' in my gemfile to ruby '2.7.1' .然后,我将 gemfile 中的行ruby '2.6.3'更改为ruby '2.7.1' With this it ran bundle install properly.有了这个,它可以正确运行bundle install However, when I run rails server it says Your Ruby version is 2.6.3, but your Gemfile specified 2.7.1 .但是,当我运行rails server它说Your Ruby version is 2.6.3, but your Gemfile specified 2.7.1

Why is it saying two different values for my Ruby version?为什么我的 Ruby 版本有两个不同的值? How do I get it to only use one version of Ruby?我如何让它只使用一个版本的 Ruby?

If its relevant, I am on a mac and installed ruby using homebrew.如果相关,我在 mac 上并使用自制软件安装了 ruby​​。 If I run ruby -v in the terminal it says ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19] .如果我在终端中运行ruby -v ,它会显示ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19] I'm not sure why it says I have ruby 2.6.3.我不确定为什么它说我有 ruby​​ 2.6.3。

There are to different versions because Mac OSX already includes one by default for system scripts (2.6).有不同的版本,因为 Mac OSX 已经默认包含一个用于系统脚本 (2.6) 的版本。 Homebrew install another one that never overrides o reemplace the System Wide version. Homebrew 安装另一个永远不会覆盖或重新放置系统范围版本的软件。

You are looking for a " Ruby Version Manager ", are tools that allow you to install and use different versions of Ruby, even per project.您正在寻找“ Ruby 版本管理器”,该工具允许您安装和使用不同版本的 Ruby,甚至每个项目。

The popular ones are RVM and rbenv.流行的是 RVM 和 rbenv。 Personally, i choose rbenv and I think that is the most widely used of both.就我个人而言,我选择rbenv ,我认为这是两者中使用最广泛的。 Example of use:使用示例:

# Install ruby 2.7
rbenv install 2.7.1

# Make ruby 2.7 the default version
$ rbenv global 2.7.1

# Or make 2.7 the default versión only on a specific project
$ cd myproject
$ rbenv local 2.7.1
# this create a ".ruby-version" file

This webpage always have the most recent and easy to use tutorial for setup a Ruby environment, depending on the OS and version.该网页始终提供最新且易于使用的 Ruby 环境设置教程,具体取决于操作系统和版本。

https://gorails.com/setup/osx/10.15-catalina#overview https://gorails.com/setup/osx/10.15-catalina#overview

You have two different versions of Ruby installed, because MacOS natively comes with a standard installation of Ruby.您安装了两个不同版本的 Ruby,因为 MacOS 自带标准安装的 Ruby。

You also have rails pointing to the system version of Ruby.您还有指向 Ruby 系统版本的rails That version is usually under /usr/bin/ruby .该版本通常在/usr/bin/ruby The Homebrew installed version of Ruby (which is what you want) is located under /usr/local/bin/ruby unless you specified a completely different root path to install your brew packages. Ruby 的 Homebrew 安装版本(这是你想要的)位于/usr/local/bin/ruby除非你指定了一个完全不同的根路径来安装你的 brew 包。

Running brew config will give you a short list of data about your Homebrew configuration.运行brew config将为您提供有关 Homebrew 配置的简短数据列表。 Among them is an environment variable called HOMEBREW_PREFIX , which should look something like this:其中有一个名为HOMEBREW_PREFIX的环境变量,它应该如下所示:

$ brew config
....
HOMEBREW_PREFIX: /usr/local
....

I recommend placing /usr/local/bin first on your PATH environment variable so that you can easily use your brew packages via the CLI:我建议首先将/usr/local/bin放在您的PATH环境变量中,以便您可以通过 CLI 轻松使用您的 brew 包:

export PATH="/usr/local/bin:$PATH"

You may also want to look into setting the following environment variables for whichever shell you are using (examples given):您可能还想考虑为您使用的任何 shell 设置以下环境变量(给出的示例):

  • RUBY_ENGINE=ruby
  • RUBY_VERSION=2.7.1
  • GEM_ROOT=/usr/local/etc/ruby-2.7.1/lib/ruby/gems/2.7.1 (alias for GEM_HOME ) GEM_ROOT=/usr/local/etc/ruby-2.7.1/lib/ruby/gems/2.7.1GEM_HOME别名)

gem env gives a lot of great information on how Gems is configured. gem env提供了很多关于如何配置 Gems 的重要信息。

II had this exact problem and managed to fix it by running this command:我遇到了这个确切的问题,并通过运行以下命令设法解决了这个问题:

CFLAGS="-Wno-error=implicit-function-declaration" rbenv install 2.6.7

Note - I needed that version (2.6.7) please change it to the one you need注意 - 我需要那个版本 (2.6.7) 请将其更改为您需要的版本

I found this on this blog post here - https://dev.to/rbazinet/fix-installation-of-ruby-using-rbenv-on-macos-big-sur-3432我在这篇博客文章中找到了这个 - https://dev.to/rbazinet/fix-installation-of-ruby-using-rbenv-on-macos-big-sur-3432

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM