[SOLVED] Sudo npm: command not found on Ubuntu 20.04LTS?

How To Solve The Error Sudo Npm Command Not Found On Ubuntu 20.04LTS

Hello folks, In this article, we will discuss how to solve the error npm: command not found.

What is npm?

npm stands for node package manager. You may get this error while installing the node package manager. Node and npm packages are not related to nodejs and therefore not included in the nodejs package.

So to use the npm and node command, you need to install both packages. In Windows, npm is installed with NodeJS but in Ubuntu, you need to install it separately. You can either install from the Ubuntu repository or node source repository. Follow this article to see both the methods:

Solution 1: Using Ubuntu repository

Open a terminal window by pressing Ctrl+Alt+T. Run the following command to update the Ubuntu repository:

sudo apt update

Now, install the nodejs package by running the following command and pressing Y to continue.

sudo apt install nodejs

Now, install the node package manager i.e. npm. You got the error because npm was not installed. Install it by executing the following command:

sudo apt install npm

That’s it! You will not get the error after successfully installing both packages.

To confirm, check the version of node js and npm. Enter the following command to check the installed version.

sudo npm -v
node -v
npm-4

Solution 2: Using node source repository

To install node js along with npm using node source repository, follow the steps below. I will prefer this method because using node source repository will always install the latest version of node js.

Open a terminal by pressing Ctrl+Alt+T. Update the package repository using the following command:

sudo apt update

Now, Install the dependent python libraries by executing the following command:

sudo apt install python3-software-properties

Now install node js PPA or repository by executing the following command:

 curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
npm-7

Replace the setup_16.x with the version you want to install. 15.x is not actively supported anymore i.e. you will not get security and stability updates for the 15.x version. We have installed 16.x version. Installing 15.x will give you a warning message like this.

npm-8

If curl is not installed on your system, you may get an error like curl not found. Install it by executing the following command:

sudo apt install curl
npm-9

Now using the apt command, install node js and npm with a single command i.e. you don’t need to install npm separately here.

sudo apt install -y nodejs

Node JS and npm are installed successfully. Confirm by checking the version of both the packages using the following command:

node --version
npm --version
npm-11

Uninstalling npm in Ubuntu

In case you need to uninstall npm in Ubuntu, You can do so by executing the following command:

sudo apt remove npm

Conclusion

So, we discussed both the methods of installing npm in Ubuntu. Both the methods work perfectly fine, but using the node source repository always installs the latest version. Thank you for reading!!