Last updated: 23 October 2026. This guide now combines my earlier separate posts on installing npm and installing NVM.
npm comes with Node.js: install Node and you get npm too, so you never install npm on its own. This guide shows the three ways to install them on Ubuntu, which one to pick, and the npm commands you'll use every day. For development I use nvm, because different projects often need different Node versions.
Contents
- Which method should I use?
- Method 1: nvm (recommended for development)
- Method 2: NodeSource (one current version, good for servers)
- Method 3: Ubuntu's own packages (quickest, oldest)
- npm basics
- Troubleshooting
- Uninstalling
1. Which method should I use?
| Method | Node version | Best for |
|---|---|---|
| nvm | Any version, several side by side | Your own development machine |
| NodeSource | One current LTS line, updated with apt | Servers, CI, Docker images |
| Ubuntu apt | Whatever your Ubuntu release shipped with, often well behind | Quick tests only |
Pick one. Mixing methods gives you two Node installations, and node -v may not show the one you expect. If you've installed Node before, check with which node and remove the old one first (see section 7).
Which Node version? Use the current LTS (long-term support) release shown on nodejs.org. As of October 2026, that's Node 24, with Node 26 due to become LTS at the end of the month. Tools like Vite need a recent LTS, so avoid older versions such as Node 18 and 20, which no longer get updates.
2. Method 1: nvm (recommended for development)
nvm (Node Version Manager) installs Node into your home folder. You never need sudo for npm, and you can switch versions per project.
Install nvm
sudo apt update
sudo apt install curl
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.8/install.sh | bash
v0.40.8 was the latest release when I updated this post. Check the nvm releases page and use the newest version number in the URL.
Then load nvm in your current terminal, or simply close and reopen it:
source ~/.bashrc
nvm --version
If you use zsh, run source ~/.zshrc instead; the install script prints which file it changed.
Install Node.js (and npm)
nvm install --lts # latest LTS version
node -v
npm -v
Switch between versions
nvm install 22 # install another version
nvm ls # list installed versions
nvm use 22 # use it in this terminal
nvm alias default 24 # version used in every new terminal
Tip: put the version a project needs in a file named .nvmrc in the project folder (for example, containing just 24). Then nvm use in that folder switches automatically, and teammates use the same version.
Note: global npm packages are installed per Node version. After switching to a new version, reinstall any global tools you need, or migrate them with nvm install 26 --reinstall-packages-from=24.
3. Method 2: NodeSource (one current version, good for servers)
NodeSource provides up-to-date Node.js packages for apt. Replace 24 with the current LTS major version:
curl -fsSL https://deb.nodesource.com/setup_24.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh
sudo apt install -y nodejs
node -v
npm -v
The nodejs package already includes npm. Don't also run sudo apt install npm; it pulls in Ubuntu's separate npm package and causes conflicts.
Downloading the script to a file first (instead of piping straight into bash) lets you open and read it before running it with sudo.
4. Method 3: Ubuntu's own packages (quickest, oldest)
sudo apt update
sudo apt install nodejs npm
node -v
This installs whatever version your Ubuntu release shipped with, which is often too old for current tools such as Vite. Fine for a quick test; use nvm or NodeSource for real projects.
5. npm basics
Start a project
mkdir my-app && cd my-app
npm init -y # creates package.json with defaults
package.json records your project's name, scripts and dependencies. package-lock.json, created on the first install, records the exact versions installed. Commit both to Git, and add node_modules/ to .gitignore.
Everyday commands
| Command | What it does |
|---|---|
npm install axios | Add a package your app needs (saved under dependencies) |
npm install -D vitest | Add a development-only tool (saved under devDependencies) |
npm install | Install everything listed in package.json, for example after cloning a project |
npm ci | Clean, exact install from package-lock.json; use it in CI and Docker |
npm run dev | Run a script defined in the scripts section of package.json |
npm uninstall axios | Remove a package |
npm outdated | List packages with newer versions available |
npm update | Update packages within the version ranges allowed by package.json |
npm audit | Check dependencies for known security vulnerabilities |
npx create-vite@latest | Run a package's command once without installing it globally |
Prefer npx or project-level (-D) installs over global installs with -g; that way every project records the exact tools it needs.
Next: build a real project with my React, Vite and Axios tutorial.
6. Troubleshooting
| Problem | Fix |
|---|---|
nvm: command not found after installing | Close and reopen the terminal, or run source ~/.bashrc. Check the nvm lines were added to that file. |
EACCES: permission denied on npm install -g | Don't fix it with sudo. Switch to nvm, which installs into your home folder. |
| A tool says your Node version is too old | Check with node -v. With nvm: nvm install --lts && nvm alias default lts/*. |
node -v shows a different version from the one you installed | Two installations exist. which node shows which one runs; remove the one you don't want. |
| Strange errors after changing Node versions | Delete node_modules and reinstall: rm -rf node_modules && npm install. |
7. Uninstalling
nvm: remove one Node version with nvm uninstall 22. To remove nvm completely, delete its folder with rm -rf ~/.nvm, then delete the nvm lines (starting export NVM_DIR) from ~/.bashrc or ~/.zshrc.
NodeSource or Ubuntu apt:
sudo apt remove nodejs npm
sudo apt autoremove
For NodeSource, also remove its repository file from /etc/apt/sources.list.d/ (the file whose name contains nodesource) so apt stops checking it.