Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to install npm on Ubuntu

This guide shows you two ways to install npm on Ubuntu. It works on a VPS or dedicated server where you have sudo access.

How-To Guides2 min read11 steps

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.

  1. Log in to your server over SSH.
  2. Update your package list.
sudo apt update
  1. Install Node.js and npm.
sudo apt install -y nodejs npm

This installs both tools in one go.

  1. 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.

  1. Install curl, a tool that downloads files.
sudo apt install -y curl
  1. 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 -
  1. Install Node.js. This already includes npm.
sudo apt install -y nodejs
  1. Check the versions again with node -v and npm -v.

Try it out

  1. Make a folder with mkdir demo and go in with cd demo.
  2. Run npm init -y. This creates a package.json file, which lists your project's packages.
  3. 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 sudo for 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 npm for a quick setup.
  • Use NodeSource for newer versions.
  • Check with node -v and npm -v.