How to Install NPM & Node js on Ubuntu 19.04

How to Install NPM & Node js on Ubuntu 19.04

To install Node.js and NPM on latest Ubuntu 19.04, we don’t need to add any kind PPA (personal package manager), however, there is an official PPA for Node.js that we can use to install latest versions of Nodejs & NPM on Ubuntu such as 19.04, 18.04 LTS, 16.04 LTS (Trusty Tahr) and 14.04 LTS (Xenial Xerus) Linux servers.

Node.js on Ubuntu gives a way to write server-side scripting. It is an open source Javascript cross-platform that executes the Javascript outside the browsers. It uses an NPM package manager to fetch different packages and modules for function extensions of the app developed on Node.js. To install Node.js and NPM on latest Ubuntu 19.04, we don’t need to add any kind PPA (personal package manager), however, there is an official PPA for Node.js that we can use to install latest versions of Nodejs & NPM on Ubuntu such as 19.04, 18.04 LTS, 16.04 LTS (Trusty Tahr) and 14.04 LTS (Xenial Xerus) Linux servers.

Install NPM & Node js on Ubuntu 19.04

Step 1: Offical PPA repository for Node.js

Open the command terminal on Ubuntu Linux and add the official repository for downloading and installing the NPM and Node.js. The Long term stable release of Node.js is 10.15.3 LTS and a current stable release is 11.14.0. By default, the user will get the long term stable version 10.15.3 on Ubuntu 19.04, however, we can install the current version using Node.js PPA.

For long term version, we don’t need to add the repo, just type the commands:

sudo apt-get install nodejs
Sudo apt-get install npm

Note: Without adding latest PPA manually, the user will get Node.js LTS 10.x version from Ubuntu 19.04 repo. So to get a current 11.X release, add the below official repo.

For latest Node.js version 11.x use this PPA:


sudo apt install curl
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -

For 10.X version Node.js use this repo.

sudo apt install curl
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -

Note: As we are using 11.x repo on Ubuntu 19.04, here.

Step 2: Update your system.

After adding the Node.js Repo run the system update command:


sudo apt-get update


Step 3: Command to install the Node.js on Ubuntu 19.04

Note: Without PPA we have to install Node and NPM using two different commands, however, those are using the PPA will get both Nodejs and NPM using single below command:

sudo apt-get install nodejs

Step 4 Install NPM on Ubuntu 190.4 via command

After installing the Node.js, use this command to set up the NPM package manager on Ubuntu 19.04


sudo apt-get install npm


Step 5: Check the installed Node and NPM version on Ubuntu

Versions on Ubuntu 19.04 without using PPA- LTS versions

To check the Node version on Ubuntu, the command is:


node --version


To check the NPM version, the command is:

npm -v 


After using the Latest Node.js PPA repo, the latest version–

Step 6: Command to upgrade NVM and Node (optional)

Note: If you are using the PPA, simply run sudo apt update

First, update npm,

sudo npm update


npm install -g [email protected]


Then update node to the next version,


npm install -g [email protected]  
or
npm install -g [email protected]

Step 7: Command to select previous version npm on Ubuntu 19.04 (optional)

Note: Run these commands, if you know what are you doing otherwise it could mess your NPm setup.

Command to see the older available version on Ubuntu

npm view npm versions --json


Command to select the previous NPM version

npm i -g [email protected]


For example, I want to select the older 5.8.0 version then the command will be:

npm i -g [email protected]


or


sudo npm cache clean -f
sudo npm install -g n
sudo n 5.8.0

Step 8: Let’s install something using NPM (options step):

For example, we want to install Angular CLI using NPM then the commands will like this:

For locally:

npm install @angular/cli


Fo Globally: -g syntax refers to the global

npm install -g @angular/cli


Step 9: Uninstall node js ubuntu

The command to uninstall node.js on Ubuntu is:

To uninstall Nodejs

sudo apt-get remove nodejs


To uninstall NPM

sudo apt-get remove npm


Completely remove NPM related entries:

sudo rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/npm*


Thanks for reading

How to install NodeJs, MongoDB on Ubuntu 18.04

How to install NodeJs, MongoDB on Ubuntu 18.04

In this article, you'll learn how to install NodeJs, MongoDB on Ubuntu 18.04

In this article, you'll learn how to install NodeJs, MongoDB on Ubuntu 18.04

Node.js is a platform built on Chrome’s JavaScript runtime for scalable network applications and easily building and fast building applications.

The latest version node js PPA is maintaining by its official website and we can add this PPA to your Ubuntu systems and install node js on ubuntu with easy commands.

Node js package is available in the current release and LTS release. So, It is your choice to select which version you want to install on the system as per your requirements.

To install Nodejs on Ubuntu, let’s add the PPA to your system.

$ sudo apt-get install curl python-software-properties

$ curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash –

Now you successfully added node js PPA in your Ubuntu system.

Now execute the below command to install Node on and Ubuntu using apt-get.

This will also install NPM with Node.js.

This command also installs many other dependent packages on your Ubuntu system.

$ sudo apt-get install nodejs

After installation please verify your versions by following commands.

$ node -v

v12.1.0

And also check npm version

$ npm -v

v6.9.0

Now you successfully installed node js in your machine. Now you can test your node.js by creating Hello World text.

Create a file with servertext.js and write the following code in it.

var http = require(‘http’);

http.createServer(function (req, res) {

 res.writeHead(200, {‘Content-Type’: ‘text/plain’});

 res.end(‘Hello World\n’);

}).listen(4000, “127.0.0.1”);

console.log(‘Server running at ‘)

Install MongoDB on Ubuntu

MongoDB is a NoSQL database that offers high performance, high availability and automatic scaling of the enterprise level database.

MongoDB is a NoSQL database, so you cannot use SQL to retrieve and insert data, and it does not store data in tables like MySQL.

The data is stored in a “document” structure in JSON format (called BSON in MongoDB). MongoDB introduced in 2009 and is currently being developed by MongoDB Inc.

MongoDB only offers packages for 64-bit LTS Ubuntu versions. For example 16.04 LTS (xenial), 14.04 LTS (trusty), 18.04 LTS (bionic) and so on.

Now, I will install MongoDB 4.0 on Ubuntu 18.04 LTS.

GPG keys of the software are required for the Ubuntu package manager apt (Advanced Package Tool) to ensure the authenticity and consistency of the package.

Execute the following command to import MongoDB keys to your server.

$ sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 68818C72E52529D4

Create a source list for MongoDB by the following command.

$ sudo echo “deb http://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

Now update the repository with the following apt command

 $ sudo apt-get update

Install MongoDB in Ubuntu using the following command

$ sudo apt-get install -y mongodb-org

Now start MongoDB and add it as a service and it started when the system is boot.

$ sudo systemctl start mongodb

$ sudo systemctl enable mongodb

$ sudo systemctl stop mongodb

Now check your MongoDB version using the following command

$ mongod –version

Node and MongoDB getting started:

Install the npm MongoDB package using the following command.

$ sudo npm install mongodb

const mongo = require(“mongodb”);

How to create a database?

To create a database in MongoDB, we can use MongoClient object and then specify the connection URL with IP address along with the database name as follows.

const MongoCli = require(‘mongodb’).MongoClient;
const url = “mongodb://127.0.0.1:27017/testdb”;

 
MongoCli.connect(url, (err, db) => {
 if (err) throw err;
 console.log(“Database created!”);
 db.close();
 });

How to create a collection?

use createCollection() to create collection.

const MongoCli = require(‘mongodb’).MongoClient;
const url = “mongodb://127.0.0.1:27017/”;

 
MongoCli.connect(url, (err, db) => {
 if (err) throw err;
 var dbo = db.db(“testdb”);
 dbo.createCollection(“users”, (err, res) => {
  if (err) throw err;
  console.log(“Collection created!”);
  db.close();
 });
 });

How to Insert record?

Insert a document into users collection. We can insert a record using **insertOne()**method.

const MongoCli = require(‘mongodb’).MongoClient;
const url = “mongodb://localhost:27017/”;

MongoCli.connect(url, (err, db) => {
 if (err) throw err;
 var dbo = db.db(“testdb”);
 var myobj = { name: “Stringify Inc”, address: “Road No 37” };
 dbo.collection(“users”).insertOne(myobj, (err, res) => {
  if (err) throw err;
  console.log(“1 document inserted”);
  db.close()
 });
 });

How to do an update?

We can update a record or document by using the updateOne() method.

const MongoCli = require(‘mongodb’).MongoClient;
const url = “mongodb://127.0.0.1:27017/”;

MongoCli.connect(url, (err, db) => {
 if (err) throw err;
 var dbo = db.db(“testdb”);
 var myquery = { address: “Road No 37” };
 var newvalues = { $set: {name: “Mickey”, address: “Canyon 123” } };
 dbo.collection(“users”).updateOne(myquery, newvalues, (err, res) => {
  if (err) throw err;
  console.log(“1 document updated”);
  db.close();
 });
 })

How to do delete?

We can delete a record or document by using deleteOne() method.

const MongoClient = require(‘mongodb’).MongoClient;
const url = “mongodb://localhost:27017/”;

MongoClient.connect(url, (err, db) => {
 if (err) throw err;
 var dbo = db.db(“testdb”);
 var myquery = { address: ‘Mountain 21’ };
 dbo.collection(“users”).deleteOne(myquery, (err, obj) => {
  if (err) throw err;
  console.log(“1 document deleted”);
  db.close();
 });
 });

How to do sort?

