注意用bash --login 解决 rvm use 的问题

前两天买了块SSD,所以重装了系统,作为一名光荣的RoR开发人员,RVM少不了,在重装RVM的过程中就遇到了一点问题。貌似顺利的安装按照官方文档 ‘Installing RVM’,我以单用户模式安装(Single-User installations):1. Download and run the RVM installation scriptcurl -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 & testsource ~/.rvm/script/rvmtype rvm | head -n 1 # rvm is a functionrvm requirement这些命令运行都健康通过,然后在同一个终端中继续下一步: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 functionSingle-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: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官方给了我们两种解决办法:就是设置Gnome Terminal默认以login-shell的方式启动,这样就会读取~/.bash_login来初始化shell,就解决问题了。如第一步安装完后输出中的提示:Installation of RVM in /home/reyesyang/.rvm/ is almost complete:* To start using RVM you need to run source /home/reyesyang/.rvm/scripts/rvmin 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值