In this tutorial, we’re going to show you how to install Node,js and npm on Ubuntu. There are different methods of installing npm and Node.js on Ubuntu, and we’re going to show you all of them.
Before we begin
These are the requirements:
- You’ll need an Ubuntu server. You can get one from Vultr or Linode. If you buy a managed server from SolaDrive, they will install and configure Node.js and npm for you for free.
- Root/sudo access to the server. You’ll get root access to the server if you get one from the 3 mentioned providers above.
There are 3 methods of installing Node.js and npm on Ubuntu:
- Installing newer versions of Node.js and npm through NodeSource
- Installing any specific Node.js and npm version with NVM
- Installing Node.js and npm through the default Ubuntu repository. This is not recommended. As of writing, the version of Node.js that’s included in the default Ubuntu repositories has reached its EOL.
Installing newer versions of Node.js and npm through NodeSource
You can use NodeSource’s repositories (PPA) to install newer and more versions of Node.js and npm.
Step 1: Update Ubuntu
Before starting with anything, update your system:
sudo apt-get update sudo apt-get upgrade
Step 2: Install the NodeSource PPA
To install the PPA, download and run the NodeSource script for the version you want to install. You can choose from multiple Node.js versions, including 17, 16, 14, and 16 LTS, or “current”. For this tutorial, we’ll use the “current” method. As of writing, the current version is v17.
Run the following command:
curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
If you want to install a different version, check the GitHub repo linked above and choose a different version.
After the script finishes, you can move onto installing Node.js.
Step 3: Install Node.js
To install Node.js (the version you chose from step 2) and npm together, run the following command:
sudo apt-get install nodejs
When using NodeSource, both nodejs and npm get installed with the command above.
You can verify what version is installed by running:
node -v
Installing any specific Node.js and npm version on Ubuntu with NVM
If you use the Node Version Manager (NVM), you can install any specific version of Node.js and npm. Follow the steps below:
Step 1: Update Ubuntu
The usual first step to every tutorial:
sudo apt-get update sudo apt-get upgrade
Step 2: Install NVM
Download the NVM installation script and run it with the following command:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
As of writing, the latest version of the script is 0.39.1, but there may be a newer one when you’re reading this, so check their repo.
After running the script, you probably will need to source the file by running the following command:
source ~/.bashrc
And now you can start using NVM.
You can install just about any Node.js version out there. To get the full list of versions, run:
nvm list-remote
Which will give you a huge list of available Node versions.
Step 3: Install Node.js and npm
For the purposes of this tutorial, we’ll install version v16.6.1 with NVM.
Run the following command:
nvm install v16.6.1
And check if it was installed with the following command:
node -v
Which should give you this output:
v16.6.1
npm is automatically installed along with Node.js if you’re using NVM.
(Not recommended) Installing Node.js and npm through the default Ubuntu repository
One of the ways of installing Nodejs and npm (arguably the easiest) is using Ubuntu’s default repositories. Here are the steps for that method:
Step 1: Update Ubuntu
As always, the first step is to update your system with the following commands:
sudo apt-get update sudo apt-get upgrade
Step 2: Install Node.js
Run the following command to install Node.js using Ubuntu’s default repository:
sudo apt-get install nodejs
To verify that it’s installed, run the following command:
node -v
Which should give you an output similar to:
v10.19.0
Step 3: Install npm
To install npm, run the following command:
sudo apt-get install npm
You can check if it’s installed and what version you’re using with:
npm -v
Which should give you an output like:
6.14.4
Since these versions are old and reached their EOL, you should use a different method of installing Node.js.