Table of Contents
Introduction
Node.js is free and an open-source cross-platform JavaScript run-time environment that allows server-side execution of JavaScript code. It is used in developing web and networking applications. NPM(Node Package Manager) is command line tool for Node.js packages that installs, updates and uninstall packages in your projects.We don’t have install npm separately it is includes with Node.js installation.Prerequisites
- Ubuntu 20.04 LTS
- SSH access with sudo privileges
- Firewall Port: 3000
sudo apt-get update sudo apt-get upgrade
Step #1: Add Node.js and npm from NodeSource Repository
Before installing, We have to install curl and add Node.js from Nodesource repository.sudo apt-get install curl
Current Release
Node.js 14.10.1 is the current release.curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
Latest Stable Release
In this article , We are installing Node.js 12.18.3 latest stable version.curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
Sample Output:
## Installing the NodeSource Node.js 12.x repo... ## Populating apt-get cache... + apt-get update Ign:1 https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 InRelease Hit:2 https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 Release Hit:3 https://deb.nodesource.com/node_12.x bionic InRelease Hit:5 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic InRelease Get:6 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB] Get:7 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB] Get:8 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB] Fetched 252 kB in 1s (221 kB/s) Reading package lists... Done ## Confirming "bionic" is supported... + curl -sLf -o /dev/null 'https://deb.nodesource.com/node_12.x/dists/bionic/Release' ## Adding the NodeSource signing key to your keyring... + curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add - OK ## Creating apt sources list file for the NodeSource Node.js 12.x repo... + echo 'deb https://deb.nodesource.com/node_12.x bionic main' > /etc/apt/sources.list.d/nodesource.list + echo 'deb-src https://deb.nodesource.com/node_12.x bionic main' >> /etc/apt/sources.list.d/nodesource.list ## Running `apt-get update` for you... + apt-get update Ign:1 https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 InRelease Hit:2 https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 Release Hit:4 https://deb.nodesource.com/node_12.x bionic InRelease Hit:5 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic InRelease Hit:6 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease Hit:7 http://security.ubuntu.com/ubuntu bionic-security InRelease Hit:8 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease Reading package lists... Done ## Run `sudo apt-get install -y nodejs` to install Node.js 12.x and npm ## You may also need development tools to build native addons: sudo apt-get install gcc g++ make ## To install the Yarn package manager, run: 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
Step #2: How to Install Node.js and NPM on Ubuntu 20.04 LTS
Once repository added in ubuntu,enter below command to install Node.js and NPM.sudo apt-get install -y nodejs
Step #3: Check Node.js and NPM Version on Ubuntu
After installation, check Node.js version by entering below command,node --versionOutput:
$ node --version v12.18.3To Check NPM version.
npm --versionOutput:
$ npm --version 6.14.6
Step #4: Testing Node.js Server
Create a javascript file with name nodeapp.js, add below code which prints ” Hello World”.
sudo nano nodeapp.jsPaste the below lines into it.
const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
node nodeapp.jsOutput:
$ node nodeapp.js Server running at http://127.0.0.1:3000/
http://127.0.0.1:3000We have successfully covered, How to Install Node.js and NPM on Ubuntu
Installing Node.js and NPM using NVM
Download the NVM installation script from NVM GitHUB Page using curl.curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh -o install_nvm.shonce downloaded, run the bash script
bash install_nvm.sh
It will install your system’s home directory ~/.nvm and adds configuration in ~/.profile
Load the profile
source ~/.profileTo check all available node.js versions
nvm ls-remote
Sample output:
..... v12.14.0 (LTS: Erbium) v12.14.1 (LTS: Erbium) v12.15.0 (LTS: Erbium) v12.16.0 (LTS: Erbium) v12.16.1 (LTS: Erbium) v12.16.2 (LTS: Erbium) v12.16.3 (LTS: Erbium) v12.17.0 (LTS: Erbium) v12.18.0 (LTS: Erbium) v12.18.1 (LTS: Erbium) v12.18.2 (LTS: Erbium) v12.18.3 (Latest LTS: Erbium) v13.0.0 v13.0.1 v13.1.0 v13.2.0 v13.3.0 v13.4.0 v13.5.0 v13.6.0 v13.7.0 v13.8.0 v13.9.0 v13.10.0 v13.10.1 v13.11.0 v13.12.0 v13.13.0 v13.14.0 v14.0.0 v14.1.0 v14.2.0 v14.3.0 v14.4.0 v14.5.0 v14.6.0 v14.7.0 v14.8.0 v14.9.0 v14.10.0 v14.10.1Error: npm update check failed try running with sudo or get access change the permission to install node.js
sudo chown -R $USER:$(id -gn $USER) /home/ubuntu/.configNow you can install node.js version as you want.
nvm install 12.18.3
To set recently node.js version
nvm use 12.18.3chaeck the node varsion
node -vcheck the nvm version
nvm --versionWe have performed ,How to Install Node.js and NPM on Ubuntu 20.04 LTS.
Uninstall Node.js and NPM in Ubuntu
If we want to uninstall/remove node.js and npm from ubuntu, Enter Y to proceed uninstallation.$ sudo apt remove nodejs npmOutput:
$ sudo apt remove nodejs npm Reading package lists... Done Building dependency tree Reading state information... Done Package 'npm' is not installed, so not removed The following packages will be REMOVED: nodejs 0 upgraded, 0 newly installed, 1 to remove and 84 not upgraded. After this operation, 76.5 MB disk space will be freed. Do you want to continue? [Y/n] y (Reading database ... 143850 files and directories currently installed.) Removing nodejs (12.16.3-1nodesource1) ... Processing triggers for man-db (2.7.5-1) ...Conclusion: In this article, We have performed ,How to Install Node.js and NPM on Ubuntu 20.04 LTS, Checked Node.js and NPM version and performed to uninstall node.js and NPM from ubuntu. Related Articles: How to Install Latest Node.js and NPM on Ubuntu 19.04,18.04/16.04 LTS How to Install Angular CLI on Ubuntu 18.04/16.04 LTS How to Install node.js on Mac OS How to Install Node.js on Windows 10