Method 1: Install Node.js from the Ubuntu repository
Ubuntu 18.04 contains a version of Node.js in its default repositories that can be used to provide a stable experience. It will not be latest but it should be stable.
To get this version, you can use the
apt package manager. Update your local package with command given below:
$ sudo apt update
Now Install Node.js from the repositories using command given below:
$ sudo apt install nodejs
Above command will start installing nodejs and its dependencies so you have to wait until every thing done it might take few seconds. Once it is done you can verify nodejs installation by checking Nodejs and node package manager’s version using command given below
$ node -v $ npm -v
Above command will show installed versions of nodejs and npm.
Method 2: Install Node.js Latest Version
To get a more recent version of Node.js you can add the PPA (personal package archive) as this will have more up-to-date versions of Node.js than official Ubuntu repositories.
We will need curl to add node.js ppa in our system. In addition we will also need to install python-software-properties package if its is not installed already.
$ sudo apt-get install curl $ sudo apt-get install python-software-properties
Now Adding NodeJs PPA :
$ curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
After adding the node.js ppa we just need to give the node.js install command to install node.js. Npm will be installed automatically with node.js.
$ sudo apt-get install nodejs
Once completed, I will run the command below to check that I’ve installed the correct version
$ node -v //output: v11.5.0 $ npm -v //output: 6.4.1
You can uninstall Node.js using
apt command given below:
$ sudo apt remove nodejs
This command will remove the package and retain the configuration files. These may be of use to you if you intend to install the package again at a later point.
In the end you can see that there are a quite a few ways to get up and running with Node.js on your Ubuntu 18.04 server. Your need will dictate which of the above methods is best for your situation.