What is npm?
npm stands for Node Package Manager. It is a tool that downloads add-ons for JavaScript projects. These add-ons are called packages. npm comes together with Node.js, which lets you run JavaScript on a server instead of in a browser.
Method 1: Install from the Ubuntu package list
This is the simplest way. The version may be older than the newest release.
- Log in to your server over SSH.
- Update your package list.
sudo apt update
- Install Node.js and npm.
sudo apt install -y nodejs npm
This installs both tools in one go.
- Check the versions.
node -v
npm -v
Method 2: Install a newer version with NodeSource
NodeSource is a trusted source for current Node.js releases. Use this if your app needs a newer version.
- Install curl, a tool that downloads files.
sudo apt install -y curl
- Add the NodeSource list. The number 20 is an example. Choose the version you need.
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
- Install Node.js. This already includes npm.
sudo apt install -y nodejs
- Check the versions again with
node -vandnpm -v.
Try it out
- Make a folder with
mkdir demoand go in withcd demo. - Run
npm init -y. This creates apackage.jsonfile, which lists your project's packages. - Run
npm install lodash. This downloads a sample package.
Update npm
sudo npm install -g npm@latest
This updates npm to its newest version for all users.
Common problems
- Permission denied: Use
sudofor global installs, the ones with-g. - Old version: Use Method 2.
- Command not found: Log out and in again.
Tip: Only run install scripts from sources you trust. Read the line before you paste it.
Quick recap
- npm downloads packages for Node.js projects.
- Use
apt install nodejs npmfor a quick setup. - Use NodeSource for newer versions.
- Check with
node -vandnpm -v.