由Ruby安装的RVM无法工作?

46

我使用 RVM 网站提供的单行指令(使用 git)安装了 RVM。

然后我使用以下命令安装了 Ruby 1.9.2 和 1.8.7 版本:

rvm install 1.9.2
rvm install 1.8.7

然而,我找不到 Ruby 二进制文件。当我尝试执行命令时,会出现以下错误:

[root@server1 support]# rvm use 1.9.2
Using /usr/local/rvm/gems/ruby-1.9.2-p136

[root@server1 support]# ruby
-bash: ruby: command not found

这是rvm info的输出结果:

[root@server1 support]# rvm info

system:

  system:
    uname:       "Linux server1.myserver.com 2.6.18-194.26.1.el5.028stab070.14 #1 SMP Thu Nov 18 16:34:01 MSK 2010 x86_64 x86_64 x86_64 GNU/Linux"
    bash:        "/bin/bash => GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)"
    zsh:         " => not installed"

  rvm:
    version:      "rvm 1.2.6 by Wayne E. Seguin (wayneeseguin@gmail.com) [http://rvm.beginrescueend.com/]"

  homes:
    gem:          "not set"
    ruby:         "not set"

  binaries:
    ruby:         ""
    irb:          ""
    gem:          ""
    rake:         ""

  environment:
    PATH:         "/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/rvm/bin"
    GEM_HOME:     ""
    GEM_PATH:     ""
    MY_RUBY_HOME: ""
    IRBRC:        ""
    RUBYOPT:      ""
    gemset:       ""


[root@server1 support]#

请在命令行中输入 rvm info,并将其输出添加到您的问题中以进行编辑。 - the Tin Man
1
你的意图是以root身份进行RVM的系统范围安装,还是单个用户安装?如果是单个用户安装,你不应该以root身份运行。 - the Tin Man
10个回答

76

RVM需要在您登录时对~/.bashrc~/.bash_profile进行一些小的修改以初始化它。在安装文档的Post Install部分中有说明。您是否这样做了?


根据您的rvm info输出,看起来您尚未完成安装。输出中的所有条目都应该具有相应的值。因此,我怀疑您尚未添加:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"  # This loads RVM into a shell session.
将以下代码添加到您的〜/ .bashrc 〜/ .bash_profile 文件,然后开始新会话:

# Load RVM if it is installed,
#  first try to load  user install
#  then try to load root install, if user install is not there.
if [ -s "$HOME/.rvm/scripts/rvm" ] ; then
  . "$HOME/.rvm/scripts/rvm"
elif [ -s "/usr/local/rvm/scripts/rvm" ] ; then
  . "/usr/local/rvm/scripts/rvm"
fi

如果您正在进行“多用户”安装,则需要执行更多操作。您是否修改了/etc/profile,或者如果您正在使用Bash作为您的shell,则是否修改了/etc/bash.bashrc以包含:

就个人而言,我不喜欢多用户安装,也不建议使用它,但您可能有不同的看法。


附注:去年在IRC上与RVM维护者讨论时,他们告诉我他们不建议使用系统范围内的安装,而是推荐使用本地“单用户”安装,即使是服务器也是如此。


只需删除目录并从您的~/.bashrc~/.bash_profile中删除相关行,然后重新启动终端会话即可。 RVM文档中已经很好地涵盖了这一点。尝试使用rvm help命令并阅读有关implode的内容。 - the Tin Man
我的~/.bash_profile文件的内容正是你说应该添加的那一行。但每次我打开一个shell时,rvm info命令返回的输出与问题中的相同,ruby命令也无法工作。我进行了单用户安装。还有什么其他问题吗? - Slavo
2
你也可以使用 source $HOME/.rvm/scripts/rvm - mikatakana
1
你知道你不必要求其他人修复链接,通过提交编辑,你可以自己完成。这就是 [so] 的美妙之处。 - the Tin Man
我曾经为此苦苦挣扎。更新了 .bash_profile,但没有效果。更新了 .bashrc,现在一切都好了。我正在运行 xfce。 - Eric Fuller
显示剩余2条评论

16

我曾经遇到过类似的问题(在使用Ubuntu 13.10时),以下是解决方法:

$ source .rvm/scripts/rvm 

并且是为了长期考虑

$ echo "source ~/.rvm/scripts/rvm" >> ~/.bashrc

$ ruby -v


The program 'ruby' can be found in the following packages:
 * ruby1.8
 * ruby1.9.1
Try: sudo apt-get install <selected package>

$ source .rvm/scripts/rvm

$ ruby -v

ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]

15

我在一台Ubuntu 11.10虚拟机上使用"如何在Ubuntu 11.10上安装Ruby on Rails"的方法安装了Ruby on Rails。

安装完后,我遇到了相同的问题。在我看来,该教程唯一缺少的是以下命令:

