Guide for Ubuntu: Install NodeJs instructions

ubuntu install nodejs

ubuntu install nodejs

Node.js (or NodeJs as it is commonly called), is a well-known open-source javascript runtime. It allows developers to run Javascript (referred to as JS generally and in this article as well) on the server side. Node.js allows seamless back-end development and full-stack applications.

If your preferred operating system is Ubuntu, install Nodejs for your production server requirements. Nodejs also comes in handy when you are setting up a development environment. Today at Unixmen, we are going to learn how to install Nodejs in Ubuntu. We will walk you through the common methods in Ubuntu to install Nodejs, from basic repositories to advanced package managers.

Why Should You Install Node.js on Ubuntu?

Ubuntu is one of the most stable Linux based operating systems. It provides a customizable setup for web development. In Ubuntu, install NodeJs to:

  • Create cross-platform runtime and create web and mobile applications
  • Utilize Node package manager (npm) to make use of numerous libraries and modules
  • Build and ship scalable applications with the help of JS at the back-end

What You Would Need

In Ubuntu, to install NodeJS successfully, you will need:

  • A device running on Ubuntu (the latest 22.04 is recommended but anything above 18.04 is sufficient)
  • Root access or sudo privileges

Now let us see the multiple methods of installing NodeJS on Ubuntu machines.

Method 1: Installing NodeJS from Ubuntu Repositories

The simplest way to install NodeJS on an Ubuntu machine is via APT. APT is Ubuntu’s official package manager. There are instances when APT installs an older version of NodeJS so it is better to keep an eye on the version installed.

In Ubuntu Install NodeJS with APT

Start by updating the package list. Execute the command:

sudo apt update

Once the package list is updated, install Node.js and NPM by executing the command:

sudo apt install -y nodejs npm

To check of the installation is successful and to verify the versions of Node.js and npm, execute the commands:

node -v
npm -v

How to Install NodeJS on Ubuntu Using NodeSource Repository

The problem with installing NodeJS via APT is that it does not install the latest version. For the latest stable release you can use NodeSource Repository.

NodeSource is a trusted repository that offers the latest Node.js versions. An added advantage is that this method lets you choose the version of Node.js so that you can be aware of recent releases.

First update the package list by running the command:

sudo apt update

Next step is to install curl by running the command:

sudo apt install -y curl

Next step is to download and run the NodeSource setup script. NodeSource provides a setup script for every NodeJS version. Tweak the command and run to add the repository for the latest version.

curl -fsSL https://deb.nodesource.com/setup_VersionNumber | sudo -E bash -

Run this command to install NodeJS.

sudo apt install -y nodejs

As usual, verify the successful installation of the package and the version of NodeJS installed by running the command:

node -v
npm -v

For production environments, using NodeSource is preferred because you can choose which version of NodeJS to install.

How to Install NodeJS using Node Version Manager

Developers who work with multiple NodeJS versions prefer Node Version Manager. Node version manager or “n”, lets you install, update, and switch between different versions of NodeJS easily.

First step is to install “n” using npm. In case npm is not already installed, execute the command:

sudo npm install -g n

Nexts step is to install the latest stable version of NodeJS. Run the command:

sudo n stable

If you have a specific version of NodeJS to be installed, you can replace “VersionNumber” with the version number and execute this command:

sudo n VersionNumber

You could have guessed what we are about to do right now. To verify if the installation is successful, execute the command:

node -v
npm -v

 

You can switch between versions with “n”. To list the installed versions, execute the command:

n list

To use a specific version, run the command:

sudo n VersionNumber

Most developers prefer n when they prefer to work with different NodeJS versions. This helps them immensely during development and testing phases.

How to Install NodeJS Using NVM

Unlike “n”, nvm (or node version manager) does not require sudo access so this is helpful in user-level installations. But it still retains the advantage of managing multiple NodeJS versions, especially in developer environments.

How to Install NVM
First step is to download and run the NVM installation script.

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

Once NVM is installed, restart the terminal or source your profile. Execute the command:

source ~/.bashrc

Verify if NVM is successfully installed by executing the command:

nvm --version

To install the latest version of NodeJS via NVM, run the command:

nvm install node

If you have a specific version of NodeJS on your mind, you can replace VersionNumber with the version of your choice and execute the command:

nvm install VersionNumber

To switch between versions, first view the list of available versions by running the command:

nvm list

Then, switch to one of the versions listed by replacing VersionNumber with the number listed in the list and running the command:

nvm use x.x.x

To verify if the installation is successful, execute the command:

node -v
npm -v

Wrapping Up

At Unixmen, ur goal is not to give straightforward commands, but rather explain the use cases, differences in approaches, and then the commands. In this guide, we have covered the various ways in Ubuntu to install NodeJS, depending on varying environments. The right installation method depends on what you want and the access levels you have. While the official APT method is quick and easy, it might not give you the latest version. The “n” method is commonly used but you need sudo access for that. Choose the installation method carefully.

You Might be Interested In

How to Install Node.Js on Ubuntu Linux 15.04