RVM/rbenv 手把手安装 RVM 以及为什么 RVM is not a function

reyesyang · 2012年06月07日 · 最后由 reyesyang 回复于 2015年04月19日 · 22968 次阅读
本帖已被管理员设置为精华贴

前两天买了块 SSD,所以重装了系统,作为一名光荣的 RoR 开发人员,RVM 少不了,在重装 RVM 的过程中就遇到了一点问题。

貌似顺利的安装

按照官方文档 [‘Installing RVM’][1],我以单用户模式安装 (Single-User installations):

1. Download and run the RVM installation script

curl -L get.rvm.io | bash -s stable

一切顺利的话,会自动下载 RVM 的安装脚本并进行安装。成功后终端中会有很多无用输出(后面有逆袭)。

2. Load RVM into your shell sessions as a function

引用官方文档:

Single-User: The rvm function will be automatically configured for every user on the system if you install as single user. Read the output of installer to check which files were modified.

按照我当时的理解,这里没有给任何需要运行的命令,然后就天真的以为不需要配置,进行下一步。

3. Reload shell configuration & test

source ~/.rvm/script/rvm
type rvm | head -n 1 # rvm is a function
rvm requirement

这些命令运行都健康通过,然后在同一个终端中继续下一步:

rvm install 1.9.3
cd path/to/my/rails/project
bundle install
rails s

都顺利通过。Ok,大功告成。

突然就悲剧了

1. 症状

由于种种原因,你总会关掉现在的终端而另开一个,这时再进入项目文件夹执行

rails s

发现了错误提示

The program 'rails' is currently not installed. You can install it by typing: sudo apt-get install rails

很诡异呀,一路按照官方文档安装都很顺利,为什么重开个终端就不行了?这时再执行:

rvm use

应该会有类似下面的提示:

RVM is not a function, selecting rubies with 'rvm use ...' will not work. You need to change your terminal settings to allow shell login. Please visit https://rvm.io/workflow/screen/ for example.

2. 初步修复

其实上面的错误提示已经很明显了:

You need to change your terminal settings to allow shell login.

将 Ubuntu 的 Gnome Terminal 改为以 login shell 运行,可参考 [Integrating RVM with gnome-terminal][2]。 现在关掉现有终端重开一个或者在现有终端中执行:

bash --login

然后运行:

rvm use

一切都又正常了,程旭猿们又过上了和谐安康的 code 生活。

Dig More

上面确实解决了 RVM 的问题,但难免有些疑问:

为什么改为 login shell RVM 就正常了?

这个还要从我们的安装步骤说起,温习官方文档第二步: Load RVM into your shell sessions as a function

Single-User: The rvm function will be automatically configured for every user on the system if you install as single user. Read the output of installer to check which files were modified.

虽然这里没有给出任何明确命令让我们执行,但还是给了重要的提示:Read the output of installer to check which file were modified.

那就按照指示看看第一步的自以为无用的输出,其中比较关键的下面三点:

  1. Installing RVM to /home/reyesyang/.rvm/ Adding rvm PATH line to /home/reyesyang/.bashrc /home/reyesyang/.zshrc. Adding rvm loading line to /home/reyesyang/.bash_login /home/reyesyang/.zlogin.

  2. In case of any issues read output of 'rvm requirements' and/or 'rvm notes'

  3. Installation of RVM in /home/reyesyang/.rvm/ is almost complete:

    • To start using RVM you need to run source /home/reyesyang/.rvm/scripts/rvm in all your open shell windows, in rare cases you need to reopen all shell windows.

行面可以看出,RVM 在安装是给~/.bashrc(~/.zshrc) 和~/.bash_login(~/.zlogin) 中分别添加了 RVM PATH:

PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting

和 RVM loading line:

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

Ubutnu 的 Gnome Terminal 在启动时是 non-login shell,而 non-login shell 只会读取~/.bashrc 来进行初始化,所以没有读入写在~/.bash_login 中的 RVM loading line,以至于 RVM 没有作为 function 载入,故悲剧发生了。

所以 RVM 官方给了我们两种解决办法:

  1. 就是设置 Gnome Terminal 默认以 login-shell 的方式启动,这样就会读取~/.bash_login 来初始化 shell,就解决问题了。
  2. 如第一步安装完后输出中的提示:

Installation of RVM in /home/reyesyang/.rvm/ is almost complete:

  • To start using RVM you need to run source /home/reyesyang/.rvm/scripts/rvm in all your open shell windows, in rare cases you need to reopen all shell windows.

但其实第一种方法有副作用

就是 login shell 不会读取我们在~/.bashrc 中的配置,解决方法也不少:

  1. 将 RVM loading line 从~/.bash_login 中移到~/.profile 中。但是 login shell 初始化时,如果~/.bash_login 存在,就不会读取~/.profile(可参考 [类 unix 系统如何初始化 shell][3]),所以要将~/.bash_login 文件删除。而~/.profile 文件中存在如下代码: bash if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi fi 故该 login shell 也会自动载入~/.bashrc 中的配置。
  2. 将~/.profile 文件中的代码拷贝到~/.bash_login 中
  3. 直接将 RVM loading line 从~/.bash_login 中移到~/.bashrc 中,但不删除~/.bash_login,就需要确保 Gnome Terminal 以 non-login shell 的方式启动。
  4. 直接将 RVM loading line 从~/.bash_login 中移到~/.bashrc 中,删除~/.bash_login,这时 Gnome Terminal 以 non-login shell 或 login-shell 的方式启动均可。

参考

  1. [Installing RVM][1]
  2. [类 unix 系统如何初始化 shell][3]
  3. [What shell login means ('bash -l')][4]

[1]: https://rvm.io/rvm/install/#explained [2]: https://rvm.io/integration/gnome-terminal/ [3]: http://reyesyang.info/articles/26-how-to-initialize-a-new-shell-in-unix-like-os [4]: https://rvm.io/support/faq/#shell_login

原文链接

手把手安装 RVM 以及为什么 RVM is not a function

匿名 #1 2012年06月07日

我写过一篇关于 RVM 的博客。针对 Debian Linux 做的实验。相信其它平台都是大同小异。一步步慢慢来,别落下什么东西,一般不会遇到太大困难。http://www.tylerlong.me/1329747539/

嗯,这个应该是精华帖子了

用 zsh 也需要手动添加,有问题看看 rvm notes 准没错

今天也遇到了这个问题,回想是因为之前安装了 zsh,后来觉得不习惯又切换回来,查看当前的 bash,echo $SHELL,竟然莫名其妙的设置为了/bin/sh,后用 chsh 重新设置为 bash,chsh -s /bin/bash,问题解决。

我也经常遇到过,没楼主这样分析和整理。

楼主的帖子写的很细致,赞一个。 我的做法是把 loading line 放到 .bashrc 中,然后在 .bash_login 中装载 .bashrc

好帖子,终于找到问题的所在了啊。

谢谢 楼主分享!

#4 楼 @douguohui 我按照你的方法试了,可是还是显示/bin/sh啊,我用 chsh 和 exec 各种方法都试过了,而且/etc/passwd 里面写入的也是 bash

谢谢楼主,新手受教

楼主我遇到一个问题是:我用终端查看 ruby 版本如图所示,但是在我创建的一个工程中访问http://localhost:3000/ruby版本为2.1.5![](https://l.ruby-china.com/photo/2015/3d0b07f02ee98a7317a91196f800ac7e.png)所显示的 不知道是为什么,希望楼主能够指教

楼主我遇到一个问题是:我用终端查看 ruby 版本如图所示,但是在我创建的一个工程中访问http://localhost:3000/ruby2.1.5版本为 所显示的 不知道是为什么,希望楼主能够指教

#12 楼 @trithirty rvm list 返回啥结果呢?

@reyesyang

rvm rubies

=* ruby-2.2.1 [ x86_64 ] ruby-2.2-head [ x86_64 ]

=> - current

=* - current && default

* - default

#14 楼 @trithirty 没遇到过这样的情况啊,好奇怪。

@reyesyang 楼主我能加你 qq 吗?我是新人,之前都没有过 web 开发经验,好多困惑,能在 qq 上请教你吗?

@reyesyang 楼主我能加你 qq 吗?我是新人,之前都没有过 web 开发经验,好多困惑,能在 qq 上请教你吗?

#17 楼 @trithirty QQ 不常用,你有问题发我邮件 [email protected]

今天我也遇到这个问题了。感谢楼主的总结。

hope to tell us your system, ubuntu, fedora or others?

需要 登录 后方可回复, 如果你还没有账号请 注册新账号