RVM is not a function

来源:互联网 发布:mac推出键 编辑:程序博客网 时间:2024/05/16 12:13

转载自http://www.reyesyang.info/articles/27-install-rvm-step-by-step-and-why-rvm-is-not-a-function

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

貌似顺利的安装

按照官方文档 ‘Installing RVM’,我以单用户模式安装(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/rvmtype rvm | head -n 1 # rvm is a functionrvm requirements

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

rvm install 1.9.3cd path/to/my/rails/projectbundle installrails 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。
现在关掉现有终端重开一个或者在现有终端中执行:

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),所以要将~/.bash_login文件删除。而~/.profile文件中存在如下代码:

if [ -n "$BASH_VERSION" ]; then    # include .bashrc if it exists    if [ -f "$HOME/.bashrc" ]; then    . "$HOME/.bashrc"    fifi

故该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的方式启动均可。



0 0
原创粉丝点击