DEV Community

loading...
Cover image for How do I install NodeJS in Ubuntu ?

How do I install NodeJS in Ubuntu ?

bricourse
Developer, writer, coffee and cookie lover!!
・3 min read

Quick instructions on how to install NodeJS in Ubuntu Linux …

**Step 1: **add this PPA to your Ubuntu 20.04 LTS, 19.10, 18.04 LTS, 16.04 LTS (Trusty Tahr) and 14.04 LTS (Xenial Xerus) systems and install node.js on Linux VPS with easy commands.

Node.js package is available in the LTS release and the current release. Select which version you wish to install on your machine according to your needs. Then add the PPA to your system in order to install Nodejs on Ubuntu.

Use Current Release — During the last update of this tutorial, Node.js 15.8.0 is the current Node.js release available.

sudo apt-get install curl
curl -sL [https://deb.nodesource.com/setup_15.x](https://deb.nodesource.com/setup_15.x) | sudo -E bash -
Enter fullscreen mode Exit fullscreen mode

Use LTS Release — Instead of current release, its good idea to use stable version. During the last update of this tutorial, Node.js 14.x LTS release available.

sudo apt-get install curl
curl -sL [https://deb.nodesource.com/setup_14.x](https://deb.nodesource.com/setup_14.x) | sudo -E bash -
Enter fullscreen mode Exit fullscreen mode

For this tutorial, I am using the latest current release and added their PPA to my system.

Step 2 — Install Node.js on Ubuntu

You can successfully add Node.js PPA to the Ubuntu system. Now execute the below command 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 system.

sudo apt-get install nodejs
Enter fullscreen mode Exit fullscreen mode

Step 3 — Check Node.js and NPM Version

After installing node.js verify and check the installed version. You can find more details about current version on node.js official website.

node -v 

v15.8.0
Enter fullscreen mode Exit fullscreen mode

Also, check the npm version

npm -v 

7.4.0
Enter fullscreen mode Exit fullscreen mode

Step 4 — Create Demo Web Server (Optional)

This is an optional step. If you want to test your node.js install. Let’s create a web server with “Hello World!” text. Create a file server.js

vim server.js
Enter fullscreen mode Exit fullscreen mode

and add the following content

server.js

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(3000, "127.0.0.1");
console.log('Server running at [http://127.0.0.1:3000/');](http://127.0.0.1:3000/');)
Enter fullscreen mode Exit fullscreen mode

Now start the Node application using the command.

node server.js

debugger listening on port 5858
Server running at [http://127.0.0.1:3000/](http://127.0.0.1:3000/)
Enter fullscreen mode Exit fullscreen mode

You can also start the application with debugging enabled with the following commands.

node --inspect server.js

Debugger listening on ws://127.0.0.1:9229/938cf97a-a9e6-4672-922d-a22479ce4e29
For help, see: [https://nodejs.org/en/docs/inspector](https://nodejs.org/en/docs/inspector)
Server running at [http://127.0.0.1:3000/](http://127.0.0.1:3000/)
Enter fullscreen mode Exit fullscreen mode

The web server has been started on port 3000. Now access http://127.0.0.1:3000/ URL in browser. Now you will need to configure a front-end server for your app.

Additional Resources to learn NodeJS:

Introduction to NodeJS Learn and Understand JavaScript

NodeJS — The Complete Guide (MVC, REST APIs, GraphQL, Deno)

Discussion (5)

Collapse
blindfish3 profile image
Ben Calder

Or just use nvm

Collapse
bricourse profile image
bricourse Author

any comments or instructions on how to do it that way ?

Collapse
blindfish3 profile image
Ben Calder • Edited

Sorry: that was sent in a hurry from a mobile so I didn't include a link.

Node Version Manager (nvm) is a really useful tool for installing and managing node installations. It makes installing node a trivial one line command. It also allows installation of multiple versions of node and to switch between these using a single command; or IIRC even using a project level config file.

It's especially useful if working on multiple projects which may have dependencies on different node versions. Or running a project against the version of node available on the deployment server (to ensure you're developing/testing against the correct version) whilst still being able to try out a bleeding edge release locally.

So you're not tied to using the one version you installed using the method you described ;)

Thread Thread
bricourse profile image
bricourse Author

Sounds amazing, in a way I guess it would be kinda like the concept of a container, I will look more into how to install NVM hopefully is not that complicated

Thread Thread
blindfish3 profile image
Ben Calder

IIRC it's fairly straightforward to install. I think the only possible problem that may crop up is ensuring the command is available on your PATH; but I'm pretty sure that's already documented.

The other thing to be careful of is what version you currently have active; since some dependencies may break depending on the version. I've had a couple of cases of things not working and having to remember to switch to the appropriate version of node.