The right way to install Node.js on Ubuntu depends on whether you need a simple system package or control over Node versions per project. APT is straightforward, while nvm is useful when projects require different versions. Whichever route you choose, check what your shell actually runs before installing another copy.
Check what Ubuntu and your project need
Ubuntu’s repositories can provide different Node.js versions depending on the release and configured package sources. Check your Ubuntu version, the available APT candidate, and any existing Node installation:
lsb_release -a
apt policy nodejs
node --version
npm --version
If lsb_release is unavailable, /etc/os-release contains release details. The apt policy output shows the candidate version available from your current sources; compare that with the version your project requires rather than relying on instructions written for another Ubuntu release.
Option 1: Install the Ubuntu packages
For a basic system installation, refresh APT’s package index and install Node.js and npm:
sudo apt update
sudo apt install nodejs npm
Then verify both commands are available:
node --version
npm --version
APT manages these packages system-wide, and their versions are tied to Ubuntu’s repositories. An older version than the latest upstream release doesn’t necessarily indicate a failed install. If you’re setting up a new Ubuntu machine, this fits naturally into the broader Ubuntu server setup and secure access checklist.
If the project’s required Node.js version is newer than APT’s candidate, don’t add a repository intended for a different Ubuntu release. Choose a method that provides the version you need.
Option 2: Use nvm for project-specific versions
nvm installs Node.js versions in your user environment and lets you switch between them. It’s a practical choice for development when different projects have different version requirements. Install and run it as your regular user—not with sudo.
After installing nvm using its current project instructions, install a version and select it:
nvm install 22
nvm use 22
node --version
npm --version
Here, 22 is only an example. Use the major version supported by your project. You can see versions managed by nvm and switch to another installed version like this:
nvm ls
nvm use 20
To select a default version for new shells, run nvm alias default 22, replacing 22 with the version you want. If a newly installed version isn’t available in the current terminal, open a new shell or reload the shell configuration that initializes nvm.
Know which installation your shell is using
APT and nvm manage Node.js differently. Installing both without a clear reason can leave you wondering why node --version doesn’t match the version you expected. Ask the shell which executable it finds:
command -v node
command -v npm
With nvm, the paths commonly point inside your home directory. An APT-installed binary is generally in a system location. Check the path and version together before making changes; this helps identify whether you need to switch nvm versions or investigate your package sources.
NodeSource is another option for administrators who want a selected Node.js release installed as a system-wide APT package. It adds a third-party package source, so confirm that the Ubuntu release and architecture are supported and follow the current instructions for the release you choose. Avoid mixing package sources without a specific reason.
Quick troubleshooting
node or npm is not found
For an APT installation, check whether the Node.js package is installed:
dpkg -s nodejs
If it isn’t installed, install the needed packages with APT. If you used nvm, make sure the shell initialized nvm and that you selected an installed version with nvm use. Use command -v node or command -v npm to check whether the command is on your current shell’s path.
The version doesn’t match the project
Check the selected executable and installed nvm versions before installing anything else:
command -v node
node --version
nvm ls
APT’s candidate depends on your configured Ubuntu package sources. With nvm, install or select the project’s required major version. Installing a second copy blindly can make it harder to tell which one your shell is using.
APT reports repository errors
Run sudo apt update and read the errors it prints. Check network access and verify that any third-party repository is configured for your Ubuntu release. Don’t add a repository for another release just to make an installation command succeed. If you’re diagnosing a broader package or installer failure, this Ubuntu installation troubleshooting guide can help narrow down the cause.
Which method should you pick?
Use Ubuntu APT when you want a simple package-managed installation and its available version meets your needs. Use nvm when you need to switch versions across projects or manage Node.js as your regular user. For a managed system-wide version through APT, consider a supported third-party repository and follow its current setup instructions.
After installing, verify with node --version, npm --version, and, if anything looks unexpected, command -v node. That small check can save you from debugging a project with the wrong Node.js runtime.
I originally published a more detailed version of this guide on the SSHFlow blog.
I'm also building SSHFlow — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.
Top comments (0)