Node.js is a powerful runtime environment that allows you to run JavaScript on the server side, making it an essential tool for web developers. If you’re using Ubuntu 22.04 and want to harness the capabilities of Node.js for your projects, this guide will walk you through the installation process step by step.
Prerequisites
Before you start installing Node.js on your Ubuntu 22.04 system, make sure you have the following:
- An Ubuntu 22.04 Installation: Ensure that you are running Ubuntu 22.04 or a compatible version.
- A Terminal: You will need a terminal to run commands.
- Superuser Privileges: You should have superuser privileges or access to a user account with sudo rights to install software.
Method 1: Installing Node.js using the Official Ubuntu Repositories
The easiest way to install Node.js is by using the official Ubuntu repositories. This method provides a stable version, but it may not always be the latest. To install Node.js this way, follow these steps:
Step 1: Update your package repository.
$ sudo apt update
This command will update the package repository to ensure that you have the latest information on available packages.
Step 2: Install Node.js and npm.
You can install Node.js and npm using the following command:
$ sudo apt install nodejs npm
This command will install both Node.js and the Node Package Manager (npm).
Step 3: Verify the installation.
To check if Node.js and npm have been installed correctly, run the following commands:
$ node -v
$ npm -v
You should see the installed versions of Node.js and npm, confirming that the installation was successful.
Method 2: Installing Node.js using NodeSource
NodeSource provides more up-to-date Node.js versions and offers flexibility in selecting specific versions. Here’s how to install Node.js using NodeSource:
Step 1: Install the required software.
Before adding the NodeSource repository, make sure you have the necessary software installed. This will allow you to add and update repositories via HTTPS:
$ sudo apt-get update
$ sudo apt install -y ca-certificates curl gnupg software-properties-common
$ sudo mkdir -p /etc/apt/keyrings
$ curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
Step 2: Create deb repository.
NodeSource maintains a repository for Node.js. To add it to your system, run the following command, replacing 16.x
with your desired Node.js version:
$ NODE_MAJOR=20
$ echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
Optional: NODE_MAJOR
can be changed depending on the version you need.
NODE_MAJOR=16
NODE_MAJOR=18
NODE_MAJOR=20
NODE_MAJOR=21
Step 3: Install Node.js and npm.
After adding the NodeSource repository, install Node.js and npm with the following command:
$ sudo apt-get update
$ sudo apt-get install nodejs -y
Step 4: Verify the installation.
To confirm the successful installation of Node.js and npm, run:
$ node -v
$ npm -v
You should see the installed versions displayed on the screen.
Uninstall nodejs
Ubuntu & Debian packages
To completely remove Node.js installed from the deb.nodesource.com package methods above:
$ sudo apt-get purge nodejs &&\
$ sudo rm -r /etc/apt/sources.list.d/nodesource.list &&\
$ sudo rm -r /etc/apt/keyrings/nodesource.gpg
Optional: Managing Node.js Versions with NVM
If you want to manage multiple Node.js versions on your system, you can use NVM (Node Version Manager). Here’s how to install and use NVM:
Step 1: Install NVM.
Download and install NVM by running the following command:
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
Step 2: Load NVM.
Close and reopen your terminal or run the following command to load NVM:
$ source ~/.nvm/nvm.sh
Step 3: Install Node.js using NVM.
Now you can install and manage different Node.js versions with NVM. For instance, to install Node.js version 20, use:
$ nvm install 20
You can switch between installed Node.js versions with:
$ nvm use 20
Conclusion
You’ve successfully installed Node.js on your Ubuntu 22.04 system using both the official repositories and NodeSource. With Node.js and npm installed, you’re equipped to build and run JavaScript applications on your server. Whether you’re a web developer or a system administrator, Ubuntu 22.04 is a fantastic platform for harnessing the power of Node.js for your projects. Happy coding!
http://local.brightwhiz/how-to-install-node-js-18-lts-on-centos-7-8-or-fedora-38-34/
Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.