如何使用 rbenv 在系统范围内安装 ruby

How to install ruby system-wide using rbenv

你可能认为这是一个经典的 "global" 命令问题,但事实并非如此。

我可以通过这个全局设置ruby:

rbenv global 2.5.1

然而,这使得 ruby2.5.1 对所有用户来说是全局的,而不是对整个系统。当同一服务器中的应用程序想要调用 ruby 或 access/pipe ruby 时,它们会收到 command not found 错误。

我相信我们应该安装或符号链接到 /usr/bin/usr/local/bin 或其他东西...

我找不到与此相关的任何信息。如何让其他应用程序看到选定的 ruby 版本?

我唯一的解决办法是 ruby 我自己构建到系统目录,但这会与 rbenv 冲突。

示例:

解决方法

在执行的脚本中将 /root/.rbenv/shims 文件夹添加到 $PATH 可以正常工作,但在某些情况下无法直接修改 $PATH我的选择是什么?

rbenv 根本就不是为了支持这个而设计的,你可以在 Github Issue. There are many technical considerations to take into account like permissions if you do this. I found another blog post outlining the process - System Wide Install With rbenv 中看到围绕这个的讨论。将其复制到此答案中,以防博客 post 消失。

但是,在较长的 运行 中,您可能会发现创建或使用 Ruby 包(例如 BrightBox PPA 包)更加容易。

Installing rbenv

Instead of the usual location of ~/.rbenv for single installs we'll be installing to /usr/local. You can use a different path if you want, but this is what I prefer.

cd /usr/local
git clone git://github.com/sstephenson/rbenv.git rbenv
chgrp -R staff rbenv
chmod -R g+rwxXs rbenv

Make sure the users that will use rbenv are part of the group you associated with the rbenv folder.

Now we want to add the following code into each users ~/.profile, ~/.bash_profile, or ~/.zshenv depending on the environment. You can also add it in /etc/skel/.profile or /etc/skel/.bash_profile template files that are copied when new users are created.

export RBENV_ROOT=/usr/local/rbenv
export PATH="$RBENV_ROOT/bin:$PATH"
eval "$(rbenv init -)"

Installing ruby-build (optional)

Optionally, you can install the ruby-build plugin to save yourself from building it yourself.

cd /usr/local/rbenv
mkdir plugins
cd plugins
git clone git://github.com/sstephenson/ruby-build.git
chgrp -R staff ruby-build
chmod -R g+rwxs ruby-build

Notes

Now you should have rbenv and optionally ruby-build setup so you can get started installing and using Ruby. This install is the same as the single user install with two exceptions. The global setting applies to all users and single user rbenv installs can "override" the system wide install.

If you have permission issues make sure all the files in the rbenv folder belong to the proper group and that the users trying to use rbenv are also members of the group.