WindowsMacSoftwareSettingsSecurityProductivityLinuxAndroidPerformanceConfigurationApple All

How to Install Node.js on Ubuntu

Edited 3 weeks ago by ExtremeHow Editorial Team

Node.jsJavaScriptUbuntuProgrammingLinuxSoftwareInstallationOperating SystemsDevelopmentSystem

How to Install Node.js on Ubuntu

This content is available in 7 different language

Node.js is a powerful, JavaScript-based platform built on Google Chrome's V8 JavaScript engine. It is used to develop server-side and networking applications. Node.js applications are written in JavaScript and can be run within the Node.js runtime on a variety of platforms, including Windows, macOS, and Linux. In this article, we will explore how to install Node.js on the popular Linux distribution Ubuntu. We will walk through the steps in simple terms and provide practical examples where necessary.

What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside of a web browser. This means that with Node.js, you can write server-side code using JavaScript. Node.js was initially created to build scalable network applications. It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, ideal for data-intensive and real-time applications. Node.js has a huge library of JavaScript modules, called npm (Node Package Manager), which simplifies the development of web applications.

Why use Node.js?

Preparations before installation

Before you install Node.js, you need to prepare your environment. Follow these steps to get started:

  1. Access to the Ubuntu terminal: Make sure you have access to the Ubuntu terminal on your machine. This is where all the installation steps will take place.
  2. Internet connection: Make sure your machine is connected to the internet to download the required files.
  3. Free up space: It's wise to make sure you have enough space on your machine to install Node.js and any future dependencies.
  4. Updating the system: First, you should update your system to make sure you have the latest package listings. Run the command:
    sudo apt update && sudo apt upgrade

This command updates your package list and installs any updated packages.

Ways to install Node.js on Ubuntu

There are several ways to install Node.js on Ubuntu. Here, we will cover three popular methods:

  1. Installing Node.js from Ubuntu Repositories
  2. Installing Node.js using nvm (Node Version Manager)
  3. Installing Node.js using the NodeSource repository

1. Installing Node.js from Ubuntu Repositories

This is the simplest method, but it may not provide the latest version. There are Node.js packages in the Ubuntu repositories that can be easily installed using the apt package manager.

  1. First, update the apt package index using the following command:
    sudo apt update
  1. Next, install Node.js using the following command:
    sudo apt install nodejs
  1. Install the Node.js package manager npm with the following:
    sudo apt install npm
  1. Verify the installation and check the installed version of Node.js:
    nodejs -v

And for npm, type:

    npm -v

The Node.js version installed through this method may not be the latest. If you need the latest version, consider using nvm or NodeSource.

2. Installing Node.js using nvm (Node Version Manager)

Node Version Manager (nvm) is a tool that allows you to manage multiple Node.js versions in a single machine. This is especially useful when you need to maintain compatibility with different projects.

  1. Install nvm by running the installation script from your terminal. This will download and install nvm for your Node environment.
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Load the newly installed nvm into the current terminal session:

    source ~/.bashrc
  1. You can now verify that nvm is installed:
    command -v nvm

If the installation was successful the output should be 'nvm'.

  1. With nvm installed, you can install the latest version of Node.js:
    nvm install node

Optionally, specify a specific version:

    nvm install v14.17.0
  1. Set the default Node.js version:
    nvm alias default node

or use:

    nvm use node
  1. Verify that the correct Node.js version is installed:
    node -v

The advantage of using nvm is the ability to switch between different Node.js versions depending on the project requirements. nvm use VERSION.

3. Installing Node.js using the NodeSource repository

NodeSource maintains up-to-date Node.js packages for enterprise Linux and Ubuntu. This method ensures that you install the latest version.

  1. Add the NodeSource PPA (Personal Package Archive) to your system:
    curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -

This script automatically adds the required repositories.

  1. After adding the repository, install Node.js:
    sudo apt install -y nodejs
  1. Verify the installation:
    node -v

and for npm:

    npm -v

The NodeSource method is a very reliable way to get the latest version of Node.js, while also requiring using the apt package manager for installation management.

Test the Node.js installation

After installation, it is important to check if Node.js runs correctly. You can do this by creating a simple Node.js application.

  1. Create a new directory for your test app:
  2. Navigate to the directory:
  3. Create a new file, app.js, in the directory:
  4. Edit the file and add the following JavaScript code:
    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}/`); });
  1. Run the application using Node.js:
  2. Open a web browser and visit http://127.0.0.1:3000/ to see the "Hello World" output.

This confirms that Node.js is installed correctly and you can start developing your applications.

Conclusion

Installing Node.js on Ubuntu can be done in several different ways. Whether you use the system's repository, the Node version manager or the NodeSource repository depends on your personal needs and the specific versioning requirements of your project. Once installed, use it to build traditional web applications, real-time applications, APIs or even complex enterprise-level systems. Each technology has its own advantages, providing different levels of flexibility and ease of maintenance.

After setting up Node.js correctly, you now have the baseline to explore the vast possibilities of JavaScript on server machines. From developing scalable applications to taking advantage of the wide catalog of npm packages, Node.js opens up a whole world of possibilities for any developer working on the web and beyond.

If you find anything wrong with the article content, you can


Comments