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-get update sudo apt-get -y 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 None LTS repository
If you want to go with the latest upstream version, add APT for that version. E.g for Node.js 11.x
. It will be added like this.
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
This is most fit for Development on your Local machine.
Add Node.js LTS repository
For production use, I recommend you do the installation of the latest LTS release. As of this article update, this is Node.js 10.x
.
curl -sL https://deb.nodesource.com/setup_10.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-get -y install nodejs
You can also development tools to build native addons:
sudo apt-get install gcc g++ make
Confirm versions.
$ node --version v11.13.0 $ npm --version 6.7.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-get update && sudo apt-get install yarn
Confirm installation.
$ yarn --version 1.15.2
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
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN node-demo@1.0.0 No description
npm WARN node-demo@1.0.0 No repository field.
+ express@4.16.4
added 48 packages from 36 contributors and audited 121 packages in 2.555s
found 0 vulnerabilities
Add start script to our packge.json
file
"start": "node index.js"
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.