We can use sort() method to sort the result in ascending order or descending order. It will take one parameter and an object decide the sorting order.

const MongoCli = require(‘mongodb’).MongoClient;
const url = “mongodb://localhost:27017/”;

MongoClient.connect(url, (err, db) => {
 if (err) throw err;
 var dbo = db.db(“testdb”);
 var mysort = { name: 1 };
 dbo.collection(“users”).find().sort(mysort).toArray((err, result) => {
  if (err) throw err;
  console.log(result);
  db.close();
 });
 });

Below the code for descending order.

const MongoCli = require(‘mongodb’).MongoClient;
const url = “mongodb://localhost:27017/”;

MongoCli.connect(url, (err, db) => {
 if (err) throw err;
 var dbo = db.db(“testdb”);
 var mysort = { name: -1 };
 dbo.collection(“users”).find().sort(mysort).toArray((err, result) => {
  if (err) throw err;
  console.log(result);
  db.close();
 });
 });

How to do Limit?

We can do limit with** limit() **method.

const MongoCli = require(‘mongodb’).MongoClient;
const url = “mongodb://localhost:27017/”;

MongoCli.connect(url, (err, db) => {
 if (err) throw err;
 var dbo = db.db(“testdb”);
 dbo.collection(“users”).find().limit(5).toArray((err, result) => {
  if (err) throw err;
  console.log(result);
  db.close();
 });
 });

How to do Join?

MongoDB is not like a relational database, but we can do left outer join by using $lookup stage.

Let’s assume we have “orders” and “products” collections

const MongoCli = require(‘mongodb’).MongoClient;
const url = “mongodb://127.0.0.1:27017/”;

MongoClient.connect(url, (err, db) => {
 if (err) throw err;
 var dbo = db.db(“testdb”);
 dbo.collection(‘orders’).aggregate([
  { $lookup:
   {
    from: ‘products’,
    localField: ‘product_id’,
    foreignField: ‘_id’,
    as: ‘orderdetails’
   }
  }
  ]).toArray((err, res) => {
  if (err) throw err;
  console.log(JSON.stringify(res));
  db.close();
 });
 });

How to Install Node.js and npm on Ubuntu 18.04

How to Install Node.js and npm on Ubuntu 18.04

In this tutorial, we will show you several different ways of installing Node.js and npm on Ubuntu 18.04\. The same instructions apply for any Ubuntu-based distribution, including Kubuntu, Linux Mint and Elementary OS.

Node.js is an open-source cross-platform JavaScript run-time environment that allows server-side execution of JavaScript code. This means that you can run JavaScript code on your machine as a standalone application, free of any web browser. Node.js is mainly used to build back-end server-side applications, but it is also very popular as a full-stack and front-end solution.

Npm is the default package manager for Node.js and the world’s largest software registry.

In this tutorial, we will show you several different ways of installing Node.js and npm on Ubuntu 18.04. The same instructions apply for any Ubuntu-based distribution, including Kubuntu, Linux Mint and Elementary OS.

If you need Node.js only as a local runtime for deploying Node.js applications then the simplest option is to install Node.js from the NodeSource repository. Developers should prefer installing Node.js using the NVM script. Choose the installation option that is appropriate for your environment. It is best to consult the documentation of the Node.js application that you use to find out which Node.js versions are supported.

Installing Node.js and npm from NodeSource

NodeSource is a company focused on providing enterprise-grade Node support and they maintain a repository containing the latest versions of Node.js.

Use this repository if you need to install a specific version of Node.js. At the time of writing, NodeSource repository provides the following versions - v12.x, v11.x, v10.x, and v8.x. We'll install the current LTS version of Node.js, version 10.x, Carbon.

To install Node.js and npm from the NodeSource repository, follow these steps:

  1. Enable the NodeSource repository by running the following curl as a user with sudo privileges:

    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    

    The command will add the NodeSource signing key to your system, create an apt sources repository file, install all necessary packages and refresh the apt cache.

    If you need to install another version, for example 12.x, just change setup_10.x with setup_12.x

  2. Once the NodeSource repository is enabled, install Node.js and npm by typing:

    sudo apt install nodejs
    

    The nodejs package contains both the node and npm binaries.

  3. Verify that the Node.js and npm were successfully installed by printing their versions:

    node --version
    
    v10.16.3
    
    npm --version
    
    6.9.0
    
Installing Node.js and npm using NVM

NVM (Node Version Manager) is a bash script used to manage multiple active Node.js versions. With NVM you can install and uninstall any specific Node.js version you want to use or test.

To install Node.js and npm using NVM on your Ubuntu system, perform the following steps:

1. Installing NVM (Node Version Manager) script

To download and install the nvm script run:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash

The command above will clone the NVM repository from Github to the ~/.nvm directory:

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

As the output above says, you should either close and reopen the terminal or run the commands to add the path to nvm script to the current shell session. You can do whatever is easier for you.

Once the script is in your PATH, verify that nvm was properly installed by typing:

nvm --version

0.34.0

2. Installing Node.js and npm

Now that the nvm is installed you can install the latest available version of Node.js, by typing:

nvm install node

The output should look something like this:

Downloading and installing node v12.8.1...
Downloading https://nodejs.org/dist/v12.8.1/node-v12.8.1-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.8.1 (npm v6.10.2)
Creating default alias: default -> node (-> v12.8.1)

Once the installation is completed, verify it by printing the Node.js version:

node --version

v12.8.1

Let's install two more versions, the latest LTS version and version 8.10.0

nvm install --lts
nvm install 8.10.0

To list installed Node.js versions type:

nvm ls

The output should look something like this:

->      v8.10.0
       v10.16.3
        v12.8.1
default -> node (-> v12.8.1)
node -> stable (-> v12.8.1) (default)
stable -> 12.8 (-> v12.8.1) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/dubnium (-> v10.16.3)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.1 (-> N/A)
lts/dubnium -> v10.16.3

The entry with an arrow on the right (-> v8.10.0) is the Node.js version used in the current shell session and the default version is set to v12.8.1. Default version is the version that will be active when opening new shells.

You can change the currently active version with:

nvm use 10.16.3

Now using node v10.16.3 (npm v6.9.0)

If you want to change the default Node.js version use the following command:

nvm alias default 10.16.3

Install Node.js and npm from the Ubuntu repository

Node.js and npm packages are available from the default Ubuntu 18.04 repositories.

At the time of writing, the version included in the Ubuntu repositories is v8.10.0 which is the previous TLS version.

To install nodejs and npm run the following commands:

sudo apt update
sudo apt install nodejs

The Node.js executable from the Ubuntu repositories is named nodejs instead of node because of a conflict with another package.

Verify the installation by executing:

nodejs --version

v8.10.0

Install development tools

To be able to compile and install native add-ons from npm you need to install the development tools.

The following command will install all the necessary packages including the GCC compilers:

sudo apt install build-essential

Uninstall Node.js

If for some reasons you want to uninstall Node.js and npm packages, you can use the following command:

sudo apt remove nodejs npm

Conclusion

We have shown you three different ways to install Node.js and npm on your Ubuntu 18.04 server. The method you choose depends on your requirements and preferences. Even though installing the packaged version from the Ubuntu or NodeSource repository is easier, the nvm method gives you more flexibility for adding and removing different Node.js versions on a per-user basis.

Thank you for reading !

How to Install Node Js & NPM In Ubuntu 18.04

How to Install Node Js & NPM In Ubuntu 18.04

In Ubuntu 18.04 How to Install Node Js & NPM . In this article , we’ll show you how to install Node Js & NPM on Ubuntu. NPM is a node package manager. NPM is the package manager for the Node JavaScript platform. It puts modules in place so that node can find them, and manages dependency conflicts intelligently.

In Ubuntu 18.04 How to Install Node Js & NPM . In this article , we’ll show you how to install Node Js & NPM on Ubuntu. NPM is a node package manager. NPM is the package manager for the Node JavaScript platform. It puts modules in place so that node can find them, and manages dependency conflicts intelligently. It is extremely configurable to support a wide variety of use cases. Most commonly, it is used to publish, discover, install, and develop node programs.

After install npm and node js on ubuntu, we will run the simple command check the version of installed node js & npm on ubuntu system.

What is Node.js?

  • It is an open source server environment
  • It is free
  • Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
  • Node.js uses JavaScript on the server

What Can Node.js Do?

  • It can generate dynamic page content
  • It can create, open, read, write, delete, and close files on the server
  • Node.js can collect form data
  • It can add, delete, modify data in your database

Install Node js & NPM On Ubuntu

before install the Node js & NPM on Ubuntu. First update the apt package :

sudo apt-get update

sudo apt-get upgrade 

Add Node.js PPA

PPA (Personal Package Archives) is Python software libraries. Before install node js & npm on ubuntu, first install PPA in our ubuntu system. Using this below commands.

sudo apt-get install python-software-properties

curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -

Now Install Node.js and NPM On Ubuntu

After adding PPAs in our system. we need to run the below command for installing node js & npm on Ubuntu.

For Install Node js on Ubuntu :

sudo apt-get install nodejs

For Install NPM on Ubuntu

sudo apt-get install npm

Test Version

After complete the installation process. We need to check the version.

Check the installed Node js version :

nodejs  -v

Check the Installed NPM version :

npm -v

Conclusion

You have successfully installed Node js & NPM on Ubuntu 18.04. You are now ready to start deploying your applications and use Node js and NPM.

If you have any questions or thoughts to share, use the comment form below to reach us.