How ToLinuxProgramming

How to Install Node.js on Ubuntu

Node.js is a powerful and widely used JavaScript runtime environment that allows developers to build scalable web applications. This guide will walk you through the process of installing Node.js on Ubuntu using various methods, ensuring you have the latest version and optimal configuration.

Prerequisites

Before proceeding, ensure you have the following:

  • A system running Ubuntu 20.04, Ubuntu 22.04, or later versions.
  • A user account with sudo privileges.
  • An active internet connection.

Method 1: Installing Node.js Using Ubuntu’s Default Repositories

Ubuntu’s default repository often includes Node.js. While this version may not be the latest, it’s stable and secure.

Step 1: Update Package Index

Run the following command to update the package index before installation:

sudo apt update

Step 2: Install Node.js

To install Node.js, run:

sudo apt install nodejs

Step 3: Verify Installation

Confirm Node.js is installed by checking its version:

node -v

Note: The version might not be the latest. If you need the latest version, proceed with Method 2 or 3.

Step 4: Install npm

For package management with Node.js, install npm (Node Package Manager):

sudo apt install npm

Verify the npm installation with:

npm -v

Method 2: Installing Node.js Using NodeSource PPA (Recommended for Latest Versions)

For the latest stable release of Node.js, use NodeSource PPA.

Step 1: Install NodeSource Repository

Add the NodeSource PPA by running the following command:

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

Step 2: Install Node.js

Install Node.js (with npm included) using this command:

sudo apt install nodejs

Step 3: Verify Installation

Check the installed versions:

node -v
npm -v

Step 4: Install build-essential for npm Packages (Optional but Recommended)

Some npm packages require compilation. Install build-essential for smooth functionality:

sudo apt install build-essential

Method 3: Installing Node.js Using NVM (Node Version Manager)

NVM is the best method for installing and managing multiple Node.js versions efficiently.

Step 1: Install NVM

Run the following command to download and install NVM:

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

Step 2: Activate NVM

Run this command to load NVM in the current session:

source ~/.bashrc

Step 3: Install Node.js Using NVM

To install the latest LTS version of Node.js:

nvm install --lts

To install a specific version:

nvm install <version_number>

For example:

nvm install 18.17.1

Step 4: Verify Installation

To confirm Node.js is installed:

node -v
npm -v

Step 5: Switching Node.js Versions with NVM

To list installed versions:

nvm list

To switch between versions:

nvm use <version_number>

For example:

nvm use 18.17.1

To set a default version:

nvm alias default 18.17.1

Method 4: Installing Node.js via Snap (Quick and Simple)

Snaps offer a quick installation method with automatic updates.

Step 1: Install Node.js Snap Package

Run the following command:

sudo snap install node --classic

Step 2: Verify Installation

Check the Node.js version:

node -v

Method 5: Building Node.js from Source (Advanced Users)

This method is suitable for developers who want to build Node.js from source for specific configurations.

Step 1: Install Dependencies

sudo apt install python3 g++ make

Step 2: Download Node.js Source Code

cd /usr/local/src
sudo wget https://nodejs.org/dist/v20.0.0/node-v20.0.0.tar.gz

Step 3: Extract the Archive

tar -xvf node-v20.0.0.tar.gz
cd node-v20.0.0

Step 4: Compile and Install Node.js

sudo ./configure
sudo make -j4
sudo make install

Step 5: Verify Installation

node -v

Post-Installation Tips

1. Check Node.js Path

To ensure Node.js is correctly added to your path:

which node

2. Update Node.js

To update Node.js installed via NodeSource:

sudo apt update && sudo apt upgrade

To update Node.js via NVM:

nvm install --lts

3. Uninstall Node.js

To uninstall Node.js installed via APT:

sudo apt remove nodejs

To remove Node.js via NVM:

nvm uninstall <version_number>

Conclusion

Node.js is a powerful runtime environment ideal for building scalable web applications. By following this guide, you can successfully install Node.js on your Ubuntu system using multiple methods. For most users, NodeSource or NVM are the best approaches for managing Node.js efficiently.

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

10 thoughts on “How to Install Node.js on Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *