安装 Ruby gems 不适用于 Homebrew

发布于 2024-11-17 08:19:05 字数 304 浏览 3 评论 0原文

我通过 sudo gem install ... 安装的 gem 无法执行(我收到 command not find)。

它们似乎安装到 Brew 的安装目录 /usr/local/Cellar/ 中(此外,/Library/Ruby/ 中的 gem 也不起作用)。我还需要做些什么才能使 gems 可执行吗?我在 Mac OS X 10.6 上使用 Zsh,在 Brew 中使用 Ruby v1.8。

编辑:它现在似乎正在工作。我刚出去几个小时,回来再试一次。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

记忆で 2024-11-24 08:19:05

自制的很不错。然而,与 brewnpm 不同,gem 不会自动在 /usr/local/bin 中创建别名。

解决方案

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

# 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

将其添加到您的 .bashrc (或 .bash_profile.zshrc、 ETC。)。

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

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

export PATH=/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@ -> ../Cellar/ruby/2.5.3_1

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

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

除了,这个长版本硬编码了当前安装的 Ruby 版本,并且将停止工作下次升级 Ruby 时。

至于 Gem,以下命令将告诉您 Gem 将新包添加到的确切目录:

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

工具

这些工具旨在自动在 Homebrew 和 Gem 之间建立桥梁:

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

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.

不奢求什么 2024-11-24 08:19:05

brew 取消链接 ruby​​; brew link ruby​​ 可能会将符号链接添加到 /usr/local/bin/

$ which sass
$ brew unlink ruby; brew link ruby
Unlinking /usr/local/Cellar/ruby/2.0.0-p0... 20 links removed
Linking /usr/local/Cellar/ruby/2.0.0-p0... 31 symlinks created
$ which sass
/usr/local/bin/sass

brew --prefix ruby​​ 仍然很慢,但您也可以只添加 < code>/usr/local/opt/ruby/bin 到路径。

$ time brew --prefix ruby
/usr/local/opt/ruby
0.216
$ time brew --prefix ruby
/usr/local/opt/ruby
0.076
$ stat -f%Y /usr/local/opt/ruby
../Cellar/ruby/2.0.0-p0

brew unlink ruby; brew link ruby might add symlinks to /usr/local/bin/:

$ which sass
$ brew unlink ruby; brew link ruby
Unlinking /usr/local/Cellar/ruby/2.0.0-p0... 20 links removed
Linking /usr/local/Cellar/ruby/2.0.0-p0... 31 symlinks created
$ which sass
/usr/local/bin/sass

brew --prefix ruby is still pretty slow, but you could also just add /usr/local/opt/ruby/bin to the path.

$ time brew --prefix ruby
/usr/local/opt/ruby
0.216
$ time brew --prefix ruby
/usr/local/opt/ruby
0.076
$ stat -f%Y /usr/local/opt/ruby
../Cellar/ruby/2.0.0-p0
脸赞 2024-11-24 08:19:05

使用蒂莫的答案中的信息,我得到了这个:

PATH=/usr/local/opt/ruby/bin:$PATH
GEMSDIR=$(gem environment gemdir)/bin
PATH=$GEMSDIR:$PATH
export PATH

适用于 Homebrew,适用于单独的 gems 目录,并且不不要硬编码 Ruby 版本。

Using the info in Timo's answer, I've got this:

PATH=/usr/local/opt/ruby/bin:$PATH
GEMSDIR=$(gem environment gemdir)/bin
PATH=$GEMSDIR:$PATH
export PATH

Works for Homebrew, works for the separate gems directory, and doesn't hardcode a Ruby version.

影子的影子 2024-11-24 08:19:05

我喜欢自酿啤酒。可能有更好的方法来做到这一点,但如果你运行:

gem environment

这将打印出所有相关路径的漂亮列表。查找标有 EXECUTABLE DIRECTORY 的目录。这就是您要添加到路径中的路径。就我而言,它是 /usr/local/Cellar/ruby/1.9.3-p362/bin/ruby 但我想它会随着 Ruby 的新版本而改变。

我使用 /bin/bash 作为我的 shell,但将其添加到路径的过程应该与名称差不多。

我使用 TextWrangler(通过命令行工具)编辑我的 .profile 文件。要做到这一点,只需:

edit ~/.profile

完成后,关闭终端并打开一个新终端,或者运行:

source ~/.profile

I like home brew. There's probably a better way to do this, but if you run:

gem environment

That will print out a nice list of all the relevant paths. Look for the one labeled EXECUTABLE DIRECTORY. That's the one you want to add to your path. In my case that's /usr/local/Cellar/ruby/1.9.3-p362/bin/ruby but I would imagine it would change with newer version of Ruby.

I'm using /bin/bash as my shell, but the process of adding it to your path should be pretty much the name.

I use TextWrangler (via the command line tools) to edit my .profile file. To do that, it's just:

edit ~/.profile

When your done, either close your terminal and open a new one, or run:

source ~/.profile
被翻牌 2024-11-24 08:19:05

你也可以使用自制软件安装的红宝石。你只是缺少自制软件的自定义宝石集的功能。

首先:

sudo nano /etc/paths

这将打开 Nano 编辑器,

然后将以下内容添加到路径中:

/usr/local/Cellar/ruby/1.9.3-p194/bin

您的 ruby​​ 版本可能会有所不同。

就是这样。它现在应该检测到您的宝石。

哦,顺便说一句,你需要 Ctrl+X > y>按 ENTER 键将文件保存在 nano 中。

You can be fine with ruby installed by homebrew too.. You just lack the functionality of custom gemsets with homebrew.

first do:

sudo nano /etc/paths

this will bring up nano editor,

then add the following to the paths:

/usr/local/Cellar/ruby/1.9.3-p194/bin

your version of ruby will probably vary.

Thats it. It should now detect your gems.

Oh, btw, you need to Ctrl+X > y > ENTER to save a file in nano.

ˉ厌 2024-11-24 08:19:05

而不是使用 => $(cd $(which gem)/..; pwd)

你可以使用这个 => $(brew --prefix ruby​​)/bin

Instead of using => $(cd $(which gem)/..; pwd)

You could use this instead => $(brew --prefix ruby)/bin

枕头说它不想醒 2024-11-24 08:19:05

我认为这有点发展。

只需添加

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

到您的 .bashrc (或 .bash_profile、.zshrc/.bashrc、.. – 无论您使用什么)。

如果您对 ruby​​ 本身有问题

brew unlink ruby
brew link ruby

I think this evolve a bit.

Just add

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

To your .bashrc (or .bash_profile, .zshrc/.bashrc, .. – whatever you use).

If you have a problem with ruby itself

brew unlink ruby
brew link ruby
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文