How to add Node.js In Ubuntu

How to add Node.js In Ubuntu

How to Install Node.js on Ubuntu

Node.js is an open-source, cross-platform runtime environment that allows you to run JavaScript outside of a browser — commonly used for building fast, scalable server-side and networking applications.

This guide walks you through installing Node.js on Ubuntu 20.04, 22.04, or later.

Step 1: Update System Packages

Before installing anything, update your package index:

sudo apt update sudo apt upgrade -y

Step 2: Install Node.js Using APT (Default Ubuntu Repo)

Ubuntu includes Node.js in its default repositories. You can install it directly with:

sudo apt install -y nodejs

Then, check the installed version:

node -v

However, this version may be outdated. If you want the latest stable version, follow the next method using NodeSource.

Alternative: Install Latest Node.js Using NodeSource

NodeSource provides official up-to-date Node.js binaries for most distributions.

Step 2A: Install curl (if not already installed)

sudo apt install curl -y

Step 2B: Download the Node.js Setup Script

Choose your desired version, like 18.x (LTS) or 20.x (Current):

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

Step 2C: Install Node.js and npm

sudo apt-get install -y nodejs

Check versions to confirm:

node -v npm -v

npm (Node Package Manager) comes bundled with Node.js.

Step 3 (Optional): Install build-essential for Compiling Native Add-ons

Some npm packages require build tools:

sudo apt install -y build-essential

Step 4: Test Node.js

You can run a simple Node.js command to verify everything is working:

node

Then, in the Node.js REPL (interactive shell), type:

console.log("Hello from Node.js!");

You should see the output:

Hello from Node.js!

Type .exit to leave the REPL.

Optional: Uninstall Node.js

If you ever need to remove Node.js:

sudo apt remove nodejs sudo apt purge nodejs

Bonus: Install n – Node Version Manager (Alternative Method)

n allows you to manage multiple versions of Node.js.

Install it using npm:

sudo npm install -g n

Then install the latest Node version:

sudo n latest

Or the LTS version:

sudo n lts

To switch between installed versions, just use:

sudo n

Done!

You now have Node.js and npm installed on your Ubuntu system, and you're ready to build and run JavaScript applications on the backend!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close