Install Node.js In Ubuntu and Debian

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. It allows developers to run JavaScript on the server-side, enabling them to create fast and scalable applications. In this article, we will cover how to install Node.js in Ubuntu, one of the most popular Linux distributions.

Why Node.js?

One of the benefits of using Node.js is its event-driven, non-blocking I/O model, which makes it well-suited for building real-time applications such as chat applications, games, and streaming services. It also has a large and active community that provides a vast range of modules, libraries, and frameworks that can be easily integrated into Node.js applications.

NVM (Node Version Manager) is a command-line tool that allows developers to manage multiple Node.js versions on a single machine. With NVM, developers can easily switch between different versions of Node.js without having to uninstall and reinstall each time. This is useful because different applications or projects may require different versions of Node.js.

NPM (Node Package Manager) is a package manager for Node.js that allows developers to easily manage and install third-party packages, modules, and dependencies. It comes bundled with Node.js, so installing it separately is unnecessary. NPM also has a large and active community that maintains a vast repository of packages that can be easily installed and used in Node.js applications.

How to Install Node.js in Ubuntu and Debian?

When writing this article, 16.13.0 is the latest LTS (Long-term Support) version of Node.js, and 17.1.0 is the latest release of Node.js.

For newcomers, 17.1.0 is a good place to start because it contains all of the most recent features released by the developers. It is strongly advised to develop your app in the LTS version for production because it will receive all security and maintenance updates for an extended period.

Install Node.js LTS version

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

Install Node.js latest version

curl -fsSL https://deb.nodesource.com/setup_xx.x | sudo -E bash -
Add Node.js repository
sudo apt-get install -y nodejs
Install Node.js in Ubuntu

You can use these instructions on any Ubuntu-based Linux distribution. As you can see, I am using Pop!_OS for this article. If you are installing Node.js on a minimal version of Ubuntu that does not have curl and sudo setup, you can install curl and sudo using the following command –

apt install curl sudo

You can install it manually if you want to use something other than the above method.

Installing Node.js manually in Ubuntu

KEYRING=/usr/share/keyrings/nodesource.gpg
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor | sudo tee "$KEYRING" >/dev/null

Now open the terminal, customize and pass the following commands one-by-one. Or you can also write a shell script and run it.

Replace the value of VERSION with the Node.js version you want to install. If your distribution lacks the lsb_release command, you can manually assign the DISTRO value or install lsb_release using this article.

VERSION=node_8.x
KEYRING=/usr/share/keyrings/nodesource
DISTRO="$(lsb_release -s -c)"

echo "deb [signed-by=$KEYRING] https://deb.nodesource.com/$VERSION $DISTRO main" | sudo tee /etc/apt/sources.list.d/nodesource.list

echo "deb-src [signed-by=$KEYRING] https://deb.nodesource.com/$VERSION $DISTRO main" | sudo tee -a /etc/apt/sources.list.d/nodesource.list

Update the system and install Node.js –

sudo apt-get update
sudo apt-get install nodejs