How do I set $PATH?

2019-05-14 17:35发布

问题:

I'm using Ruby 2.3.0. Every time I open a new bash tab, my ruby version downgrades to 2.2.3. So to ensure I'm using the latest, I do rvm use 2.3.0. That fixes the problem on a per-tab basis, and gives this message:

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'.

So I do echo $PATH and get something pretty self explanatory:

/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

My .bashrc has this:

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

And .bash_profile has this:

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*

This is what I get from 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:       ""

So this confuses me even further. rvm list rubies says I have only 2.3.0, but my version here is set to 2.2.3.

I can see pretty clearly what I need to do, which is (I think) set /Users/mkeable/.rvm/gems/ruby-2.3.0/bin at the start of $PATH. But how?

回答1:

Assuming you have your Ruby 2.2 binary in /usr/local/bin, then you definitely do not want this path in front of your /Users/mkeable/.rvm/gems/ruby-2.3.0/bin.

To ensure the former in front of the latter do

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

in your .bash_profile.

One additional note - if Ruby 2.2 is indeed inside of /usr/local/bin, that means you may just be able to remove (or temporarily rename) the binary from that path instead of re-arranging your $PATH.



回答2:

On my mac in ~/.profile I have:

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*

In my ~/.bash_profile:

source ~/.profile

After these configurations close your terminals (Cmd + q on a mac) and open it again. Ensure all requirements are installed, RVM works and ruby 2.3.0 is installed:

rvm requirements
rvm info
rvm install ruby-2.3.0

In your project I suggest to save a .rvmrc file in your project root folder:

rvm use ruby-2.3.0

or with a gemset:

rvm use ruby-2.3.0@my_projet_gemset

This way you switch to ruby 2.3.0 (and your gemset) every time you enter the project root folder.

Works great for me.



回答3:

Putting export PATH="/Users/mkeable/.rvm/gems/ruby-2.3.0/bin:$PATH" in .bash_profile put the correct Ruby binary at the start of the path. Removing source ~/.profile from .bash_profile erased all that doubling I got from echo $PATH. .bashrc is already loaded, so there's no reason to load it a second time.



标签: ruby macos rvm