As of writing this article, Node.js 20 is the latest LTS release, which is best suited for production servers. Alternatively, you can utilize the latest features with Node.js version 22. There are several ways to install Node.js on any system; however, we will focus on two methods. The first method uses NVM (Node Version Manager), which is the best and quickest way to install and manage multiple Node.js versions. The second method uses the official package repository to install Node.js on Ubuntu, allowing the installation of only one version at a time. This is ideal for production servers where applications run on the same version.
Prerequisites
- A running Ubuntu 24.04 system
- Basic knowledge of working with command-line interface
- Shell access with sudo privilege’s (required for installing from PPA)
Step 1: Update System Packages
First of all, its good practice to update current system packages. From a terminal, run following command:
sudo apt update
sudo apt upgrade
The next step is divided into two parts (A and B). Choose one based on installation method you preferred.
Step 2 (A): Installing Node.js on Ubuntu using NVM
NVM is a quick and widely accepted option for installing Node.js on Linux systems. It allows you to manage multiple Node.js versions on a single machine and creates separate installations for each user, preventing conflicts between users on shared machines.
- First, ensure NVM is installed on your system. To install NVM, use the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
This command downloads and runs the NVM installation script.
- Activate the NVM environment for the current user by sourcing your bash profile:
source ~/.bashrc
- You can now install the required Node.js versions on your system. To install a specific version, use NVM:
nvm install 20
Replace “20” with the version number of Node.js you want to install, like 22, 18, 16, 14, or any other version.
- Verify the installation by running:
node --version
This command will display the currently installed version of Node.js.
Step 2 (B): Installing Node.js using Official Repository
You can also use the official Node.js packages from Nodesource, which are available for Debian-based systems. However, this method only supports the installation of Node.js on currently supported Ubuntu versions; it does not support end-of-life (EOL) systems, such as Ubuntu 18.04 LTS and older.
- Configure the Node.js Debian repository on your system for the required version:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
Replace “20” in the above command with the Node.js version you need, such as 22, 21, 18, or 16. This command sets up the Debian repository on your system.
- Install Node.js on your system by running the following command:
sudo apt-get install -y nodejs
- Once the installation is finished, check the installed Node.js version using the
node -v
command.
Step 3: Create Sample Application (Optional)
If you want to test your Node.js installation, creating a sample application is a great way to start. Here’s how to create a simple “Hello World” application:
- Create a new directory for your project and navigate into it:
mkdir myapp && cd myapp
- Next, create a new JavaScript file named app.js:
echo "console.log('Hello World');" > app.js
- Run your application using Node.js:
node app.js
This command will print “Hello World” to the console, confirming that Node.js is working on your system.
Step 4: Upgrade Node.js (Optional)
If you need to upgrade to a newer version of Node.js, you can do so easily with NVM. First, check the available versions:
nvm ls-remote
Then, install a newer version:
nvm install [version]
Replace [version] with the version number you wish to install. After installing, you can switch to the new version using:
nvm use [version]
Step 5: Remove Node.js (Optional)
If you decide to remove Node.js from your system, you can uninstall it using NVM. To remove a specific version:
nvm uninstall [version]
Replace [version] with the version of Node.js that you want to remove.
Conclusion
This guide has walked you through methods to install Node.js using both NVM and the Nodesource Debian repository. Each method provides its own advantages: NVM offers flexibility in managing multiple versions of Node.js on a single machine, while the Nodesource repository is ideal for users who prefer a straightforward installation of specific versions directly from the Debian package manager.