[node.js] Node Version Manager install - nvm command not found

I am trying to install NVM as per these instructions

I typed in this command in terminal:

$ curl https://raw.github.com/creationix/nvm/master/install.sh | sh

After running the install, I restart the terminal and attempt to install Node.js with this command:

$ nvm install 0.8

but I get the response:

-bash: nvm: command not found

I'm not sure what I am doing wrong here.

Additional Info--

I've been looking around for solutions from other posts and forums. I found another solution using

$ git clone git://github.com/creationix/nvm.git ~/.nvm

but this times out every time I attempt that. Any help would be appreciated. Thanks.

This question is related to node.js terminal nvm

The answer is


I think you missed this step:

source ~/.nvm/nvm.sh

You can run this command on the bash OR you can put it in the file /.bashrc or ~/.profile or ~/.zshrc to automatically load it

https://github.com/creationix/nvm


If you are using OS X, you might have to create your .bash_profile file before running the installation command. That did it for me.

Create the profile file

touch ~/.bash_profile

Re-run the install and you'll see a relevant line in the output this time.

=> Appending source string to /Users/{username}/.bash_profile

Reload your profile (or close/re-open the Terminal window).

.  ~/.bash_profile

Quick answer

Figure out the following:

  1. Which shell is your terminal using, type in: echo $0 to find out (normally works)
  2. Which start-up file does that shell load when starting up (NOT login shell starting file, the normal shell starting file, there is a difference!)
  3. Add source ~/.nvm/nvm.sh to that file (assuming that file exists at that location, it is the default install location)
  4. Start a new terminal session
  5. Profit?

Example

As you can see it states zsh and not bash. enter image description here

To fix this I needed to add source ~/.nvm/nvm.sh to the ~/.zshrc file as when starting a new terminal my Deepin Terminal zsh reads ~/.zshrc and not bashs ~/.bashrc.

Why does this happen

This happens because when installing NVM it adds code to ~/.bashrc, as my terminal Deepin Terminal uses zsh and not bash it never reads ~/.bashrc and therefor never loads NVM.

In other words: this is NVMs fault.

More on zsh can be read on one of the answers here.


For MacOS;

Run on Terminal >

open ~/.bash_profile

Paste all of this=

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

For me this worked. First check that the file .bashrc has following line

[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm

Now bashrc is loaded for each terminal session. Thus restart the terminal so that it is loaded again and you should be good to go.


Not directly connected to the question, but there is a similar problem that may happen, take a look at this question: Can't execute nvm from new bash


Here's my answer on that post, just for the reference:

If you are running from a new bash instance, and you HAVE the initialization code at your ~/.bashrc, ~/.bash_profile, etc, then you need to check this initialization file for conditionals.

On Ubuntu 14, there is a:

case $- in
    *i*) ;;
      *) return;;
esac

At line 6, that will halt it's execution if bash is not being ran with the "-i" (interactive) flag. So you would need to run:

bash -i

Also, at the end of the file, there is a

[ -z "$PS1" ] && return

That will halt it's execution if not being ran with $PS1 set (like on a remote ssh session).

If you do not wish to add any env vars or flags, you will need to remove those conditionals from your initialization file.

Hope that's helpful.


For the issue was fixed when I moved

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

to the end of .zshrc


This works for me:

  1. Before installing nvm, run this in terminal: touch ~/.bash_profile

  2. After, run this in terminal:
    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash

  3. Important... - DO NOT forget to Restart your terminal OR use command source ~/.nvm/nvm.sh (this will refresh the available commands in your system path).

  4. In the terminal, use command nvm --version and you should see the version


In macOS, i had to source it using source ~/.nvm/nvm.sh command to fix this problem.

After that, add these lines

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

onto ~/.bash_profile so that nvm will be sourced automatically upon login.


After trying multiple steps, not sure what was the problem in my case but running this helped:

touch ~/.bash_profile
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash

Verified by nvm --version

nvm -v output


OSX 10.15.0 Catalina (released November 2019) changed the default shell to zsh.

The default shell was previously bash.

The installation command given on the nvm GitHub page needs to be tweaked to include "zsh" at the end.

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | zsh

Note: you might need to ensure the .rc file for zsh is present beforehand:

touch ~/.zsrhrc

Use following codes

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash`
source ~/.nvm/nvm.sh`
nvm install 0.8

Add the following lines to the files ~/.bashrc and ~/.bash_profile :

# NVM changes
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

and restart the terminal or do source ~/.bashrc or source ~/.bash_profile. If you need command completion for nvm then also add the line:

