How to Install Node.js with nvm on Ubuntu 22.04 (Latest LTS, Switching Versions)

This guide installs the Node.js LTS release on an Ubuntu 22.04 server with nvm, then covers checking node and npm, insta…

Ubuntu 22.04Node.js 24 LTSx86_64About 9 min

Verified guide

This guide was verified by creating a cloud server and running every command in it, from start to finish, exactly as written.

OS
Ubuntu 22.04.5 LTS · x86_64
Installed version
Node.js 24.22.0 (npm 11.19.1), nvm 0.40.8
Test server
DigitalOcean · 1 vCPU / 1 GB RAM / 25 GB SSD (s-1vcpu-1gb, sgp1)
Last checked
2026-10-15

This guide contains affiliate links. If you sign up through them, Codesand may earn a commission. This doesn't affect the content or our assessments.

This guide installs the Node.js LTS release on an Ubuntu 22.04 server with nvm, then covers checking node and npm, installing and switching between versions, changing the default version, fixing the "Command 'nvm' not found" and "sudo: node: command not found" errors, and removing everything cleanly.

1. Requirements

  • One Ubuntu 22.04 LTS server or PC
  • A regular user account with sudo privileges
  • An internet connection (files are downloaded from nodejs.org and GitHub)

If you don't have a server, try the smallest DigitalOcean Droplet (1 vCPU, 1 GB). It is enough to install and run Node.js. Every step in this guide was verified on a Droplet (1 vCPU, 1 GB memory, 25 GB SSD, Ubuntu 22.04, Singapore) using a regular user account with sudo privileges.

2. Check the state before installing

If Node.js is not installed, you see a message that the node command cannot be found, along with a suggestion to install it with apt.

node -v
Command 'node' not found, but can be installed with:
sudo apt install nodejs

Let's check which version you would get by following that suggestion and installing with apt. First, update the package list. The beginning of the output (the Hit and Get lines) is omitted.

