It’s simple to install Node.js on Ubuntu, and it’s a strong tool for creating server-side apps. Node.js can be installed on Ubuntu using a variety of techniques, each of which has benefits and drawbacks. With node.js also you can understand how to Install ElasticSearch on Ubuntu Itself.
Using the Ubuntu package manager to install Node.js is the simplest and most recommended approach. This method entails downloading the most recent versions of Node.js and npm from the Ubuntu repository and installing them using the apt package manager. The majority of consumers are advised to choose this technique because it is simple to use and offers a secure installation.
You may install and switch between various Node.js versions using nvm (Node Version Manager) if you need to handle many Node.js versions on your PC. Installing nvm, a utility that enables simple management of several Node.js versions on your machine, is necessary for this procedure. You can download and install certain Node.js versions as needed, then switch between them using nvm.
Ways of Installing Node.js on Ubuntu
Option 1: Download and Install Node.js from the Official Ubuntu 22.04 23.04 Repository
Npm and Node.js are included in the standard Ubuntu repositories. As a result, installing them is simple using the APT package manager. Update the Ubuntu system first:
$ sudo apt update
Next, use apt to install Node.js and NPM as seen below:
$ sudo apt install nodejs npm
With this, Node.js and other necessary components are installed.
Option 2: Install Node.js from Nodesource as an Alternative.
In order to assist businesses in running production-ready Node.JS applications with performance, reliability, and security in mind, Nodesource is a Node.JS partner. If you want to install more recent versions of Node.JS and NPM, use this technique.
The installation script that NodeSource offers refreshes the repository and adds the Nodesource repository to the list of sources. Therefore, download the script and execute it as follows using the curl command:
$ curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
Install Node.JS using these instructions after the script has successfully run.
$ sudo apt install nodejs
This sets up Node.js along with other required dependencies and libraries. Run: to verify Node.js is installed.
$ node --version
Option 3: Set up NVM to Install Node JS and NPM
The script known as NVM, or “Node Version Manager,” enables you to control multiple Node.js versions on your computer. Run the following script to download and install NVM before installing NodeJS and NPM with it:
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
On the terminal, you will see a number of outputs as depicted.
Then, execute the instructions listed below to load nvm:
$ export NVM_DIR="$HOME/.nvm" $ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" $ [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
Check the installed version of nvm as follows:
$ nvm -v
Run the following command to get a list of every Node.js version that supports NVM:
$ nvm ls-remote
Next, you can install the latest available version of node.js with the following command:
$ nvm install node
Enter the version number to install a certain Node.js version. Run the following command, for example, to install NodeJS 18.1.0:
$ nvm install 18.1.0
There are various ways to install Node.js on Ubuntu 22.04, 23.04, 20.04 each having pros and cons of their own. The approach you select will be determined by the needs and conditions you have. The most straightforward and suggested approach for managing various versions of Node.js is the Ubuntu package manager, although more experienced users might prefer to utilize NodeSource or nvm.
In the end, the strategy you use will rely on your unique wants and demands. The Ubuntu package manager technique is the simplest and best choice if you’re just getting started with Node.js. The nvm or NodeSource methods may be more appropriate if you have more complex requirements, such as requiring a certain version of Node.js or managing several versions of Node.js. A pre-built package can be the best choice if you’re utilizing a certain platform or framework that needs a customized version of Node.js.