rvm --default use 1.9.2
尽管Ruby已经正确安装,但RVM似乎在每个会话中重新定义要使用的Ruby。问题在于默认的Ruby指向“系统Ruby”,而在我的情况下,这个指向并没有作用,并导致调用rvm info返回类似于初始帖子的结果。
为了解决这个问题,必须使用以下其中一条命令之一:
rvm  --default use 1.9.x

或(仅适用于当前会话)

rvm use 1.9.x

在我启动服务器之前,我也遇到了"ExecJS和找不到JavaScript运行时"的问题。根据一些回答的建议,我通过在Gemfile中添加以下行来解决它。

gem 'execjs'

gem 'therubyracer'

然后运行bundle install


9

我遇到了同样的问题。您可能需要先登录:

/bin/bash --login

5
这里是如何修复RVM中未检测到的Ruby的方法。
首先,我进行了一次健全性检查,以确保每件事情看起来都很好,然后运行了以下命令:
rvm use ruby --default

这个命令完成了工作。命令如下:

-SVE1411EGXB:~/code$ cd app1/
  -SVE1411EGXB:~/code/app1$ rails s
The program 'rails' can be found in the following packages:
 * ruby-railties-3.2
 * ruby-railties-4.0
Try: sudo apt-get install <selected package>
  -SVE1411EGXB:~/code/app1$ rails s
The program 'rails' can be found in the following packages:
 * ruby-railties-3.2
 * ruby-railties-4.0
Try: sudo apt-get install <selected package>
  -SVE1411EGXB:~/code/app1$ which ruby
  -SVE1411EGXB:~/code/app1$ which gem
  -SVE1411EGXB:~/code/app1$ rvm list

rvm rubies

=* ruby-2.1.2 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

  -SVE1411EGXB:~/code/app1$ \curl -sSL https://get.rvm.io | bash -s stable
Downloading https://github.com/wayneeseguin/rvm/archive/stable.tar.gz

Upgrading the RVM installation in /home  /.rvm/
    RVM PATH line found in /home  /.profile /home  /.bashrc /home  /.zshrc.
    RVM sourcing line found in /home  /.bashrc /home  /.bash_profile /home  /.zlogin.
Upgrade of RVM in /home  /.rvm/ is complete.

# nullsoulexception,
#
#   Thank you for using RVM!
#   We sincerely hope that RVM helps to make your life easier and more enjoyable!!!
#
# ~Wayne, Michal & team.

In case of problems: http://rvm.io/help and https://twitter.com/rvm_io

Upgrade Notes:

  * No new notes to display.

  -SVE1411EGXB:~/code/app1$ rails s
The program 'rails' can be found in the following packages:
 * ruby-railties-3.2
 * ruby-railties-4.0
Try: sudo apt-get install <selected package>
  -SVE1411EGXB:~/code/app1$ source ~/.rvm/scripts/rvm
  -SVE1411EGXB:~/code/app1$ rvm requirements
Checking requirements for ubuntu.
Requirements installation successful.
  -SVE1411EGXB:~/code/app1$ rvm get stable
Downloading https://get.rvm.io
Downloading https://github.com/wayneeseguin/rvm/archive/stable.tar.gz

Upgrading the RVM installation in /home  /.rvm/
    RVM PATH line found in /home  /.profile /home  /.bashrc /home  /.zshrc.
    RVM sourcing line found in /home  /.bashrc /home  /.bash_profile /home  /.zlogin.
Upgrade of RVM in /home  /.rvm/ is complete.

# nullsoulexception,
#
#   Thank you for using RVM!
#   We sincerely hope that RVM helps to make your life easier and more enjoyable!!!
#
# ~Wayne, Michal & team.

In case of problems: http://rvm.io/help and https://twitter.com/rvm_io

Upgrade Notes:

  * No new notes to display.

RVM reloaded!
  -SVE1411EGXB:~/code/app1$ rvm reload
RVM reloaded!
  -SVE1411EGXB:~/code/app1$ rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-head] # security released on head
[ruby-]1.9.3[-p547]
[ruby-]2.0.0-p451
[ruby-]2.0.0[-p481]
[ruby-]2.1.1
[ruby-]2.1[.2]
[ruby-]2.1-head
ruby-head

# GoRuby
goruby

# Topaz
topaz

# TheCodeShop - MRI experimental patches
tcs

# jamesgolick - All around gangster
jamesgolick

# Minimalistic ruby implementation - ISO 30170:2012
mruby[-head]

# JRuby
jruby-1.6.8
jruby[-1.7.12]
jruby-head

# Rubinius
rbx-2.0.0
rbx-2.1.1
rbx[-2.2.7]
rbx-head

# Ruby Enterprise Edition
ree-1.8.6
ree[-1.8.7][-2012.02]

# Kiji
kiji

# MagLev
maglev[-head]
maglev-1.0.0

# Mac OS X Snow Leopard Or Newer
macruby-0.10
macruby-0.11
macruby[-0.12]
macruby-nightly
macruby-head

# Opal
opal

# IronRuby
ironruby[-1.1.3]
ironruby-head

  -SVE1411EGXB:~/code/app1$ ruby -v
