How To Install Node.js And Npm On Ubuntu 21.04
1- From the Standard Ubuntu Repositories
2- From the NodeSource Repository.
3- Using Node Version Manager (nvm)
At the end of this tutorial, you can use the taught instructions for any Ubuntu-based distribution, including Kubuntu, Linux Mint, and Elementary OS.
Prerequisites to Install Node.js and Npm on Ubuntu 21.04
To let this tutorial work better, please consider the below Prerequisites:
_ A non-root user with Sudo privileges.
_ To seup, follow our Initial server setup on Ubuntu 21.04.
Install Node.js and Npm from Ubuntu Repositories
This is the first and the easiest way to install Node.js and Npm on Ubuntu 21.04. So, you can expect a straightforward installation.
Use the following command to update the package index and install Node.js and Npm:
sudo apt update
sudo apt install nodejs npm
In this way, you will install a number of packages, including the tools necessary to compile and install native addons from Npm. When you are finished, run the command below to verify the installation:
Install Node.js and Npm from NodeSource
If you want to install a different Node.js version than the one provided in the Ubuntu repositories, you can use this method. Currently, NodeSource supports Node.js v14, v13, v12, and v10.
NodeSource maintains an APT repository containing multiple Node.js versions. If your application requires a specific version of Node.js, use this repository. At the time of writing, the NodeSource repository provides v14, v13, v12, and v10 versions.
Here, you will install Node.js version 14.x. So, use the following command to download and execute the NodeSource installation script:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
In this way, the NodeSource signing key will be added to your system. The script will also create an apt repository file, install all necessary packages, and refresh the apt-cache.
Note: To change the version of Node.js to another considered one (for example 12x,) you just need to change the setup_14.x with setup_12.x.
When the NodeSource repository is enabled, run the command below to install Node.js and Npm:
sudo apt install nodejs
Note: The Node.js package contains both the node and npm binaries.
Then, you can verify if the Node.js and Npm were successfully installed or not. To do this, run:
Use the following command to install the development tools, it allows you to compile native addons from npm:
sudo apt install build-essential
Install Node.js and Npm using NVM
The third and last way of installing Node.js and Npm is to use Node Version Manager. If you need to have multiple Node.js versions installed on the same machine, this method is what you must use. Also, if you are a Node.js developer, you are recommended to choose this way of installation.
NVM is a bash script. You will use this method to manage multiple Node.js versions on a per-user basis. Also, you will be able to install and uninstall any Node.js version that you wish to use or test.
To download and install the nvm script, go to the nvm GitHub repository page and copy either the curl or wget command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
Note: Since Sudo enables nvm for the root user, do not use it.
The script will clone the project’s repository from Github to the ~/.nvm directory.
Note: In the output, you will be asked to close and reopen your terminal or run the commands to add the path to the nvm script to the current shell session. Choose which one is easier for you.
When the script is in your PATH, run the command below to verify that nvm was properly installed:
Use the command below to get a list of all Node.js versions that can be installed with nvm:
In this way, it will print a huge list of all available Node.js versions.
Now, run the following command to install the latest available version of Node.js, run:
nvm install node
After a successful installation, run the command below to verify it by printing the Node.js version:
In this article, you learned How to install Node.js And Npm On Ubuntu 21.04. There are three different ways to reach this purpose and you reviewed all of them. Due to the explanation of each section, you can choose which one is appropriate for your environment. But if you are not sure which Node.js version to install, consult the documentation of the application you’re going to deploy.