DEV Community

loading...

How to fix "Unknown command nvm" on Linux

carstenbehrens profile image Carsten Behrens Updated on ・2 min read

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.

Discussion (2)

pic
Editor guide
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 Author

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.