DEV Community

Carsten Behrens
Carsten Behrens

Posted on • Updated on

How to fix "Unknown command nvm" on Linux

Here is how you can fix "nvm: command not found" on Linux.

1. Verify that you have nvm installed

sudo find / -name "nvm.sh"

Alt Text

This command should return the full path of the file.

If it does not return the full path to then the installation of nvm probably was not successful.

Verify that nvm was installed under the correct path

Also, notice that the .nvm directory is located in my home directory. Your .nvm directory should be located in your home directory as well.

If it's not, you probably installed .nvm with the wrong user or using sudo.

In this case, you should try to reinstall nvm.

Use this command if you are unsure where your home directory is.

echo $HOME
Enter fullscreen mode Exit fullscreen mode

Verify that you restarted your terminal session

To use nvm after installation you need to restart your terminal session. Simply close your current terminal and open a new terminal.

2. Source nvm for your Shell

Now you need to source the nvm.sh file, so that nvm can be used in the shell.

For bash

Add this to your .bashrc

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
Enter fullscreen mode Exit fullscreen mode

Remember to also use the correct path, it can differ from the path that you see here.
Also notice that you have to enter the nvm directory instead of the full path to the NVM_DIR variable.

After adding the two lines of configuration you should restart your terminal emulator.
Now you should be able to use nvm.

For zsh

Add this to your .zshrc

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
Enter fullscreen mode Exit fullscreen mode

Now restart your terminal emulator and that's it, you should be able to use nvm now.

For fish

This one is a bit more difficult. Since fish is not fully POSIX compliant, it is not compatible with the nvm.sh file nvm provides.

To make it work anyway, we can use Bass. It allows us to use the nvm.sh
file in the fish shell without any modifications.

We'll use the Fisher plugin manager to make the installation process as easy as possible.

1. Install Fisher

curl -sL https://git.io/fisher | source && fisher install jorgebucaran/fisher
Enter fullscreen mode Exit fullscreen mode

2. Install Bass

fisher install edc/bass
Enter fullscreen mode Exit fullscreen mode

3. Source nvm

Now that bass is installed, add this to your config.fish file:

function nvm
   bass source $HOME/.nvm/nvm.sh --no-use ';' nvm $argv
end
Enter fullscreen mode Exit fullscreen mode

Remember to also use the correct path.

That's it! Now you can use nvm in fish.

Top comments (3)

Collapse
 
yoursunny profile image
Junxiao Shi

For me, the most common reasons of "unknown command nvm" are:

  • I just ran the nvm installer, but did not exit and restart the bash session, so that source did not happen.
  • I installed nvm in one user account and try to access it in another user account or sudo.
Collapse
 
carstenbehrens profile image
Carsten Behrens

Yes its true. Nvm not being installed in the users directory is probably the most common error. I'll add this comment to this blog post, thanks for your great input.

Collapse
 
zer0cream profile image
Dan • Edited

Dies for me for some reason. I had nvm working I could use nvm install, use etc. However, for some reason not "nvm use default 12" e.g. I just tried the steps you explained, but running into the same problem again.

I'm using fish shell as you can see from the output. The error is from within the NVM package.

 ✘  ⚙  ~/.nvm  fisher install edc/bass                                                                                                                                                                                                                                                                                                       1191ms
fisher install version 4.3.0
/usr/local/opt/nvm/nvm.sh (line 754): Unexpected ')' found, expecting '}'
      1) COLOR=$(nvm_print_color_code "$(echo "$NVM_COLORS" | awk '{ print substr($0, 1, 1); }')");;
       ^
from sourcing file /usr/local/opt/nvm/nvm.sh
    called on line 185 of file /usr/local/Cellar/fish/3.1.2/share/fish/config.fish
in function '.' with arguments '/usr/local/opt/nvm/nvm.sh'
    called on line 91 of file ~/.config/fish/config.fish
from sourcing file ~/.config/fish/config.fish
    called during startup
source: Error while reading file '/usr/local/opt/nvm/nvm.sh'
/usr/local/opt/nvm/etc/bash_completion.d/nvm (line 35): Unexpected ')' found, expecting '}'
      alias) __nvm_installed_nodes ;;
           ^
from sourcing file /usr/local/opt/nvm/etc/bash_completion.d/nvm
    called on line 185 of file /usr/local/Cellar/fish/3.1.2/share/fish/config.fish
in function '.' with arguments '/usr/local/opt/nvm/etc/bash_completion.d/nvm'
    called on line 92 of file ~/.config/fish/config.fish
from sourcing file ~/.config/fish/config.fish
    called during startup
source: Error while reading file '/usr/local/opt/nvm/etc/bash_completion.d/nvm'
Fetching https://codeload.github.com/edc/bass/tar.gz/HEAD
Installing edc/bass
           /Users/dan1/.config/fish/functions/__bass.py
           /Users/dan1/.config/fish/functions/bass.fish
Updated 1 plugin/s
Enter fullscreen mode Exit fullscreen mode