NPM is incorrect version on latest Ubuntu (18.04) installation
Solution 1
TLDR: This problem is caused by Bash caching the path of the npm
command, and can be solved by hash -d npm
. You don't even need to deal with apt purge
unless you want to.
Explanation
Here were my steps for getting a new npm
version on Ubuntu. First, do the installation as OP describes:
$ sudo apt-get install npm
(...apt installation of npm was successful...)
$ npm -v
3.5.2
$ command -v npm
/usr/bin/npm
$ sudo npm install -g npm
(...npm installation of npm was successful...so far, so good)
You can see that the new version is already working fine in /usr/local/bin/npm
, but unfortunately the Bash cache still has /usr/bin/npm
:
$ /usr/local/bin/npm -v
6.4.1
$ npm -v
3.5.2
$ command -v npm
/usr/bin/npm
$ type npm
npm is hashed (/usr/bin/npm)
To fix the problem, clear it from the Bash cache (do this in all open shells):
$ hash -d npm
Now the new version works as desired:
$ npm -v
6.4.1
$ command -v npm
/usr/local/bin/npm
Solution 2
The way I found is to purge npm through sudo apt purge npm
, then simply recreate a symlink to the global installation via ln -s /usr/local/bin/npm /usr/bin/npm
. After that fix, npm -v
returns 6.0.1
as expected.
Solution 3
To have control on installed npm version, I always use nvm
(node version control). You can install it through the instructions here: https://github.com/creationix/nvm
Then by following command install the latest npm on your computer:
nvm install node
Related videos on Youtube
Blairg23
I am a software engineer who loves Python, automation, data analytics, data visualization, intelligence gathering, simulations, web application programming, solving advanced puzzles, and learning new things every day! Some of my favorite graduate projects have been modeling populations (epidemics, paramecium, and whales), physics simulations (Per Bak self-organized criticality, ice sheets, bullet trajectories, and particle interactions), computational biology (deterministic) modeling, and artificial intelligence (machine learning, pattern recognition, and data-mining). I completed a graduate portfolio as part of my Master's graduate requirements back in 2014. I can't believe it's been 5 years since I last updated this spiel and I still haven't had time to update my portfolio! I just stay too busy working! For the last couple years (2017-2019), I've been developing a web application for TOMIS (https://tomis.tech) and managing the engineering team, DevOps, and Quality Assurance. Built in Python/Django on the backend and React.js on the frontend, the goal was to automate marketing tasks; provide an aggregated visualization of analytics, seo, email and social campaigns, and reservation booking data; and provide beautiful monthly reports. The engineering involved automating distributed tasks (Celery, Redis), custom data ETL workflows, 3rd party API integrations (Google APIs, reservation systems, WebCEO, Twilio SMS/SendGrid, and Mailchimp), project management (Atlassian Jira, Confluence, and Bitbucket), DevOps (Pipelines, GCP), SRE/APM (DataDog, Honeycomb, Stackdriver), and handling incident comms and post-mortems to end-users (Statuspage.io). My non-technical hobbies include photography, snowboarding, hunting, fishing, hiking, swimming, floating, biking/longboarding with my three dogs, and exploring Montana. I also enjoy anything that involves strategy and is thought-provoking. I love Super Smash Bros., chess, cribbage, poker and a ton of other card and board games!
Updated on September 18, 2022Comments
-
Blairg23 over 1 year
Normal installation would be
sudo apt install nodejs
to install Node.js and thensudo apt install npm
to install Node Package Manager. However, upon doing so,npm -v
says3.5.2
. To upgrade normally, I would dosudo npm install -g npm
, which updates to the latest version (which, at the time of writing this article, is6.0.1
).When I do a
which npm
, I get/usr/local/bin/npm
, howeverapt
installs a symlink at/usr/bin/npm
. If Isudo apt purge npm
to remove npm, it still leaves the npm version of npm at/usr/local/bin/npm
, howevernpm -v
says-bash: /usr/bin/npm: No such file or directory
.Many articles say to use a PPA to install nodejs, but I think there should be a native way to do this through
apt
.DigitalOcean instructions on installation normally and through PPA: https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-18-04
TecAdmin instructions on installation through PPA: https://tecadmin.net/install-latest-nodejs-npm-on-ubuntu/
-
l3l_aze almost 6 yearsWorked with Ubuntu 18.04 running through UserLAnd on Android 6.0.1. Thank you! :)
-
Robert Munn over 5 yearsOP you should mark this as the answer.
-
LordAnomander about 5 yearsWeird thing, for me it was quite different. I had the updated version in
/usr/bin/npm
, so I went the opposite route usingln -s /usr/bin/npm /usr/local/bin/npm
. Strange, but your answer helped me to find out how to deal with the incorrect version issue. -
Blairg23 about 5 yearsI actually like this answer better than my own.
-
temporary_user_name over 4 yearsThis solved my problem. Thank you so much. Great explanation, too. The one thing that's unclear to me is the exact nature of why clearing the bash cache fixes the problem. Wouldn't it be better to delete the
npm
stored at/usr/bin/npm
? And why is the right version picked up after clearing the bash cache, if both versions remain installed? -
krubo almost 4 years@temporary_user_name You can see the path using
echo $PATH
, which shows that/usr/local/bin
will be preferred before/usr/bin
if both targets exist when Bash looks through the path. Bash looks through the path only if it doesn't remember where it found the target before. -
RyanNerd over 3 yearsLifesaver! Thx. I was stuck trying to do a production build for hours. This solved the issue.
-
Tom Bird over 3 yearsAll I had to do was
hash -d npm
then everything was running!!! Thanks alot!!!