安装 Ruby gems 不适用于 Home Brew [英] Installing Ruby gems not working with Home Brew

查看:16
本文介绍了安装 Ruby gems 不适用于 Home Brew的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 sudo gem install ... 安装的 gem 无法执行(我得到一个 command not found).它们似乎安装到 Brew 的安装目录 /usr/local/Cellar/ 中(此外,/Library/Ruby/ 中的 gem 也不起作用).为了使 gems 可执行,我还需要做些什么吗?我在 Mac OS X 10.6 上使用 ZSH 和 Ruby v1.8 作为 Brew 中的那个.

它现在似乎正在工作.我刚出去玩了几个小时,又回来再试一次.

解决方案

Homebrew 很好.但是与 brewnpm 不同,gem 不会自动在 /usr/local/bin 中生成别名.p>

解决方案

我采用了一种非常简单的方法(截至 2020 年 3 月):

# 基于brew --prefix ruby​​`/bin"导出 PATH=/usr/local/opt/ruby/bin:$PATH# 基于"`gem environment gemdir`/bin"导出 PATH=/usr/local/lib/ruby/gems/3.0.0/bin:$PATH

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

就是这样!现在所有 Ruby bin 和已安装的 gem 都可以从您的 shell 中使用!

在旧版本的 Homebrew 中(2017 年之前),有一个单独的 Ruby 2 包,称为 ruby20,您可以使用以下代码段代替:

导出路径=/usr/local/opt/ruby20/bin:$PATH

这条线路是当时唯一需要的线路.但是,在 Ruby 2.1 中,gems 被移到了一个单独的目录中.不再在 /usr/local/opt/ruby/bin 下,而是在 /usr/local/lib/ruby/gems/2.0.0/bin 下(其中 "2.0.0"是 Gem 的最后一个主要 Ruby 版本).

工作原理

Homebrew 跟踪它安装软件包的位置,并为您维护指向那里的符号链接.

$ brew --prefix ruby/usr/local/opt/ruby$ l/usr/local/opt/ruby/usr/local/opt/ruby@ ->../地窖/红宝石/2.5.3_1

实际上,将/usr/local/opt/ruby 添加到PATH 与以下相同:

导出路径=/usr/local/Cellar/ruby/2.5.3_1/bin:$PATH

不过,这个长版本会硬编码当前安装的 Ruby 版本,并且会在您下次升级 Ruby 时停止工作.

至于 Gem,下面的命令会告诉你 Gem 添加新包的确切目录:

$ gem 环境 gemdir/usr/local/lib/ruby/gems/2.7.0

工具

这些工具旨在自动连接 Homebrew 和 Gem:

我没有用过这些,但它们可能对你有用.

The gems I install via sudo gem install ... can't be executed (I get a command not found). They seem to install into /usr/local/Cellar/ which is Brew's install directory (also, the gems in /Library/Ruby/ don't work either). Is there anything else I need to do to make the gems executable? I'm using ZSH on Mac OS X 10.6 with Ruby v1.8 for the one in Brew.

EDIT: It seems to be working now. I just went out for a few hours and came back to try it again.

解决方案

Homebrew is nice. However unlike brew and npm, gem does not make aliases in /usr/local/bin automatically.

Solution

I went for a very simple approach (as of March 2020):

# Based on "`brew --prefix ruby`/bin"
export PATH=/usr/local/opt/ruby/bin:$PATH
# Based on "`gem environment gemdir`/bin"
export PATH=/usr/local/lib/ruby/gems/3.0.0/bin:$PATH

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

That's it! Now all Ruby bins and installed gems will be available from your shell!

In older versions of Homebrew (before 2017), there was a separate package for Ruby 2 called ruby20, for which you'd use the following snippet instead:

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

This line was the only line needed at the time. But, in Ruby 2.1 the gems got moved to a separate directory. No longer under /usr/local/opt/ruby/bin, but instead at /usr/local/lib/ruby/gems/2.0.0/bin (where "2.0.0" is the last major Ruby version for Gem's purposes).

How it works

Homebrew keeps track of where it installed a package, and maintains a symbolic link for you that points there.

$ brew --prefix ruby
/usr/local/opt/ruby

$ l /usr/local/opt/ruby
/usr/local/opt/ruby@ -> ../Cellar/ruby/2.5.3_1

Effectively, adding /usr/local/opt/ruby to PATH is the same as the following:

export PATH=/usr/local/Cellar/ruby/2.5.3_1/bin:$PATH

Except, this long version hardcodes the currently installed version of Ruby and would stop working next time you upgrade Ruby.

As for Gem, the following command will tell you the exact directory Gem adds new packages to:

$ gem environment gemdir
/usr/local/lib/ruby/gems/2.7.0

Tools

These tools were meant to automatically bridge between Homebrew and Gem:

I haven't used these but they might work for you.

这篇关于安装 Ruby gems 不适用于 Home Brew的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