How to Upgrade Node.js on Ubuntu 24.04

Node.js is a free, cross-platform runtime environment that allows JavaScript to run on the server side. By installing Node.js on Ubuntu, you gain access to its powerful ecosystem, including the Node Package Manager (npm), the official registry for JavaScript packages. It is widely used in both front-end and back-end development, supported by a strong developer community and extensive documentation—making it a fundamental tool in modern application development.

This guide explains how to upgrade Node.js on Ubuntu 24.04 using three different methods: NVM (Node Version Manager), official binary distributions, and the APT package manager. It also covers how to check the installed version before and after upgrading.

Prerequisites

Before you start, ensure you have the following in place:

  • An Ubuntu 24.04 machine with Node.js already installed.
  • SSH access to the server.
  • A non-root user account with sudo privileges.
  • Access to the NodeSource version chart to verify supported Node.js versions for Ubuntu 24.04.

Check the Installed Node.js Version

Before upgrading, verify the version currently installed. This helps confirm if the upgrade has been applied successfully.

Output:

v18.19.1

If you get a “command not found” error, Node.js isn’t installed. In that case, follow one of the upgrade methods below to install your required version.

Note: It’s recommended to compare your installed version with the latest LTS release listed on the official Node.js website to decide if an update is needed.

Install Node Version Manager (NVM)

Node Version Manager (NVM) is a CLI tool that allows you to install, manage, and switch between multiple Node.js versions on the same system. It makes upgrading easier and lets you define a default version for your shell sessions. Follow the steps below to set up NVM.

Download and run the NVM installation script:

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash

Reload your shell configuration file to activate NVM:

If you use Zsh, replace the above command with:

Confirm that NVM was installed correctly:

Output:

0.40.2

Upgrade Node.js on Ubuntu 24.04

You can upgrade Node.js on Ubuntu 24.04 using NVM, binary packages, or the APT package manager. Choose the method that fits your setup best.

Warning: Use only one upgrade method. Mixing NVM, binary downloads, and APT may lead to conflicts. If you change your method, make sure to fully remove previously installed versions of Node.js.

NVM is the preferred method as it supports installing the latest LTS or a specific version, giving developers flexibility when working with different projects.

Install the Latest LTS Version

The Long-Term Support (LTS) version is the most stable and secure release, recommended for production use. Here’s how to install and set the latest LTS version using NVM:

List all available Node.js versions:

Output:


v20.12.2 (LTS: Hydrogen)
v22.14.0 (LTS: Iron)
v23.0.0

Install the most recent LTS version:

Output:

Installing latest LTS version.
Downloading and installing node v22.14.0…
Now using node v22.14.0 (npm v10.6.0)

Set the installed version as the default for all sessions:

Output:

default -> node (-> v22.14.0)

For production, always prefer an LTS version to ensure stability and long-term support.

Install a Specific Version

If your project depends on a particular Node.js release, you can install that version instead.

List available versions:

Output:


v18.19.1 (LTS: Hydrogen)
v20.12.2 (LTS: Iron)
v22.1.0

Install a specific version by replacing <version> with the version number (e.g., 20.12.2):

Set the installed version as the default:

$ nvm alias default <version>

Note: When switching Node.js versions, global npm packages installed under the old version won’t transfer. You’ll need to reinstall them with npm install -g <package>.

Upgrade Node.js Using Binary Packages

The official Node.js website provides precompiled binaries for Linux systems. This method is useful if you require a specific version that is not available via package managers like APT or NVM.

Download the Node.js binary package, replacing <version> with the desired release number. You can find all available versions on the official Node.js downloads page.

$ wget https://nodejs.org/dist/v<version>/node-v<version>-linux-x64.tar.xz

Extract the archive to /usr/local/ to install Node.js system-wide:

$ sudo tar -C /usr/local --strip-components 1 -xJf node-v<version>-linux-x64.tar.xz

Add /usr/local/bin to the beginning of your PATH so the system prioritizes the new version:

$ export PATH=/usr/local/bin:$PATH

Make the PATH modification permanent by appending it to your shell configuration file:

$ echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bashrc

Reload your shell configuration to apply the changes:

If you use Zsh, replace ~/.bashrc with ~/.zshrc in the last two steps.

Upgrade Node.js Using APT

APT is Ubuntu’s built-in package manager. This method installs or upgrades Node.js by enabling the official NodeSource repository. It’s ideal for environments managed system-wide or automated deployments.

Download and execute the NodeSource setup script. Replace <version> with the major version number followed by .x (e.g., 20.x or 22.x). Supported versions can be found on the NodeSource distributions page.

$ curl -fsSL https://deb.nodesource.com/setup_<version> | sudo -E bash -

Update the package index:

Install Node.js:

$ sudo apt install -y nodejs

Verify that npm was installed alongside Node.js:

Output:

10.5.0

The Node.js package from NodeSource includes npm by default, so no separate installation is required.

Verify the Active Node.js Version

After completing the upgrade, check that your system is using the correct Node.js and npm versions. This confirms that the installation method worked properly and your environment is ready for development or deployment.

Check the active Node.js version:

Output:

v22.16.0

Check the npm version bundled with Node.js:

Output:

10.9.2

If the displayed versions match the ones you installed, the upgrade was successful and your environment is correctly configured.

Conclusion

In this tutorial, you upgraded Node.js on Ubuntu 24.04 using one of three supported methods: NVM, binary packages, or the APT package manager. You confirmed the upgrade by checking the active Node.js and npm versions. Depending on your workflow, each method offers flexibility for development, manual control, or automated deployments. For advanced configuration, refer to the official Node.js documentation.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: