Hostomy Blog

How to Install Node.js on Ubuntu: A Step-by-Step Guide

Nov 12, 2024

By Rahul Mukati

Rahul Mukati
How to Install Node.js on Ubuntu: A Step-by-Step Guide

Node.js is a powerful JavaScript runtime built on Chrome’s V8 JavaScript engine. It is widely used for building server-side and networking applications. In this tutorial, we’ll walk you through the steps of installing Node.js on an Ubuntu system. Whether you're starting a new project or want to manage existing Node.js applications, this guide will help you get up and running in no time.

Prerequisites

  • A machine running Ubuntu (this guide assumes you are using Ubuntu 20.04 LTS or later).
  • A terminal with sudo privileges.

Step 1: Update Your Package List

Before you begin installing any packages, it’s a good idea to update the local package index. This ensures you’re installing the latest software from the Ubuntu repositories.

Open your terminal and run the following command:

sudo apt update

Step 2: Install Dependencies

Node.js requires a few packages to be installed beforehand. These dependencies include curl and software-properties-common for adding external repositories.

To install these dependencies, run:

sudo apt install curl software-properties-common

Step 3: Add NodeSource Repository

Node.js is not always available in the default Ubuntu repositories. To ensure you get the latest stable version, you can add the NodeSource repository.

First, add the repository for Node.js. For the latest LTS version of Node.js, use the following command:

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

Alternatively, for the latest version of Node.js, replace setup_lts.x with setup_20.x (or the latest version number).

Step 4: Install Node.js

Once the repository is added, it’s time to install Node.js. You can install it using the following command:

sudo apt install nodejs

After installation, you can verify that Node.js was installed correctly by checking its version:

node -v

You should see the version of Node.js you installed (e.g., v22.11.0)

Step 5: Install npm (if not installed)

npm, the Node.js package manager, is used to manage and install Node.js libraries. It is usually installed alongside Node.js, but in case it’s not, you can install it manually.

sudo apt install npm

Verify the npm installation:

npm -v

This will display the version of npm that has been installed.

Conclusion

Node.js is now installed and ready to use on your Ubuntu system! You can now start building server-side JavaScript applications or use Node.js as a runtime for your web projects. With the latest LTS version installed, you can be confident in its stability and security.

Feel free to explore the world of Node.js and take advantage of its powerful libraries and frameworks, such as Express.js, to simplify your development process.

Looking to host a NodeJs application on a server? Check out How to Host Your NodeJS App on Ubuntu

Happy coding!