Welcome to our guide on how to install Latest Node.js and NPM on Ubuntu & Debian Linux distributions. Node.js is a free and open source server-side programming language which runs on various platforms (Linux, Windows, Unix, macOS). Node.js is a JavaScript runtime built on Chrome’s V8 engine for building fast and scalable network applications.
Step 1: Add Node.js APT Repository
The most recent packages of Node.js are available on a APT repository. First, update your system and install some dependencies.
sudo apt update
sudo apt install curl dirmngr apt-transport-https lsb-release ca-certificates vim
If you want to know the latest release, check Node.js Releases page
Add Node.js LTS repository
If you want to go with the LTS versions, add APT for that version. E.g for Node.js 16.x
. It will be added like this (even numbers);
### Node.js 18 LTS ###
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
### Node.js 16 LTS ###
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
### Node.js 14 LTS ###
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
Step 2: Install Latest Node.js on Ubuntu / Debian
After adding the repository, proceed to install Node.js and NPM.
sudo apt install nodejs
You can also development tools to build native addons:
sudo apt install gcc g++ make
Confirm versions.
$ node --version
v18.2.0
$ npm --version
8.9.0
Step 3: Install Yarn package manager ( Bonus and Optional)
If you need yarn package manager, install it by running:
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt install yarn
Confirm installation.
$ yarn --version
1.22.19
Step 4: Test Node.js
Let’s create a simple Nodejs app as a test that Node.js is working.
mkdir /tmp/node-demo
cd /tmp/node-demo
initialize nodejs project with default package.json file:
$ npm init -y
Wrote to /tmp/node-demo/package.json:
{
"name": "node-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Create an index.js
vim index.js
Add:
const express = require('express');
const PORT = 8080;
const HOST = '0.0.0.0';
const app = express();
app.get('/', (req, res) => {
res.send('Hello Node.js World\n');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
Install express package with npm command:
$ npm install -save express
added 57 packages, and audited 58 packages in 2s
7 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
npm notice
npm notice New minor version of npm available! 8.9.0 -> 8.11.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.11.0
npm notice Run npm install -g [email protected] to update!
npm notice
Add start script to our package.json
file
$ vim package.json
{
"name": "demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.1"
}
}
You can now run Nodejs application:
$ node index.js
Running on http://0.0.0.0:8080
##OR
$ npm start
Running on http://0.0.0.0:8080
The application can be started with debugging enabled using:
$ node --inspect index.js
Debugger listening on ws://127.0.0.1:9229/49cd62a8-88e0-4b21-b898-f79a79e9d5dc
For help, see: https://nodejs.org/en/docs/inspector
Running on http://0.0.0.0:8080
If you visit your Server IP on port 8080
, you should see Node.js application output.
There you go!. Node.js has been successful installed on Ubuntu / Debian Linux distribution. Have a happy Node Development.
Related: