How to Install Node.js on Ubuntu
Learn various ways of installing Node.js on Ubuntu, be it the recent version or the LTS version.
Looking to get Node.js on your Ubuntu system? You can get it directly from the developers of Node.js.
Since you'll be adding their repository to the system, you'll also get updates on the installed Node.js version directly from the source.
You can also use Snap for installing Node on Ubuntu. I'll discuss that method in the later sections of this article.
Sounds good? Make up your mind about the Node version you want to install and follow the instructions below.
Oh, wait! You need to make sure that curl is installed on your Ubuntu system.
sudo apt install curl
You'll be using it for downloading a bash installer script provided by Node developers.
Install the latest version of Node.js
While writing, Node.js v20.x is the most recent (stable) release, but the given instruction should remain the same as they will fetch the most recent release rather than pointing to the specific one.
Let’s say you want to install Node.js v20. First, go to the NodeSource repo and check the supported version for your distribution.
You need to install some more dependencies, like ca-cerificates and GnuPG before setting up Node.js. Open a terminal and run:
sudo apt update
sudo apt install -y ca-certificates gnupg
Create a keyrings
directory and add NodeSource GPG key.
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
Now, create the sources file. For this, you need to specify the version you need. In this example, I will be using Node v.20.
NODE_MAJOR=20
Now, in the same session, run:
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
Update your system and install Node.js.
sudo apt update
sudo apt install nodejs -y
You can verify that Node is installed on your system with:
nodejs --version
It should show the version of currently installed Node:
sagar@Learnubuntu:~$ nodejs --version
v20.6.0
Uninstalling Node from Ubuntu
Whether you went with LTS or non-LTS, the uninstallation method remains the same and only needs a single command:
sudo apt purge --auto-remove nodejs
You can always check for the installed version to check whether you successfully removed Node.js or not.
sagar@Learnubuntu:~$ nodejs --version
bash: /usr/bin/node: No such file or directory
And it should give you similar results after the removal process.
Now, remove the GPG key and the sources.list file added at the time of installation.
sudo rm -r /etc/apt/sources.list.d/nodesource.list
sudo rm -r /etc/apt/keyrings/nodesource.gpg
Those who installed Node.js through the installation script should migrate to the new repository. This is because, the installation script method is being deprecated, and you won't receive any updates there. To migrate, first remove the repository and keyring of the old method:
sudo rm /etc/apt/keyrings/nodesource.gpg
sudo rm /usr/share/keyrings/nodesource.gpg
sudo rm /etc/apt/sources.list.d/nodesource.list
Now, use the commands one by one:
NODE_MAJOR=18
sudo apt update
sudo apt install -y ca-certificates curl gnupg
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
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
Update the system.
sudo apt update
sudo apt install nodejs
That's it. You are now moved to the supported method.
Alternative: Install Node.js in Ubuntu using snap
Snaps come pre-configured in Ubuntu and you can install Node.js as a snap package and also allows you to choose between stable and edge releases. So let's start with installing the stable release:
Install stable release of Node.js using snap
As always, it only requires single command when installing snap packages and the same goes for Node.js:
sudo snap install node --classic
sagar@Learnubuntu:~$ sudo snap install node --classic
node (18/stable) 18.17.1 from OpenJS Foundation (iojs**) installed
And as you can see, it gave me Node.js 18.17.1!
Install the most recent build of Node.js
I will not recommend this build to the general audience as it might have bugs and stability issues. To install the bleeding edge version of Node.js, you just have to use the given command and it will utilize the edge channel:
sudo snap install node --channel=latest/edge --classic
sagar@Learnubuntu:~$ sudo snap install node --channel=latest/edge --classic
node (edge) 21.0.0-nightly202309048dfe4248 from OpenJS Foundation (iojs**) installed
And it got me a nightly build running Node.js 21.0!
Uninstall Node in snap format
Whether you went with the bleeding edge version or the stable, the command will remain the same for the uninstallation process:
sudo snap remove node
That's it! Enjoy using Node.js on Ubuntu.
A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.