如何设置 $PATH?

How do I set $PATH?

我正在使用 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。但是怎么办?

假设您的 Ruby 2.2 二进制文件在 /usr/local/bin 中,那么您绝对不希望这条路径出现在您的 /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.

在 ~/.profile 中我的 mac 我有:

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

或使用宝石集:

rvm use ruby-2.3.0@my_projet_gemset

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

对我来说效果很好。

Putting export PATH="/Users/mkeable/.rvm/gems/ruby-2.3.0/bin:$PATH" in .bash_profile 将正确的 Ruby 二进制文件放在路径的开头。从 .bash_profile 中删除 source ~/.profile 删除了我从 echo $PATH 中得到的所有加倍。 .bashrc 已经加载,所以没有理由加载它。