Install Node.js on Ubuntu



Node.js is a well-known JavaScript runtime environment used to run JavaScript code on the server side. Using Node.js gives us the ability to execute code on the backend, extending JavaScript beyond just a traditional client side language. With this capability, you can build all kinds of applications that require a server to operate, such as an e-commerce application, a chat application, or even a social media app.

Node.js is one of the most popular backend technologies used by developers worldwide to build server applications. In this tutorial, we will go through different ways to install and run Node.js on Ubuntu.

Before proceeding with the installation, this guide assumes that you have an updated system and root access. Installing software requires root user privileges, as a normal user does not have the necessary permissions to add or remove packages from the system.

Install Node.js Using APT

Node.js is updated regularly, with the developers behind it making improvements and fixing bugs in each release. As a result, there are multiple Node.js versions with different support durations. In Ubuntu's repositories, you'll find the stable version of Node.js, but it may not be the latest LTS version.

The term LTS stands for "Long Term Support." Currently, the LTS version is v22.11.0, though this may change depending on when you read this guide.

If you're working on a project that requires the latest or a beta version, this may not be the best option.

You can check the repository to get information about the version of Node.js that will be installed before proceeding. Use the following command ?

apt show nodejs

This will display a bunch of information about the package, including the version. This can apply to any package that you want to install and need more information about before starting the installation.

The output will look like this ?


The above command shows a lot of information related to the package. To display just the version and the status of the package (whether it's installed on your machine or not), you can use the following command ?

apt-cache policy nodejs

These commands help confirm the available Node.js version in the system repository before starting the installation process.

The output will look like this ?


If you decide to use the version of Node.js available in the Ubuntu repository, it will include all the necessary packages to work with Node.js, though it may not be the latest version. This method offers an easy installation with a single command.

Before you start the installation, first update your system ?

sudo apt update

Then, the only command you need to install Node.js is ?

sudo apt install nodejs

This will install Node.js along with the popular Node Package Manager (NPM), which is the tool you'll use to install packages and tools in the JavaScript ecosystem.

After the installation is complete, you can check the version of Node.js and NPM that was installed by using the commands ?

node -v
npm -v

This will display the versions, which may look like this ?

18.19.1
9.2.0

If these versions meet your needs, then you have successfully installed Node.js and NPM on your system.

Using NodeSource Repository

NodeSource is a company that provides the binaries for Node.js, which can be used to get the latest version from a secure and trusted source.

NodeSource has long been known as a maintainer of Node.js binaries, providing a secure and production ready environment.

To begin the installation of the latest LTS version of Node.js, use the following command to add the NodeSource repository to your system ?

(Note: Using NodeSource, you can install a specific version depending on your needs.)

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

This command uses curl. If you don't have curl installed on your machine, install it first with the command ?

sudo apt install curl

Here, we are adding the latest LTS version, which at this moment is version 22. If you want the latest version, it is currently v23.1.0, which was released in October.

The next step after adding the NodeSource repository is to update the system so the repository will be recognized. Use the following update command ?

sudo apt update

After adding the repository, you should be ready to proceed with the installation using the following command ?

sudo apt install -y nodejs

This will download and install the version added from NodeSource.

After the installation is complete, you can check the installed version ?

node -v

Install a Specific Node.js Version

If you are working on a project that requires a specific Node.js version, or you are maintaining legacy code that needs an older version, NodeSource offers nearly all Node.js binaries for different versions. All you need to do is specify which version you need and then add the NodeSource repository for it.

For example, to install the latest version at this time, which is v23.1.0 ?

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

This will add the latest version (23). Next, update the system ?

sudo apt update

Finally, install the version using the command ?

sudo apt install -y nodejs

Note ? To use the NodeSource repository, you need to have Ubuntu version 20.04 or higher. If you are using Ubuntu version 18 or lower, first update your system and then start using the NodeSource repository. For Debian users, the supported versions are 10 or higher.

Install Multiple Node.js Versions

Sometimes, we need multiple versions of Node.js on our machine to switch between, depending on the project we are working on. You may also want to test an application in a different version to check for bugs or errors. Luckily, there are tools that allow us to install multiple versions and choose which one to use.

Using NVM

NVM (Node Version Manager) is a project that makes working with Node.js easier and more efficient, as it allows us to install and maintain multiple Node.js versions on the system. Using apt and NodeSource, we can install and work with one version of Node.js, but if we need another version, we would typically have to remove or update the installed version. NVM solves this problem by allowing multiple versions to be installed and giving more control over the development environment.

To install NVM on your system, run the following command to add the corresponding repository:

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

This command will download the installer script for NVM. The version may change depending on when you see this tutorial?refer to the official repository on GitHub to get the latest version.

After the installation is complete, load NVM to your bash session with the command ?

source ~/.bashrc

You can check the installed version of NVM with the command ?

nvm -version

To install the latest LTS Node.js version using NVM, simply run ?

nvm install -lts

This will install the latest available version.

You can get a list of available versions with ?

nvm ls-remote

This command will fetch and print all available versions of Node.js that can be installed using NVM.

If you want to install a specific version, for example, version 23.1.0, use the following ?

nvm install v23.1.0

This applies to other versions if you want multiple versions installed. To switch between versions, use the following command and specify the version you need ?

nvm use v22.1.0

Install NodeJs Using ASDF

ASDF is a tool that allows us to install and manage different versions of almost any programming language, not just Node.js. It can be installed on all Linux distributions. Take a look at this article where we discuss what ASDF is and how to install it if this is your first time hearing about this tool.

You can install ASDF using the following commands ?

git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.12.0
echo '. $HOME/.asdf/asdf.sh' >> ~/.bashrc
source ~/.bashrc

To install Node.js using ASDF, use the command ?

asdf plugin-add nodejs

The ASDF tool uses the term "plugin" to manage languages and tools. After adding the Node.js plugin, you can install any version you want. For example, to install the latest version, use the command ?

asdf install nodejs latest

This should install the latest version of Node.js.

Conclusion

This tutorial covered almost all the steps and methods we can use to install Node.js. Choose the right approach based on your situation and needs. In the end, all the methods lead to the same goal: getting Node.js ready to use on your Ubuntu distribution or any other Ubuntu based Linux distro.

Updated on: 2024-11-19T18:04:24+05:30

146 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements