前两天买了块 SSD,所以重装了系统,作为一名光荣的 RoR 开发人员,RVM 少不了,在重装 RVM 的过程中就遇到了一点问题。
按照官方文档 [‘Installing RVM’][1],我以单用户模式安装 (Single-User installations):
curl -L get.rvm.io | bash -s stable
一切顺利的话,会自动下载 RVM 的安装脚本并进行安装。成功后终端中会有很多无用输出(后面有逆袭)。
引用官方文档:
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.
按照我当时的理解,这里没有给任何需要运行的命令,然后就天真的以为不需要配置,进行下一步。
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,大功告成。
由于种种原因,你总会关掉现在的终端而另开一个,这时再进入项目文件夹执行
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.
其实上面的错误提示已经很明显了:
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 生活。
上面确实解决了 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.
那就按照指示看看第一步的自以为无用的输出,其中比较关键的下面三点:
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.
In case of any issues read output of 'rvm requirements' and/or 'rvm notes'
Installation of RVM in /home/reyesyang/.rvm/ is almost complete:
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 载入,故悲剧发生了。
Installation of RVM in /home/reyesyang/.rvm/ is almost complete:
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 中的配置,解决方法也不少:
bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
故该 login shell 也会自动载入~/.bashrc 中的配置。[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