Nvm: Install from zsh doesn't install to bash. Install from bash doesn't install to bash

Created on 1 Feb 2018  Â·  10Comments  Â·  Source: nvm-sh/nvm

  • Operating system and version:
    Linux mcdesktop 4.13.0-32-generic #35~16.04.1-Ubuntu SMP Thu Jan 25 10:13:43 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

  • nvm debug output:
    works in zsh, not in bash

  • How did you install nvm? (e.g. install script in readme, homebrew):
    in zsh, using curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash

  • What steps did you perform?

  • What happened?

  • From zsh, ran curl installer
  • Installed to zsh
  • closed/opened zsh
  • nvm works in zsh
  • Started bash (from zsh)
  • nvm not found <- error
  • From bash, ran curl installer
  • exit/start bash
  • nvm not found <- error

  • What did you expect to happen?

nvm to be installed.
nvm to install to bash when run from bash
nvm to install in zsh AND bash.
(I use zsh, but write scripts with #/bin/bash for compatibility with people not using zsh)

  • Is there anything in any of your profile files (.bashrc, .bash_profile, .zshrc, etc) that modifies the PATH?
☀  grep PATH .*
<trimmed>
.profile:# set PATH so it includes user's private bin directories
.profile:PATH="$HOME/bin:$HOME/.local/bin:$PATH"
profile detection pull request wanted

Most helpful comment

I had a pr recently that may have solved this issue for you.
Add auto completion support to zsh #1780

However the script detects if your using bash or zsh first.
It checks in this order:
if bash
.bashrc
.bash_profile
if zsh
.zsh

in the event the shell type is unknown it'll check in this order
.profile, .bashrc, .bash_profile, .zshrc

If installing with zsh, you can do
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | zsh

All 10 comments

Ok, this bash/zsh configuration is a mess. There's three places I want nvm:

  1. zsh interactive (my default shell)
  2. bash interactive (bash from zsh)
  3. non-interactive bash In scripts to automate dev tasks (#/bin/bash)

See more about interactive/non-interactive here

nvm installs for zsh interactive using .zshrc when zsh is installed
BUG: nvm doesn't install for bash interactive when zsh is installed.

WORKAROUND: copy this code into your .bashrc (or other bash config file see link above)

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

TEST:
Open terminal with zsh as default shell
run bash to start interactive bash shell
run nvm and it should work

Profile detection is very tricky. It's always great to get improvements here.

@ljharb Hey Jordan, I could make a PR for this, but it would be a pretty big refactor of install.sh.

My approach would be to refactor nvm_do_install() starting at about install.sh:328 to:

if (.zshrc) add to .zshrc;
if (.bashrc or .bash_profile or .profile) {
  add to first of .bashrc, .bash_profile, .profile;
} 
if (none of the above) {
  show message about installing manually
}

I think that would mean we can get rid of nvm_detect_profile() as we're no longer picking based on the shell.

nvm is a pretty mature project - I wouldn't want to mess up your users. How do you think that would work out?

I'm not sure it makes sense to prioritize zsh over bash in that way; bash is my default shell, but I do have a .zshrc for when I test on zsh.

Hey Jordan, neither would be prioritized over the other. It would add to both .zshrc and .bashrc if available.
(only adding to .zshrc is why I couldn't get it to work when switching to interactive bash).

Is there a reason it should only be in one shell or the other?

I'm using Ubuntu Mate 16.04 and came across this very issue. After straggling for hours, I figured it out.
My solution was to add

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

into .bash_profile. Only then bash was able to recognize nvm.
Note here that the same lines were found in .bashrc but bash wasn't peaking them up.

My suggestion is to add those lines in .zshrc, .bashrc and .bash_profile no matter what.

Another nice trick I found from http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html

Edit your .bash_profile, comment out everything (just to be safe, don't delete anything if you aren't sure) and add

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

I had a pr recently that may have solved this issue for you.
Add auto completion support to zsh #1780

However the script detects if your using bash or zsh first.
It checks in this order:
if bash
.bashrc
.bash_profile
if zsh
.zsh

in the event the shell type is unknown it'll check in this order
.profile, .bashrc, .bash_profile, .zshrc

If installing with zsh, you can do
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | zsh

Why not add the lines to all present known profile files? I am guessing that it would be better to have duplicates rather than missing configuration if it is tricky to get the default shell type

That seems like it would resolve this issue, but it would be brittle and perhaps overly intrusive.

True, but I think inexperienced linux users may be discouraged from using nvm and eventually nodejs as there are occasional permission issues that needs to be fixed manually when used without nvm. This may be a bit too much of a nitpicking, the solution is one googling away for zsh users anyways, but eventually one of two compromises has to be chosen, and one saves time, so :)

Was this page helpful?
0 / 5 - 0 ratings