Skip to main content

How to Install Node.js on Ubuntu

In our previous Node.js installation tutorial, we have explained how to install Node.js on Windows. In this tutorial, you will learn how to install Node.js on Ubuntu 18.04. Installing the Node.js is the first step to getting started and running your first application with Node.js.

Node.js is a cross-platform run-time for running server-side JavaScript applications. It provides a consistent JavaScript platform for general-purpose programming to build network applications quickly by leveraging JavaScript on both the front-end and server-side.


Also, read:

In this tutorial, we’ll show you how to getting started with Node.js on an Ubuntu 18.04 server. The Tutorial will include following:

  • Install Node.js on Ubuntu
  • Run Your First Node.js Application

Step1: Install Node.js on Ubuntu 18.04

The Ubuntu 18.04 contains a version of Node.js in its default repositories, so in order to get this version, we just have to use the apt package manager and update our local package index first, and then install from the repositories:

  1. So first we will update our local repository by running below command:
    $ sudo apt-get update
    
  2. Now we will install Node.js by running below command:
    $ apt-get install nodejs
    
  3. You will also need to install npm which is the Node.js package manager and needed to install modules and packages to use with Node.js application. So we will by running below command to install npm:
    $ sudo apt-get install npm
    
  4. After above steps, we will check which version of Node.js we have installed. We will run below command for this and it will display installed Node.js version.
    $ node -v
    

Congratulations! you have successfully installed Node.js on your Ubuntu 18.04 server. Now in next steps of this tutorial, we will create our first Node.js application.

Step2: Run Your First Node.js Application

After Node.js installation on Ubuntu, now we will develop our first application in Node.js.

  1. We will create a new folder for this application and change into that directory. Here we will create project directory first-app.
    $ mkdir first-app
    $ cd first-app
    
  2. Now we will create package.json file for this Node.js application to define what libraries or modules will be use in this project. We will create package.json file by running below command:
    /first-app$  npm init
    

    we need to update details into package.json file in your project folder like project name with first-app, entry point with app.js and main with app.js as it will the main file in our project.

  3. Now we will create app.js file by running below command:
    /first-app$ vi app.js

    and now write below JavaScript code into app.js file.

    var http = require('http');
    
    http.createServer(function (request, response) {
        response.writeHead(200, {'Content-Type': 'text/plain'});
        response.end('Hello! My first Node.js appliaction\n');
    }).listen(3300);
    
    console.log('Server started');
    
  4. Now after saving our app.js file and we run our first Node.js application by running below command:
    /first-app$ node app.js
    

    You will see ‘Server started’ in the terminal, Now we will open our web browser and go to http://localhost:3300 url, we will see Hello! My first Node.js application message in browser.

Congratulations again! we have successfully run our first Node.js application.

Conclusion

Here you have learned to install Node.js on Ubuntu 18.04 in easy steps and run your first Node.js application. Node.js is very fast for developing network applications. So you can install and try to develop applications easily. If you have any query, you can send through comments.

You may also like: