This guide will walk you through the installation of Node.js on Ubuntu 22.04 (Jammy Jellyfish).
You've come to the right place if you want to learn how to create web applications with Node.js. This beginner's guide will walk you through the process of installing Node.js on Ubuntu 22.04 (Jammy Jellyfish). This article will have you up and running in no time, whether you're a complete rookie or an experienced developer.
What is Node.js?
Node.js is a cross-platform, open-source JavaScript runtime environment that executes JavaScript code outside of a web browser. Ryan Dahl created it in 2009, and it has since become one of the most popular and extensively used web development tools.
Node.js employs an event-driven, non-blocking I/O approach that allows it to efficiently manage massive volumes of data and requests. It makes advantage of Google's V8 JavaScript engine, which compiles JavaScript into machine code, making it extremely fast.
Prerequisites
Before we begin, make sure you have the following prerequisites:
- A machine running Ubuntu 22.04
- Access to a terminal
We'll walk you through installing Node.js on Ubuntu 22.04 with the default repositories and NodeSource. Finally, you will learn how to use NVM to install a specific Node.js version.
Option 1: Installing Node.js Using Apt and NPM From the Default Repositories
Using the official distribution repository is an efficient way to install Node.js on Ubuntu. The default repository on an Ubuntu 18.04 or later system includes Node.js and Node Package Manager (NPM) packages.
This method, however, does not allow you to select which Node.js version to install. In other words, it may install an out-of-date version that is no longer supported.
Open up your terminal and enter the following command:
Step 1. Refresh the Apt cache to update and upgrade the repository:
sudo apt update && sudo apt upgrade
Step 2. After the update is complete, install Node.js by entering the following command:
sudo apt-get install nodejs
This prompt will show which version of Node.js will be installed from the default repository. Press Y
to proceed.
Step 3: Install npm (Optional)
If the package in the repositories suits your needs, this is all you need to do to get set up with Node.js. In most cases, you’ll also want to also install npm
, the Node.js package manager. You can do this by installing the npm
package with apt
:
sudo apt install npm
Step 4: Verify the installation
Finally, double-check the Node.js and NPM version numbers to ensure that it was successfully installed. After you enter the following commands, the command-line will return the installed version number:
node -v
npm -v
Assume you want to uninstall Node.js or NPM from your Linux system. Use the following commands if this is the case:
sudo apt remove nodejs
sudo apt remove npm
Option 2: How to Install Node.js With Apt Using NodeSource
You can use a PPA (personal package archive) maintained by NodeSource to install a different version of Node.js. These PPAs have more Node.js versions than the official Ubuntu repositories. As of this writing, Node.js v16 and v18 are available.
Open up your terminal and enter the following command:
Step 1. Refresh the Apt cache to update/upgrade the repository:
sudo apt update && sudo apt upgrade
Step 2. Install the Curl (Optional)
Skip this step if you have cURL installed already. Otherwise, enter the following command:
sudo apt-get install curl
Step 3. Install Node.js from Nodesource
Install the PPA first to gain access to its packages. Curl
the installation script for your preferred version from your home directory, making sure to replace 18.x
with your preferred version string (if different):
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
Again, Refresh the Apt cache to update the repository (This is an important step):
sudo apt update
Once you have the NodeSource repository, install Node.js:
sudo apt-get install nodejs
Use these two commands to query Node.js and NPM versions and confirm installation process was a success:
node -v
npm -v
Option 3: How to Install a Specific Version of Node.js Using NVM
Another flexible method of installing Node.js is to use nvm, the Node Version Manager. This piece of software allows you to simultaneously install and maintain multiple independent versions of Node.js and their associated Node packages.
Visit the project's GitHub page to install NVM on your Ubuntu 22.04 machine. Copy the curl
command from the README file that displays on the main page. The most recent version of the installation script will be downloaded.
Before piping the command to bash
, you should always audit the script to ensure it isn't doing anything you don't agree with. Remove the | bash
segment at the end of the curl
command to accomplish this:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh
Examine the script and make sure you're happy with the changes it's making. When you're finished, rerun the command with | bash
appended to the end. The URL you use will vary depending on the most recent version of nvm, but for the time being, the script can be downloaded and executed using the following:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
This will install the nvm
script to your user account. To use it, you must first source your .bashrc
file:
source ~/.bashrc
Now, you can ask NVM which versions of Node are available:
nvm list-remote
// Output
. . .
v18.0.0
v18.1.0
v18.2.0
v18.3.0
v18.4.0
v18.5.0
v18.6.0
v18.7.0
v18.8.0
v18.9.0
v18.9.1
v18.10.0
v18.11.0
v18.12.0 (LTS: Hydrogen)
v18.12.1 (LTS: Hydrogen)
v18.13.0 (Latest LTS: Hydrogen)
v19.0.0
v19.0.1
v19.1.0
v19.2.0
v19.3.0
v19.4.0
It’s a very long list. You can install a version of Node by writing in any of the release versions listed. For instance, to get version v14.10.0, you can run:
nvm install 19.1.0
You can view the different versions you have installed by listing them:
nvm list
Removing Node.js
You can uninstall Node.js using apt
or nvm
, depending on how it was installed. To remove the version from the system repositories, use apt remove
:
sudo apt remove nodejs
By default, apt remove
retains any local configuration files that were created since installation. If you don’t want to save the configuration files for later use, use apt purge
:
sudo apt purge nodejs
To uninstall a version of Node.js that you installed using nvm
, first determine whether it is the current active version:
nvm current
If the version you are targeting is not the current active version, you can run:
nvm uninstall node_version
This command will uninstall the selected version of Node.js.
If the version you would like to remove is the current active version, you first need to deactivate nvm
to enable your changes:
nvm deactivate
Now you can uninstall the current version using the uninstall
command used previously. This removes all files associated with the targeted version of Node.js.
Conclusion
That's all there is to it! You have now installed Node.js on your Ubuntu 22.04 system. Whether you're working on a tiny personal project or a large-scale online application, Node.js is a strong and adaptable platform that can assist you. Remember to maintain your system up to date and to double-check your installations to ensure everything is working properly.
For more information on Node.js, check out the official website at nodejs.org and the npm website at npmjs.com. Thanks for reading, and happy coding!