使用 Homebrew 安装 Ruby

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12287882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:24:03  来源:igfitidea点击:

Installing Ruby with Homebrew

rubybashterminalhomebrew

提问by cchiera

I installed Ruby with Homebrew:

我用 Homebrew 安装了 Ruby:

brew install ruby

Under "Caveats" it said:

在“注意事项”下,它说:

NOTE: By default, gem installed binaries will be placed into:
/usr/local/Cellar/ruby/1.9.3-p194/bin

You may want to add this to your PATH.

注意:默认情况下,gem 安装的二进制文件将放置在:
/usr/local/Cellar/ruby/1.9.3-p194/bin

您可能希望将此添加到您的 PATH。

What does that mean and how can I add it to my "path"? Assuming it has to do with a bash_profile but new to this.

这是什么意思,我如何将它添加到我的“路径”中?假设它与 bash_profile 有关系,但这是新的。

回答by Kyle

in ~/.bash_profileadd the following line

~/.bash_profile添加以下行

export PATH=/usr/local/Cellar/ruby/1.9.3-p194/bin:$PATH

When you're done, close your terminal and re-open it. You should be fine.

完成后,关闭终端并重新打开它。你应该没事。

Alternatively, you can execute the follwing in each open shell instead of closing/re-opening:

或者,您可以在每个打开的 shell 中执行以下操作,而不是关闭/重新打开:

source ~/.bash_profile

Note:I highly recommend installing ruby via rvmor rbenvso you can manage multiple ruby versions and use gemsets.

注意:我强烈建议通过rvmrbenv安装 ruby​​,这样你就可以管理多个 ruby​​ 版本并使用 gemset。

回答by pje

Add this line to your .profile (or .bash_profile, .bashrc, .zshrc, etc):

将此行添加到您的 .profile(或 .bash_profile、.bashrc、.zshrc 等)

export PATH=/usr/local/opt/ruby/bin:$PATH


This is an up-to-date version of Kyle's answer. As of May 2014, brew info rubyprints:

这是凯尔回答的最新版本。截至 2014 年 5 月,brew info ruby印刷品:

By default, gem installed executables will be placed into:

默认情况下,gem 安装的可执行文件将放置在:

  /usr/local/opt/ruby/bin

You may want to add this to your PATH. After upgrades, you can run

您可能希望将此添加到您的 PATH。升级后,您可以运行

  gem pristine --all --only-executables

...to restore binstubs for installed gems.

...恢复已安装 gem 的 binstubs。

回答by Pawe? Go?cicki

Install ruby:

安装红宝石:

brew install ruby

I'd recommend setting $PATH, $GEM_PATH and $GEM_HOME. For latest Ruby it's:

我建议设置 $PATH、$GEM_PATH 和 $GEM_HOME。对于最新的 Ruby,它是:

export PATH=/usr/local/opt/ruby/bin:$PATH
export GEM_HOME=/usr/local/opt/ruby/lib/ruby/gems/2.6.0
export GEM_PATH=/usr/local/opt/ruby/lib/ruby/gems/2.6.0

Put them in something like ~/.bash_profile.

把它们放在像~/.bash_profile.

And then to verify:

然后验证:

type -a ruby
> ruby is /usr/local/opt/ruby/bin/ruby
> ...

ruby -v
> ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-darwin18]

gem list
> *** LOCAL GEMS ***
> 
> did_you_mean (1.3.0)
> minitest (5.11.3)
> ...

回答by Don Johnson

Quick fix:

快速解决:

Open /etc/paths.

打开 /etc/paths。

Change the order of lines(highest priority on top).
/usr/local/bin
/usr/local/sbin
/usr/bin
/bin
/usr/sbin
/sbin

回答by mtgto

In ruby 2.6.x, brew info rubysays:

在 ruby​​ 2.6.x 中,brew info ruby说:

By default, binaries installed by gem will be placed into:
  /usr/local/lib/ruby/gems/2.6.0/bin

You may want to add this to your PATH.

ruby is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ruby first in your PATH run:
  echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc

For compilers to find ruby you may need to set:
  export LDFLAGS="-L/usr/local/opt/ruby/lib"
  export CPPFLAGS="-I/usr/local/opt/ruby/include"

I don't want to update XXshrc whenever ruby is updated. My zshrc is:

我不想在 ruby​​ 更新时更新 XXshrc。我的 zshrc 是:

if [ -d "/usr/local/opt/ruby/bin" ]; then
        export PATH=/usr/local/opt/ruby/bin:$PATH
        export PATH=`gem environment gemdir`/bin:$PATH
fi