The program 'ruby' can be found in the following packages:
 * ruby
 * ruby1.8
Try: sudo apt-get install <selected package>
  -SVE1411EGXB:~/code/app1$ rvm use ruby --default
Using /home  /.rvm/gems/ruby-2.1.2
  -SVE1411EGXB:~/code/app1$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
  -SVE1411EGXB:~/code/app1$ rails s
=> Booting WEBrick
=> Rails 4.1.1 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)

2
这可能是更准确的答案。rvm use 2.0.0 --default 解决了我的问题。感谢提示,点个赞。 - Tim

4
您是否确保已经获取了 /usr/local/lib/rvm 的源代码?请执行以下命令将其添加到 ~/.bashrc 文件中:echo 'source /usr/local/lib/rvm' >> ~/.bashrc,然后通过 SSH 重新登录或启动一个新的 Bash 实例。
为了澄清一些混淆的地方:有两种安装 RVM 的方式:用户级别安装和系统级别安装。
在日常使用中,您应该使用用户级别安装,它会将 RVM 安装在 ~/.rvm 目录下。系统级别安装适用于服务器,其中应该使用一组 Ruby。在这种情况下,RVM 的默认位置是 /usr/local/rvm。
根据您的问题,似乎您已将 RVM 安装为系统级别安装。
要使用 RVM,必须每次登录时运行脚本来初始化。为了轻松实现此操作,请将文件包含在您的 ~/.bashrc 文件中(如果您使用的是 OS X,则是 ~/.bash_profile),以便每次登录时自动运行。对于用户级别安装,请将以下文本添加到文件中:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

对于系统范围的安装,请使用以下文本:

[[ -s "/usr/local/lib/rvm" ]] && . "/usr/local/lib/rvm"

如果您确实拥有系统范围的安装,您还需要确保自己是rvm组的成员; 输入以下内容以完成操作:
sudo adduser `whoami` rvm

一旦您进行了必要的更改,请注销您的会话,然后再次登录。完成此操作后,rvm use 1.9.2应该设置一系列环境变量,您可以通过键入rvm info来查看这些变量。如果一切正常,ruby将能够正确执行。


注意:我花了几个会话尝试找到“/usr/local/bin/rvm”,确保它是“lib”而不是“bin” :) - Michelle Tilley
你能具体解释一下“source /usr/local/lib/rvm”这句话的含义吗?我对Ruby和RoR都很陌生,希望你能向我解释一下这些术语,谢谢。 - YD8877
Tin Man:這對於單用戶安裝是正確的。對於系統范圍的安裝,腳本位於/usr/local/lib/rvm,參見https://github.com/wayneeseguin/rvm/blob/master/contrib/install-system-wide#L208 - Michelle Tilley
@the Tin Man:/usr/local/lib/rvm 是系统范围内的 rvm 安装目录,肯定仍在使用。 - Samuel
1
虽然用户的问题是正确的,但输出结果为“Using /usr/local/rvm/gems/ruby-1.9.2-p136”,这意味着是系统范围内的安装,而不是安装到“~/.rvm”中。我做出了相应的回答。 - Michelle Tilley
显示剩余3条评论

3

您的路径中没有 Ruby。简单来说,RVM 处理在您的路径中切换 Ruby 的工作。请查看命令行工具的输出。

printenv

您应该看到类似于以下内容:
PATH=/Users/myuser/.rvm/gems/jruby-1.5.6/bin

请查看铁人的回复,它应该能帮助您到达目的地。

2
我注意到在.bash_profile文件中source行是正确的,但我一直收到"ruby命令未找到"的提示。所以当我检查rvm list时,我发现没有任何一个ruby版本被选中:
rvm list

rvm rubies

   ruby-2.4.0 [ x86_64 ]

# Default ruby not set. Try 'rvm alias create default <ruby>'.

# => - current
# =* - current && default
#  * - default

然后我通过rvm use 2.4.0进行了选择并再次检查。

rvm list

rvm rubies

=> ruby-2.4.0 [ x86_64 ]

# Default ruby not set. Try 'rvm alias create default <ruby>'.

# => - current
# =* - current && default
#  * - default

看箭头所指的是已安装的Ruby和被注释的字幕。之后,ruby -v 终于回答了我。
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]

我希望这可以帮助你。干杯。

编辑:您可以使用rvm --default use 2.4.0将其设置为默认的Ruby版本。


1
顺便提一下,“rvm rubies”已经不存在了。 - Dorian

1
我将我的shell设置为登录shell(打开终端>编辑>个人资料首选项>命令选项卡>“将命令作为登录shell运行”),但我不能100%确定它是否需要。
安装完RVM + Ruby后,您必须指定要使用的Ruby版本。我喜欢使用
rvm use --latest --default

1

rvm use 2.6.3 --default 可以解决问题,但每次我关闭Ubuntu后,问题又出现了! 这个链接帮助了我。https://github.com/rvm/rvm/issues/3682

  1. sudo vim .bash_profile
  2. 添加以下两行:
[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session as a function    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接