In this Linux Ubuntu tutorial, we explain how to install and run Node.js and NPM on Linux Ubuntu. We also explain how to write a Hello World Node.js program that tests the installation of Node.js.
First of all, let us explain what is Node.js. Node.js is a cross-platform open-source JavaScript runtime environment. Node.js is attractive since it enables us to execute JavaScript code outside of a web browser. Node.js can be used to create servers, web apps, command line tools and scripts.
On the other hand, Node Package Manager (NPM) is a code repository and an application manager for sharing and developing JavaScript code and programs. NPM enables us to manage Node.js libraries and dependencies.
The YouTube tutorial explaining all the installation steps is given below.
How to Install and Run Node.js on Linux Ubuntu
First, you need to update and upgrade the packages and install Curl:
sudo apt update && sudo apt upgrade
sudo apt install curl
curl –version
In this tutorial, we install Node.js version 22.14 LTS. If you want to install some other version, then you can easily modify the instructions given below (this is explained later on in the tutorial). To install Node.js, you need to type the following commands
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
\. "$HOME/.nvm/nvm.sh"
nvm install 22
This will install version 22. However, if you want to install some other version, you need to change the last number in the command
nvm install 22
to the version you want to install. Next, we need to verify the version of Node.js. To do that, type any of the following two commands:
node -v
nvm current
This should print the current Node.js version. Finally, let us verify the NPM installation:
npm -v
This should return the current NPM installation. Further installation instructions (also instructions how to install other Node.js versions) are given on this website:
https://nodejs.org/en/download
How to Write Hello World Node.js Example
First, open a terminal, and create a workspace folder
cd ~
mkdir testNode
cd testNode
Next, we need to initialize the Node project
npm init -y
ls -la

This creates the configuration file called package.json. Next, we need to create our first Node.js file. To do that, create the file called test.js
sudo nano test.js
Type the following line
console.log("Hello World from Node.js");

Save the file by typing CTRL+O, and then, exit by typing CTRL+X. To run the file type the following
node test.js

The output is shown in the figure above.