如何设置$ PATH?

时间:2016-01-30 05:46:40

标签: ruby macos rvm

我使用的是Ruby 2.3.0。每次打开一个新的bash选项卡时,我的ruby版本都会降级到2.2.3。因此,为了确保我使用最新版本,我会rvm use 2.3.0。这解决了每个标签上的问题,并给出了这样的信息:

PATH is not properly set up, '/Users/mkeable/.rvm/gems/ruby-2.3.0/bin' is not at first place,
         usually this is caused by shell initialization files - check them for 'PATH=...' entries,
         it might also help to re-add RVM to your dotfiles: 'rvm get stable --auto-dotfiles',
         to fix temporarily in this shell session run: 'rvm use ruby-2.3.0'.

所以我做echo $PATH并得到一些非常自我解释的东西:

/usr/local/bin:/Users/mkeable/.rvm/gems/ruby-2.3.0/bin:/Users/mkeable/.rvm/gems/ruby-2.3.0@global/bin:/Users/mkeable/.rvm/rubies/ruby-2.3.0/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/Users/mkeable/.rvm/bin

我的.bashrc有这个:

export PATH="/usr/local/heroku/bin:$PATH" export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting

.bash_profile有这个:

source ~/.profile
export PATH=/usr/local/bin:$PATH
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

这是我从rvm info

获得的
Warning! PATH is not properly set up, '/Users/mkeable/.rvm/gems/ruby-2.3.0/bin' is not at first place,
         usually this is caused by shell initialization files - check them for 'PATH=...' entries,
         it might also help to re-add RVM to your dotfiles: 'rvm get stable --auto-dotfiles',
         to fix temporarily in this shell session run: 'rvm use ruby-2.3.0'.

ruby-2.3.0:

  system:
    uname:       "Darwin BC.local 13.4.0 Darwin Kernel Version 13.4.0: Wed Mar 18 16:20:14 PDT 2015; root:xnu-2422.115.14~1/RELEASE_X86_64 x86_64"
    system:      "osx/10.9/x86_64"
    bash:        "/bin/bash => GNU bash, version 3.2.53(1)-release (x86_64-apple-darwin13)"
    zsh:         "/bin/zsh => zsh 5.0.2 (x86_64-apple-darwin13.0)"

  rvm:
    version:      "rvm 1.26.11 (latest) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]"
    updated:      "3 minutes 39 seconds ago"
    path:         "/Users/mkeable/.rvm"

  ruby:
    interpreter:  "ruby"
    version:      "2.2.3p173"
    date:         "2015-08-18"
    platform:     "x86_64-darwin13"
    patchlevel:   "2015-08-18 revision 51636"
    full_version: "ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin13]"

  homes:
    gem:          "/Users/mkeable/.rvm/gems/ruby-2.3.0"
    ruby:         "/Users/mkeable/.rvm/rubies/ruby-2.3.0"

  binaries:
    ruby:         "/usr/local/bin/ruby"
    irb:          "/usr/local/bin/irb"
    gem:          "/usr/local/bin/gem"
    rake:         "/usr/local/bin/rake"

  environment:
    PATH:         "/usr/local/bin:/Users/mkeable/.rvm/gems/ruby-2.3.0/bin:/Users/mkeable/.rvm/gems/ruby-2.3.0@global/bin:/Users/mkeable/.rvm/rubies/ruby-2.3.0/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/Users/mkeable/.rvm/bin"
    GEM_HOME:     "/Users/mkeable/.rvm/gems/ruby-2.3.0"
    GEM_PATH:     "/Users/mkeable/.rvm/gems/ruby-2.3.0:/Users/mkeable/.rvm/gems/ruby-2.3.0@global"
    MY_RUBY_HOME: "/Users/mkeable/.rvm/rubies/ruby-2.3.0"
    IRBRC:        "/Users/mkeable/.rvm/rubies/ruby-2.3.0/.irbrc"
    RUBYOPT:      ""
    gemset:       ""

所以这让我更加困惑。 rvm list rubies说我只有2.3.0,但我的版本设置为2.2.3。

我可以清楚地看到我需要做什么,这是(我认为)在$ PATH的开头设置/Users/mkeable/.rvm/gems/ruby-2.3.0/bin。但是如何?

3 个答案:

答案 0 :(得分:7)

假设您在/usr/local/bin中拥有Ruby 2.2二进制文件,那么您绝对不希望在/Users/mkeable/.rvm/gems/ruby-2.3.0/bin前面显示此路径。

确保前者在后者前面做

export PATH="/usr/local/heroku/bin:$PATH" 
export PATH="$HOME/.rvm/bin:$PATH"

.bash_profile

另外一个注意事项 - 如果Ruby 2.2确实位于/usr/local/bin内,这意味着您可能只能从该路径中删除(或临时重命名)二进制文件,而不是重新安排$ PATH。

答案 1 :(得分:1)

在我的mac上〜/ .profile我有:

export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

在我的〜/ .bash_profile中:

source ~/.profile

完成这些配置后,关闭终端(Mac上的Cmd + q)并再次打开。确保安装了所有要求,RVM正常运行并安装了ruby 2.3.0:

rvm requirements
rvm info
rvm install ruby-2.3.0

在您的项目中,我建议将.rvmrc文件保存在项目根文件夹中:

rvm use ruby-2.3.0

或使用gemset:

rvm use ruby-2.3.0@my_projet_gemset

这样每次进入项目根文件夹时都会切换到ruby 2.3.0(以及你的gemset)。

非常适合我。

答案 2 :(得分:0)

export PATH="/Users/mkeable/.rvm/gems/ruby-2.3.0/bin:$PATH"置于.bash_profile中,将正确的Ruby二进制文件放在路径的开头。从source ~/.profile中移除.bash_profile会删除echo $PATH所获得的所有加倍。 .bashrc已经加载,因此没有理由再次加载它。