Ubuntu

How to Install Node.js and Npm on Ubuntu 22.04

Node.js is the runtime environment that is used to run JavaScript code outside of web browsers, especially on the server-side. It is generally used for back-end development. Npm is its default package manager which can be used to install node packages and their dependencies.

In this post, we will check out three different methods of installing Node.js and Npm on Ubuntu 22.04.

How to Install Node.js and npm using Ubuntu 22.04 Repository

The process of installing Node.js from the official Ubuntu repository is just like installing any other package. It is easy and straightforward and installs the latest stable version available.

Update the repositories:

sudo apt update

Now simply use the apt package manager to install Node.js and Npm on Ubuntu 22.04:

sudo apt install nodejs npm

apt will install Node.js, Npm, and all of their required dependencies:

Once the process is finished, you can use this command to verify whether Node.js was installed successfully:

nodejs --version

How to Install Node.js on Ubuntu 22.04 using Node Version Manager

If you want to install a specific version of Node.js then you should opt for nvm (Node Version Manager) which is a bash script that allows you to install and manage multiple versions of Node.js on the same machine.

First of all, you need to install the nvm script. To install nvm you can use either the curl or wget command (whichever command-line utility, you have installed on your system):

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

Close and reopen your terminal or run the below-given command to source the bash file:

source ~/.bashrc

Now to list all versions of Node.js available with nvm, use this command:

nvm list-remote

To install a specific version of Node.js on Ubuntu 22.04, utilize the below-given command:

nvm install [version_number]

Execute this command to list all of the installed versions of Node.js:

nvm ls

You can use the following command to change the default Node.js version of your system:

nvm alias default [version_number]

To activate a version of Node.js, use this command:

nvm use [version_number]

How to Install Node.js on Ubuntu 22.04 Using NodeSource PPA

You can also use NodeSource PPA to install a specific version of Node.js. Use the curl command to download the script to install your preferred Node.js version:

curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -

Note: You can replace ‘setup_12.x’ with your preferred version.

Once your package cache is updated and the NodeSource repository is enabled, run the below-given command to install Node.js:

sudo apt install nodejs

Use this command to verify the installation:

node --version

Conclusion

Node.js is an open-source runtime environment that is utilized for executing JavaScript code on the V8 engine and is mostly used for backend/server-side development. This post showed you three different methods of installing Node.js and its package manager on your Ubuntu 22.04 machine. The method you should use for this purpose depends on the flexibility that you need over running different versions of Node.js.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.