How to Uninstall NodeJS in Ubuntu 22.04

nodejs and ubuntu logos on a green gradient background

Uninstalling NodeJS in Ubuntu can be ineffective if you don’t understand how programs get installed in Ubuntu. The confusion worsens when you are unsure whether to use the uninstall, remove, or purge commands.

This tutorial simplifies NodeJS uninstallation by explaining the workings of package managers, installation directories and the most suitable commands to apply. Lastly, it takes you through practical examples of uninstalling NodeJS in Ubuntu.

The Role of Package Managers in NodeJS installation and Uninstallation in Ubuntu

Installation files are distributed as packages. A package contains the program without its dependencies, which are installed separately. However, that should not worry you because a package manager does the work for you.

The two main types of package managers are dpkg and apt. dpkg is a low-level package manager. It can be less comfortable because you may need to trace packages in the downloader package.

sudo dpkg -i [package name]

-i option implies install.

You will likely get a dependency error since most Linux packages rely on others to function, yet dpkg only collects the specified package.

On the other hand, apt (Advanced Package Tool) finds the packages and downloads them or tells you the command to gather the missing dependencies.

sudo apt install [package]

apt searches for the packages in the list of repositories stored in the /etc/apt/sources.list or /etc/apt/sources.list.d. If the package URL is unavailable in the configuration file, you can collect them from the target package’s repository and add them to the sources list.

It is worth noting that the apt command has lower-level alternatives called apt-get and apt-cache. The key difference between apt and apt-get is that apt is more user-friendly than apt-get.

Notes/Articles/How to Uninstall NodeJS in Ubuntu/apt-get and apt-cache.png

Where Ubuntu Stores NodeJS binaries

According to the Filesystem Hierarchy Standards (FHS), the binaries of the applications you install are stored in the usr folder. The usr directory has subdirectories like bin, sbin, and local.

The bin subdirectory stores command binaries, for example, the essential commands like cat and ls. It also stores executables’ binaries.

The sbin subdirectory stores commands like adduser used by the system administrator. The local subdirectory mainly contains programs installed from the source code.

Options for Removing NodeJS Binaries

The three typical commands to remove installed binaries are uninstall, remove, and purge.

The uninstall command safely gets rid of a program and its associated files from the hard drive. Certain packages (of programming languages and runtime environments) rely on each other and can only be installed and uninstalled using a specific package manager.

For instance, a NodeJS script could need multiple packages and libraries to work. Its packages are installable through the NodeJS Package Manager (npm).

npm install express

npm gets installed with NodeJS. It caches information about NodeJS packages.

As a result, you may need to uninstall the package manager when uninstalling NodeJS in Ubuntu. Besides, you could install, run and uninstall NodeJS versions using the Node Version Manager (nvm).

remove is one of the safest commands.

sudo apt remove [package]

It deletes the package without tampering with the user data. So, you can reinstall the application and still trace your previous data.

Lastly, you should use the purge command to eliminate the package and its data.

sudo apt purge [package]bas

The key takeaway is that uninstall, remove, and purge all discard NodeJS and related programs.

The slight difference between the commands depends on your desired extent of file removal or mode of installation. For that reason, this tutorial presents you with multiple options to uninstall NodeJS in Ubuntu.

Step 1: Locate the Binaries

The first step toward uninstalling NodeJS in Ubuntu is finding the folder that houses its binaries. That will enable you to confirm whether NodeJS was uninstalled.

You can use the which or find command to locate NodeJS binaries.

which node
find $(echo $PATH | sed 's/:/ /g') -name "node"
Output
/usr/bin/node

Notes/Articles/How to Uninstall NodeJS in Ubuntu/locate node.png

Step 2: Uninstall NodeJS

After locating the node binaries, the next step is to remove them.

Option 1: Remove the Path

The most straightforward and less recommended way to discard NodeJS is manually deleting the node binaries.

sudo rm /usr/bin/node

Option 2: Uninstall with the apt or apt-get Command

apt and apt-get are the most familiar commands to uninstall NodeJS in Ubuntu.

sudo apt-get remove nodejs

Notes/Articles/How to Uninstall NodeJS in Ubuntu/use remove command.png

Here are their alternatives.

Option 3: Remove All Packages with the purge Command

The purge command clears all node-related packages, including those installed via nvm.

Let’s reinstall NodeJS.

Install node from source
sudo apt install nodejs

Confirm Installation

1. Check node and npm versions
node -v
npm -v
2. inspect the binaries
which node

Notes/Articles/How to Uninstall NodeJS in Ubuntu/nodejs available.png

Then, uninstall NodeJS using the purge command.

sudo apt-get purge nodejs

OR

sudo apt-get purge – auto-remove nodejs

Option 4: Uninstall a Particular NodeJS Version

Assume you only want to uninstall a specific NodeJS version. You can specify the version after the nvm command.

nvm uninstall [version]

e.g.

nvm uninstall 16.16.0

Notes/Articles/How to Uninstall NodeJS in Ubuntu/use nvm.png

Option 5: Remove the Source Lists

We can also remove the NodeJS information stored in the /etc/apt/sources.list.d folder.

First, confirm whether NodeJS data exist in the /etc/apt/sources.list.d directory.

ls /etc/apt/sources.list.d

Next, remove node from the sources.

rm -rf nodesource.list nodesource.list.distUpgrade

Lastly, update the repositories.

sudo apt-get update

Step 3: Clear the Cache

Uninstalling NodeJS sometimes leaves traces of packages and modules. You can clear the cache by removing npm.

Option 1: Uninstall npm

sudo apt-get remove npm

Option 2: Manually Remove npm

You can delete the hidden npm directory.

ls -la
rm -rf .npm

Or manually clear npm-related things.

rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/npm*

Here, we look into the files installed via source code in the /usr/local directory, then remove NodeJS-related data like npm and node_modules with their binaries.

Notes/Articles/How to Uninstall NodeJS in Ubuntu/clear cache.png

Conclusion

The most straightforward way to uninstall NodeJS in Ubuntu is to use the remove command.

However, since the remove command could leave some cache, it would help to clear the cache using the purge command or manually remove the hidden folders. You can also use the uninstall command with npm.

You can then confirm the uninstallation by checking the installation directories, as guided in this tutorial.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like