sudo apt update
Fetched 27.9 MB in 10s (2933 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
52 packages can be upgraded. Run 'apt list --upgradable' to see them.

Candidate is the version that apt install nodejs would install. The default Ubuntu 22.04 repository ships Node.js 12.

apt-cache policy nodejs
nodejs:
  Installed: (none)
  Candidate: 12.22.9~dfsg-1ubuntu3.6
  Version table:
     12.22.9~dfsg-1ubuntu3.6 500
        500 http://sgp1.clouds.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages
        500 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages
     12.22.9~dfsg-1ubuntu3 500
        500 http://sgp1.clouds.archive.ubuntu.com/ubuntu jammy/universe amd64 Packages

Node.js 12 reached end of life in April 2022, and the latest LTS on the day this guide was verified was 24. This guide installs Node.js with nvm (Node Version Manager) instead of apt. nvm downloads the official files from nodejs.org into your user folder (~/.nvm), so it does not need sudo, and you can keep several versions installed and switch between them with a single command.

3. Install nvm

Download and run the nvm install script. This guide installs nvm 0.40.8, the latest version on the day it was verified. curl -o- prints the downloaded content to the screen instead of saving it to a file, and | bash passes that content to bash to run it right away.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.8/install.sh | bash
=> Downloading nvm from git to '/home/devuser/.nvm'
=> Cloning into '/home/devuser/.nvm'...
…
=> Compressing and cleaning up git repository

=> Appending nvm source string to /home/devuser/.bashrc
=> Appending bash_completion source string to /home/devuser/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

nvm is installed in the ~/.nvm folder, and three lines that load nvm every time you open a terminal are appended to the end of ~/.bashrc.

4. Verify the nvm installation

If you run nvm right away in the terminal where you installed it, you get a message that the command cannot be found. This is because the terminal was opened before the three lines were added to ~/.bashrc.

nvm --version
Command 'nvm' not found, did you mean:
  command 'vm' from deb mgetty-voice (1.2.1-1.1)
  command 'pvm' from deb pvm (3.4.6-3.2)
  command 'npm' from deb npm (8.5.1~ds-1)
  …
Try: sudo apt install <deb name>

Do not install the suggested packages. Close and reopen the terminal (for SSH, disconnect and reconnect), or reload ~/.bashrc with the command below. It prints nothing.

source ~/.bashrc
nvm --version
0.40.8

5. Install the Node.js LTS release

LTS is the stable release line with long-term support. Install LTS unless you have a specific reason not to. With --lts, nvm finds and installs the latest LTS version.

nvm install --lts
Installing latest LTS version.
Downloading and installing node v24.22.0...
Downloading https://nodejs.org/dist/v24.22.0/node-v24.22.0-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v24.22.0 (npm v11.19.1)
Creating default alias: default -> lts/* (-> v24.22.0 *)

The first version you install becomes the default version (default), which is used every time you open a new terminal. Check that node, npm, and npx run.

node -v
v24.22.0
npm -v
11.19.1
npx -v
11.19.1

You can also check where the node binary that runs is located. It is installed in the .nvm folder inside your user folder.

which node
/home/devuser/.nvm/versions/node/v24.22.0/bin/node

6. Install and switch to another version

Use nvm ls-remote to see which versions you can install. Adding a number shows only that major version, and tail -n 3 shows only the last three lines.

nvm ls-remote 22 | tail -n 3
       v22.23.1   (LTS: Jod)
       v22.23.2   (LTS: Jod)
       v22.23.3   (Latest LTS: Jod)

Install Node.js 22 as well. If you give only the major version, the latest release of that major version is installed, and the current terminal switches to it right away.

nvm install 22
Downloading and installing node v22.23.3...
Downloading https://nodejs.org/dist/v22.23.3/node-v22.23.3-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v22.23.3 (npm v10.9.9)
node -v
v22.23.3

Check the list of installed versions. The one marked with -> is the version used in the current terminal, and default is the version used when you open a new terminal. The lts/… lines that follow are a list of version names, so they are omitted.

nvm ls
->     v22.23.3
       v24.22.0
default -> lts/* (-> v24.22.0)

nvm install 22 changed only that terminal. When you open a new terminal, the default version v24.22.0 is used again.

node -v
v24.22.0

To switch only the current terminal to another version, use nvm use.

nvm use 22
Now using node v22.23.3 (npm v10.9.9)
node -v
v22.23.3

To keep using 22 in new terminals too, change the default version.

nvm alias default 22
default -> 22 (-> v22.23.3)

Open a new terminal, and 22 is used.

node -v
v22.23.3

Set the default version back to 24, and switch the current terminal to the default version as well.

nvm alias default 24
default -> 24 (-> v24.22.0)
nvm use default
Now using node v24.22.0 (npm v11.19.1)

7. Run a first project with npm

To confirm the installation works, install and run an npm package. Create a hello-node folder under your home folder and move into it. npm init -y creates a package.json file with default values without asking any questions.

mkdir hello-node && cd hello-node
npm init -y
Wrote to /home/devuser/hello-node/package.json:

{
  "name": "hello-node",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "type": "commonjs"
}

Install the dayjs package, which handles dates.

npm install dayjs
added 1 package, and audited 2 packages in 1s
found 0 vulnerabilities

Create an index.js file that uses dayjs and run it. The first line creates the file from the text in quotes, so it prints nothing.

echo "const dayjs = require('dayjs'); console.log('Hello Node.js', process.version, dayjs().format('YYYY-MM-DD'));" > index.js
node index.js
Hello Node.js v24.22.0 2026-10-15

If the Node.js version and today's date are shown, both Node.js and npm are working.

8. Troubleshooting

Command 'nvm' not found, did you mean:

This means you ran it in a terminal that was open before nvm was installed. That terminal has not read the three lines added to ~/.bashrc during installation. Close and reopen the terminal, or run source ~/.bashrc. The packages listed with the message are other programs with similar names and have nothing to do with nvm, so do not install them. If a new terminal shows the same message, check with tail -n 3 ~/.bashrc that the three NVM_DIR lines are there.

sudo: node: command not found

Node.js installed with nvm lives in your user folder (~/.nvm), but sudo replaces the command search path (PATH) with the system default path, so it cannot find this folder. Run node and npm without sudo.

sudo node -v
sudo: node: command not found
Command 'node' not found, but can be installed with:

This means Node.js is not installed, or the terminal has not loaded nvm. First check that nvm runs with nvm --version. If nvm cannot be found either, open a new terminal as described above. If nvm runs but you still see this message, check with nvm ls that a version is installed. Do not run the suggested sudo apt install nodejs, because it installs Node.js 12 from the Ubuntu repository.

nvm: Cannot uninstall currently-active node version

You cannot uninstall the version that the current terminal is using. Switch to another version with nvm use, then uninstall it. You can see the actual message in step 9.

9. Uninstall

Remove a single Node.js version

To remove just one of the versions installed with nvm, use nvm uninstall. First, go back to your home folder. The version you are currently using cannot be removed.

cd ~
nvm uninstall 24
nvm: Cannot uninstall currently-active node version, v24.22.0 (inferred from 24).

Version 22, which is not in use, is removed.

nvm uninstall 22
Uninstalled node v22.23.3
nvm ls
->     v24.22.0
default -> 24 (-> v24.22.0)

Remove nvm and all Node.js versions

nvm and every Node.js version installed with it live in the ~/.nvm folder. First, check the three lines added to ~/.bashrc in step 3.

tail -n 3 ~/.bashrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Delete the ~/.nvm folder, the ~/.npm folder where npm keeps downloaded packages, and the practice ~/hello-node folder from step 7. Then delete the three lines containing NVM_DIR from ~/.bashrc. sed -i edits the file in place, and /NVM_DIR/d deletes the lines that contain NVM_DIR. Neither command prints anything.

rm -rf ~/.nvm ~/.npm ~/hello-node
sed -i '/NVM_DIR/d' ~/.bashrc

Close and reopen the terminal to check. If both node and nvm report that the command cannot be found, everything has been removed.

node -v
Command 'node' not found, but can be installed with:
sudo apt install nodejs
nvm --version
Command 'nvm' not found, did you mean:
  command 'kvm' from deb qemu-system-x86 (1:6.2+dfsg-2ubuntu6.31)
  command 'nvme' from deb nvme-cli (1.16-3ubuntu0.3)
  command 'pvm' from deb pvm (3.4.6-3.2)
  …
Try: sudo apt install <deb name>

Try it yourself

No server yet? You can spin up an Ubuntu server in a few minutes with one of these providers. We verified this guide on DigitalOcean.