[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

Along with the above lines to ~/.bashrc and ~/.bash_profile.


On Debian, as well as adding the below lines to my .bash_profile as one of the above answers said. I also had to open up my terminal preferences (Edit -> Profile Preferences -> Command) and enable 'Run command as a login shell' to get it to work.

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

Edit: For those on Mac be aware that macOS doesn't read .bashrc on Terminal start, so using .bash_profile is preferable. See Here.


I have the same problem and what saved my life is the sentence "you may have to add to more than one of your "~/.bashrc, ~/.profile, or ~/.zshrc files". the following lines were in my .bashrc only, I added it to files ".bash_profile" and ".profile" and worked for me .

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

All answers to this questions are useful. Especially the answer given by Travis helped me. For Mac OS X users I would like to provide some steps which will help them to work with the fresh installation of Node Version Manager a.k.a. nvm.

Installing & using nvm on Mac OS X

Here are the steps for fresh installation of nvm and using it without any issue:

  • Install homebrew from here.
  • Using homebrew install nvm

    brew update brew install nvm

  • Create .nvm directory at ~/.nvm location.

    mkdir ~/.nvm

  • Now if you don't have .bash_profile file setup for OS X terminal then please create a .bash_profile at the root level:

    nano ~/.bash_profile

  • Paste below code in the .bash_profile and press CTRL + O and press enter to save .bash_profile file. Press CTRL + X to exit from editor:

    export NVM_DIR=~/.nvm source $(brew --prefix nvm)/nvm.sh

  • Now either quite (CMD + Q) the terminal or run below command to load .bash_profile settings:

    source ~/.bash_profile

  • Now run nvm ls command to get the list of all installed nodejs versions.


I had fixed this problem.

  1. touch ~/.bash_profile
  2. open ~/.bash_profile
  3. pasteexport 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

First add following lines in ~/.bashrc file

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

then open terminal and source the nvm.sh script

source ~/.nvm/nvm.sh

For my case, it because I use fish. if I not start fish, just type nvm will no error now.


For Mac OS:

  1. Open Terminal
  2. Run touch ~/.bash_profile
  3. Run vi ~/.bash_profile
  4. Type source ~/.nvm/nvm.sh
  5. Press Shift + Esc and type wq and press enter
  6. Done.

source ~/.nvm/nvm.sh Add this line to ~/.bashrc, ~/.profile, or ~/.zshrc


Had the same problem, but this worked for me:

Since macOS 10.15, the default shell is zsh and nvm will look for .zshrc to update, none is installed by default. Create one with touch ~/.zshrc and run the install script again.

more info here: Troubleshooting on macOS


I also faced the same problem recently and sourcing nvm bash script by using source ~/.nvm/nvm.sh resolved this issue.


In Windows 8.1 x64 same happened with me, and received the following message.

nvm install 8.3.0 bash: nvm: command not found windows

So, follow or verify below following steps-

first install coreybutler/nvm-windows from github.com. Currently available latest release 1.1.5 nvm-setup.zip, later extracted the setup nvm-setup.exe and install as following locations:

NVM_HOME    : C:\Users\Administrator\nvm
NVM_SYMLINK : C:\Program Files\nodejs

and meanwhile setup will manage the environment variable to Path as above said for you.

Now run Git Bash as Administrator and then.

$ nvm install 8.3.0 all

Downloading node.js version 8.3.0 (64-bit)...
Complete
Creating C:\Users\Administrator\nvm\temp

Downloading npm version 5.3.0... Complete
Installing npm v5.3.0...

Installation complete. If you want to use this version, type

nvm use 8.3.0

$ nvm use 8.3.0
Now using node v8.3.0 (64-bit)

here run your command without using prefix $, it is just shown here to determine it as a command line and now we will verify the nvm version.

$ nvm --version
Running version 1.1.5.

Usage:
-----------------------

if you have problem using nvm to install node, you can see this list of available nodejs releases here https://nodejs.org/download/release/ and choose the correct installer as per your requirement version equal or higher than v6.3.0 directly.


The nvm install script by default adds initialization code to your $HOME/.profile, which is only loaded by a login shell (in a desktop environment you may never see a login shell).

The nvm command in your login shell is not propagated to sub-shells (like console windows and IDE terminals after you log in). This snippet in your $HOME/.bashrc will only load nvm if it is an interactive shell and has not been loaded already

# if nvm dir is not set and the standard nvm directory exists
if [ -z "$NVM_DIR" -a -d "$HOME/.nvm" ] ; then
# set nvm dir
  export NVM_DIR="$HOME/.nvm"
fi

# if nvm dir is set and this shell is interactive
if [ -d "$NVM_DIR" -a -n "$PS1" ] ; then
  # if nvm command is not defined
  if ! type -t nvm >/dev/null ; then
    # set it
    source "$NVM_DIR/nvm.sh"
  fi
fi

Putting this in your $HOME/.bashrc file will fix the missing nvm problem in interactive bash shells, even from a gui, and even if nvm is installed in a non-standard location.


Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to terminal

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave Flutter command not found VSCode Change Default Terminal How to switch Python versions in Terminal? How to open the terminal in Atom? Color theme for VS Code integrated terminal How to edit a text file in my terminal How to open google chrome from terminal? Switch between python 2.7 and python 3.5 on Mac OS X

Examples related to nvm

How can the default node version be set using NVM? How to properly upgrade node using nvm nvm is not compatible with the npm config "prefix" option: Node Version Manager (NVM) on Windows nvm keeps "forgetting" node in new terminal session Node Version Manager install - nvm command not found