Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install from zsh doesn't install to bash. Install from bash doesn't install to bash #1717

Open
MichaelJCole opened this issue Feb 1, 2018 · 10 comments
Labels
installing nvm: profile detection Issues with nvm's own install script, related to figuring out the right profile file. pull request wanted This is a great way to contribute! Help us out :-D

Comments

@MichaelJCole
Copy link

  • Operating system and version:
    Linux mcdesktop 4.13.0-32-generic nvm.sh fails to source on initial install #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?

  1. From zsh, ran curl installer
  2. Installed to zsh
  3. closed/opened zsh
  4. nvm works in zsh
  5. Started bash (from zsh)
  6. nvm not found <- error
  7. From bash, ran curl installer
  8. exit/start bash
  9. 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"
@MichaelJCole
Copy link
Author

MichaelJCole commented Feb 1, 2018

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

@ljharb ljharb added the installing nvm: profile detection Issues with nvm's own install script, related to figuring out the right profile file. label Feb 2, 2018
@ljharb
Copy link
Member

ljharb commented Feb 2, 2018

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

@ljharb ljharb added the pull request wanted This is a great way to contribute! Help us out :-D label Feb 2, 2018
@MichaelJCole
Copy link
Author

MichaelJCole commented Feb 3, 2018

@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?

@ljharb
Copy link
Member

ljharb commented Feb 4, 2018

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.

@MichaelJCole
Copy link
Author

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?

@JimiC
Copy link

JimiC commented Feb 22, 2018

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

@JemiloII
Copy link
Contributor

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

@MuhsinFatih
Copy link

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

@ljharb
Copy link
Member

ljharb commented Aug 2, 2018

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

@MuhsinFatih
Copy link

MuhsinFatih commented Aug 2, 2018

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
installing nvm: profile detection Issues with nvm's own install script, related to figuring out the right profile file. pull request wanted This is a great way to contribute! Help us out :-D
Projects
None yet
Development

No branches or pull requests

5 participants