Blog / How to install NodeJS on Ubuntu 22.04

How to install NodeJS on Ubuntu 22.04

by SW Team

Node.js is a widely used JavaScript runtime environment for web and server application development, due to its performance and scalability.

It uses a non-blocking, event-driven I/O model that is lightweight and efficient, perfect for real-time, data-intensive applications. Given these capabilities, it is a popular choice for building APIs and microservices.

In this post, I'll walk you through the steps required to install Node.js on a machine running Ubuntu 22.04.

1. Upgrade the system:

Before starting with the installation, it is important to make sure that your system is up to date. Open a terminal and run the following commands:

sudo apt update
sudo apt upgrade -y

2. NPM Installation (Recommended):

Node Package Management, or NPM, is the default package manager for Node.js and is essential for managing dependencies in Node.js projects. It allows developers to install, update, and manage the third-party libraries and modules their projects need. Without npm, you would have to manually manage all dependencies, which would be inefficient and error-prone.

apt install npm -y

The latest version of "npm ” will be installed, you can check the version with the following command:

npm -v

3. Installing Node.js from the official Ubuntu 22.04 repositories

The default Ubuntu repositories include Node.js. Therefore, you can install Node.js easily, using the “apt” package manager.

Then install Node.js with the following command:

apt install nodejs -y

Once the installation is finished, you will be able to check the version of the Node.js installation:

node -v

This method will download and install the latest version available in the Ubuntu repositories, currently v12.22.9.

4. Install a specific version of Node.js:

If you want to install a specific version of Node.js on an Ubuntu 22.04 server, you can use NVM(Node Version Manager), to manage all available versions, easily.

To download and install NVM, run the following script:

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

Next, run the following commands to load NVM into the terminal session(required):

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"

You can check some of the available versions with the following official Node.js URL:

https://nodejs.org/en/download/package-manager

In order to install a specific version. For example, if you want to install version 17.1.0, you can use the following command with the version:

nvm install v17.1.0

5. How to Uninstall Node.js from Ubuntu 22.04 server?

If you used "apt ‘ to install it, use the ’apt remove ” command to remove the Node.js version from the system repositories:

apt remove nodejs

If you used NVM to install Node.js, you must first deactivate the version of Node.js that is currently active and in use:

nvm deactivate 

Once disabled, you can uninstall Node.js:

nvm uninstall node

In case you have more than one version of Node.js installed, you can uninstall a specific version with:

nvm uninstall [version de node.js]

As an example, to uninstall version 17.1.0:

nvm uninstall V17.1.0

Conclusion

Hopefully you now understand how to install Node.js on Ubuntu 22.04. Once Node.js is properly installed on your Ubuntu 22 system, you will be able to develop and run Node.js applications. To maintain the security of your Node.js applications, remember to follow security best practices, keep your dependencies up to date and apply appropriate security measures.

cta:cloud_app_swpanel_smart_d5

i