Installing Node.js and NPM on Ubuntu 24.04 LTS Linux

Node.js and its package manager, NPM, can easily be installed on Ubuntu 24.04 or any other version. In this article, we learn how to do so.

Node.js is a platform for developing standalone JavaScript programs that run independently of host applications like web browsers. It can be used to program server-side scripts, network tools, and web apps. Node.js is based on Google’s JavaScript engine V8, also used in the Chrome web browser. V8 is a process-based virtual machine that uses a JIT compiler to translate the JavaScript code into the machine language of the underlying hardware at run time.

What do we need to perform this tutorial?

We need Ubuntu 24.04 or any previous version of it, such as Ubuntu 22.04 or 20.04. Along with it, we need a user account with sudo rights and an internet connection to download the required packages.

Method 1: Using Ubuntu 24.04’s Official Repository

The packages to install Node.js and NPM are already present through Ubuntu 24.04’s default system repository. However, the only long-term version you will have to install is 18.x. Therefore, those who have some other Node version can opt for the other two methods given in this tutorial.

Step 1: Update Package Index

Before installing packages on your Ubuntu, run the system update command, which updates the cache of the repository so that the users can have the latest available packages to install.

sudo apt update && sudo apt upgrade  

Step 2: Install Node.js and npm

After updating the package list, we can execute the given command using APT to install both Nodejs and NPM.

sudo apt install nodejs npm
Default Nodejs and NPM installation

Step 3: Confirm the Installation

As we are done with the previous step’s command, let’s confirm that both Node and NPM are on our system by verifying their versions.

nodejs -v  
or
node -v

npm -v
checking node versions

If the command Nodejs returns the version but not the one with “node,” then create a symlink:

sudo ln -s /usr/bin/nodejs /usr/bin/node

Now you can verify again with:

node -v

Method 2: Using the official NodeSource Repository

As we saw in the previous method, the version of Nodejs we will have using the default system repository of Ubuntu 24.04 is 18.x. So, to get a more recent version of Node.js, we can add the NodeSource repository manually, either for the latest LTS (Long Term Support) or current releases.

Step 1: Add NodeSource Repository

Now, use the given command as per your requirement. Those who want the latest Node version can add the repository for that.

Choose only one current or LTS:

For the latest current version:

This command will not install the LTS; it will be the latest current version.

curl -fsSL https://deb.nodesource.com/setup_current.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh

For the LTS version:

The following command will add the repository for the latest LTS version. After adding this repository, when we run the Nodejs installation command, whatever the latest LTS version is available will be installed on our Ubuntu system.

curl -fsSL https://deb.nodesource.com/setup_lts.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh

For some particular older or latest LTS versions:

Note: If you want a particular LTS version, mention that version in the given command. For example, we need 20.x, but if you need a different one, replace setup_20.x with the desired one.

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

Step 2: Install Node.js and npm

After adding the repository, we can use the APT package manager to install Node.js and NPM, like any other software.

sudo apt install nodejs

Step 3: Verifying the Installation

Check the installed versions of Node.js and npm:

node -v
npm -v

Method 3: Using NVM (Node Version Manager)

NVM is the best way to install Node.js and NVM because it can manage multiple Node versions and allows users to switch between them easily as per the project requirement. Hence, NVM can be pretty useful if you need to work on different projects requiring different Node.js versions.

Step 1: Installing NVM on Ubuntu 24.04

Unlike Nodejs, NVM is not available to install using Ubuntu’s default system repository. Therefore, execute the given curl command to run a script for installing NVM.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash

To apply the changes to your current terminal session, source your profile:

source ~/.bashrc

Step 2: Installing Node.js Using NVM

With NVM installed, you can now install Node.js. Installing the LTS version without specifying any version means you want the latest version of Node.js in LTS available to be installed. For that, the NVM command will be:

nvm install --lts

You can also install a specific version by specifying it:

nvm install 18.16.0

After completing the installation of Node.js using the NVM, check the version:

node -v
npm -v

Step 3: Use Specific Node.js Version

You can switch between installed Node.js versions by using the “use” option and the Nodejs version number in the NVM command:

nvm use 18.20.03

Step 4: Check other available versions

We can list all installed versions of Node.js with:

nvm ls

And list all available versions you can install with:

nvm ls-remote

Other Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.