Resolving Node.js 18 Installation Installs Node.js 12 on Ubuntu

Resolving Node.js 18 Installation Installs Node.js 12 on Ubuntu

Overview

When attempting to install Node.js version 18 on Ubuntu, some users encounter a situation where the system installs Node.js v12 instead.
This typically happens due to outdated repositories, cached package sources, or incorrect repository configurations that still point to older Node.js sources.

This article explains the root causes, symptoms, and step-by-step resolution methods to ensure the correct version of Node.js is installed.


Issue Description

Symptom:
When you run:

      curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
      sudo apt-get install -y nodejs

and then check the installed version:

      node -v

You see: v12.x.x instead of the expected v18.x.x.


Possible Causes

Category Root Cause Explanation
Old Repository Cache The system’s package list still references older Node.js repositories or cached metadata. Ubuntu’s APT sometimes retains older metadata, causing apt to fetch packages from outdated sources.
Duplicate Node.js Repositories Multiple NodeSource entries exist in /etc/apt/sources.list.d/. Having both setup_12.x and setup_18.x repositories can cause the package manager to select the older version.
Ubuntu Default Repository The official Ubuntu repo includes Node.js 12 by default. If NodeSource is not configured correctly, apt falls back to Ubuntu’s own package — which is version 12.
Proxy or Mirror Interference Corporate proxies or mirrors caching old packages. Cached responses from an internal mirror or proxy may serve outdated Node.js binaries.
Broken or Incomplete Setup Script Execution The NodeSource setup script did not complete due to a “broken pipe” or partial download. If the script didn’t update APT sources correctly, the system reverts to the older repo.

Resolution Steps

Step 1 — Verify the Installed Version

Check the current Node.js version:

      node -v

If it shows v12.x, proceed with cleanup.


Step 2 — Remove Existing Node.js and Old Repositories

Remove the existing Node.js installation and old repository entries:

      sudo apt-get remove -y nodejs
      sudo rm /etc/apt/sources.list.d/nodesource.list

Also clean apt cache:

      sudo apt-get clean
      sudo apt-get autoremove -y
      sudo apt-get update

Step 3 — Add the Correct Node.js 18 Repository

Re-add the NodeSource setup for Node.js 18:

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

      🔹 Tip: Use --retry 3 for unstable network environments:

      curl --retry 3 -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

Step 4 — Install Node.js 18

Now install Node.js:

      sudo apt-get install -y nodejs

Then verify:

      node -v
      npm -v

Expected output:

      v18.x.x
      8.x.x

Step 5 — Lock to the Correct Version (Optional)

To prevent downgrades in future updates:

      sudo apt-mark hold nodejs

Alternative Installation (Using NVM)

If the above steps don’t work or you prefer version control flexibility, install via Node Version Manager (NVM):

      curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
      source ~/.bashrc
      nvm install 18
      nvm use 18

Then confirm:

      node -v

Verification Checklist

Check Command Expected Result
Repository file cat /etc/apt/sources.list.d/nodesource.list Should show deb.nodesource.com/node_18.x
Node version node -v Should show version 18.x
APT source priority apt-cache policy nodejs Top priority should be deb.nodesource.com