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.

They provide two variants you can use:

  • The latest version of Node.js
  • The long-term support (LTS) version

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 to get 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 v19.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.

Download the set up script provided by Node.js developers and run it instantly with:

curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash

The script takes care of adding the node repository to your system, its signing key to your keyring and updating the package cache.

Installing node.js on Ubuntu

Since everything else is taken care of, you just need to install nodejs package now.

sudo apt install -y nodejs

You can verify that Node is installed on your system with:

node --version

It should show the version of currently installed Node:

[email protected]:~$ node --version
v19.0.0

Install the LTS version of Node.js

This is recommended to most users as it gets the job done with enhanced stability.

The steps remain the same mostly. You just have to download a different script this time.

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash

Now, you can proceed with the installation by the given command:

sudo apt-get install -y nodejs

Check the node version to ensure that it is installed correctly.

node --version
[email protected]:~$ node --version
v18.12.0

Yep, it is one iteration behind the most recent version but stability is the key factor here.

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.

[email protected]:~$ node --version
bash: /usr/bin/node: No such file or directory

And it should give you similar results after the removal process.

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
[email protected]:~$ sudo snap install node --classic
node (18/stable) 18.12.0 from OpenJS Foundation (iojs**) installed

And as you can see, it gave me Node.js 18.12!

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
[email protected]:~$ sudo snap install node --channel=latest/edge --classic
node (edge) 20.0.0-nightly20221101590cf569 from OpenJS Foundation (iojs**) installed

And it got me a nightly build running Node.js 20.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.

Linux Foundation Training