This question already has an answer here:

I'm having issues with npm in a Vagrant box I'm setting up. I noticed that the npm version is somewhat old, so I wanted to check the problem with the latest release.

It is my understanding that you should be able to update npm using npm install -g npm, but the command has no effect on the npm being used:

vagrant@box:~$ npm -v
1.3.10
vagrant@box:~$ sudo npm install -g npm
npm http GET https://registry.npmjs.org/npm
npm http 200 https://registry.npmjs.org/npm
npm http GET https://registry.npmjs.org/npm/-/npm-2.1.12.tgz
npm http 200 https://registry.npmjs.org/npm/-/npm-2.1.12.tgz
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
npm@2.1.12 /usr/local/lib/node_modules/npm
vagrant@box:~$ npm -v
1.3.10

I also tried using n (as suggested in How can I update my nodeJS to the latest version?) to update, but it affects neither npm nor node:

vagrant@box:~$ node -v
v0.10.25
vagrant@box:~$
vagrant@box:~$ sudo npm install -g n
/usr/bin/n -> /usr/lib/node_modules/n/bin/n
n@1.2.9 /usr/lib/node_modules/n
vagrant@box:~$ sudo n stable

     install : v0.10.33
       mkdir : /usr/local/n/versions/0.10.33
       fetch : http://nodejs.org/dist/v0.10.33/node-v0.10.33-linux-x64.tar.gz
   installed : v0.10.33

vagrant@box:~$ node -v
v0.10.25
vagrant@box:~$ npm -v
1.3.10

What do I have to do to update npm to the latest version?

marked as duplicate by Ciro Santilli 新疆改造中心 六四事件 法轮功, Eric Carvalho, karel, Charles Green, Videonauth Nov 20 '17 at 0:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Are you sure there is a newer version available? Because for me, 1.3.10 is the latest version available in official repo. – Registered User Dec 17 '14 at 9:52
  • @RegisteredUser: In my first output listing it even says 2.1.12, which is the latest release: github.com/npm/npm/releases – Der Hochstapler Dec 17 '14 at 9:53
  • Try, $ sudo npm install npm -g – vembutech Dec 17 '14 at 10:14
  • @vembutech: I did. Unless you're suggesting that the -g should be placed somewhere else on the command line. – Der Hochstapler Dec 17 '14 at 10:18
  • which npm is very helpful. Old npm was in /usr/local/bin and has priority above /usr/bin new one – vp_arth May 16 '16 at 18:08

10 Answers 10

up vote 130 down vote accepted

I still don't understand why, but I have to run npm install -g npm twice for it to have the desired effect:

vagrant@box:~$ npm -v
1.3.10
vagrant@box:~$ sudo npm install -g npm
npm http GET https://registry.npmjs.org/npm
npm http 200 https://registry.npmjs.org/npm
npm http GET https://registry.npmjs.org/npm/-/npm-2.1.12.tgz
npm http 200 https://registry.npmjs.org/npm/-/npm-2.1.12.tgz
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
npm@2.1.12 /usr/local/lib/node_modules/npm
vagrant@box:~$ npm -v
1.3.10
vagrant@box:~$ sudo npm install -g npm
/usr/bin/npm -> /usr/lib/node_modules/npm/bin/npm-cli.js
npm@2.1.12 /usr/lib/node_modules/npm
vagrant@box:~$ npm -v
2.1.12
  • 12
    If you recently reconfigured to stop sudo, as suggested here: justjs.com/posts/…, you may have altered your ~/.bashrc file. Thus, be sure to run source ~/.bashrc or else npm -v won't find the updated version. – modulitos Jan 29 '15 at 12:34
  • 9
    I just closed and reopened my shell instance to see the updated version. – absynce Apr 7 '15 at 15:13
  • 1
    For me it worked from the first time. – Daneel S. Yaitskov Jul 18 '16 at 7:44
  • I had to do this twice also in 14.04 before it was picked up. Wouldn't have guessed otherwise. Cheers – Dark Star1 Aug 26 '16 at 12:06
  • No idea, how I got there, but this might help someone: after you have installed nodejs as explained here https://nodejs.org/en/download/package-manager/, you have to restart your terminal and it will be there. You do not need to install npm separately! So you also won't need to install and update npm. – Ufos Nov 25 '16 at 12:39

You can update nodejs by using npm itself, a PPA, or manually.

npm:

Check the current version you have:

node -v

The following clears your cache.

sudo npm cache clean -f

Install n

sudo npm install -g n

You can tell it to install a specific version like so:

sudo n 0.8.11

Or just tell it to install the latest stable version. Both may take a while.

sudo n stable

To see if it actually upgraded, run:

node -v

PPA:

Other option is to install it via a PPA by chris-lea;

sudo add-apt-repository ppa:chris-lea/node.js  
sudo apt-get update  
sudo apt-get install nodejs

This PPA Supports the following distros: Utopic (14.10), Trusty (14.04), Saucy (13.10), Raring (13.04), Quantal (12.10), Precise (12.04), Oneiric (11.10), Natty (11.04), Lucid (10.04).

Manually:

You can always update it by manually downloading the latest version and installing it yourself!


Reference:

  • I know you're trying to help, but that is exactly what it says in my question. – Der Hochstapler Dec 17 '14 at 10:42
  • @OliverSalzburg , my bad, i saw the above post, and was to noisy for me to read. So, i just started posting, my own answer. – blade19899 Dec 17 '14 at 10:46
  • @OliverSalzburg, I updated my answer with multiple option on how to upgrade! – blade19899 Dec 17 '14 at 10:55
  • awesome, using n worked well for me (debian sid) – Michel Feldheim Aug 4 '15 at 13:55
  • This is obviously the correct answer. It annoys me when question posters answer the own question 'WRONGLY' then mark it as accepted. They should be penalized! – Twifty May 22 '17 at 15:53

When you first do npm install -g npm, the shell (Bash) will search for npm in your path, find /usr/bin/npm installed by the system package, and then use it to install the new version of npm. The new version will be installed in /usr/local/bin/npm.

Now, your path should have /usr/local/bin/ BEFORE /usr/bin/, so you would think it would now pick up the updated version in /usr/local/bin/, right? Wrong.

Bash will CACHE executable paths after the first time it searches for them, so when you say npm the 2nd time, it is still using the cached version which it first found as /usr/bin/npm.

To tell Bash to clear this cache and look through the path again, you have to do a hash -r.

After installing npm and doing this, my shell picked up the new version of npm just fine.

Thanks

  • this worked for me on 14.04 Thanks! -- prnt.sc/bybbao – Patoshi パトシ Jul 27 '16 at 19:45
  • This worked for me,Thank you. All other answer did not work. – rotoava Oct 6 '16 at 11:06
  • Perfect and clear answer it worked for me – mrroboaat Nov 1 '16 at 16:10
  • Windows 10 Bash 14.04 this worked for me after updating – mrpatg Dec 30 '16 at 7:41
  • 1
    hash -r save my ass. XD – slideshowp2 Jul 21 '17 at 9:21

Update NPM to latest version in one command

To upgrade or update the version of your npm, just type in terminal:

sudo npm install npm@latest -g

As mentioned in the footer of the NPM documentation

Most of the time I'm unable to upgrade it with the global command. What worked for me however is upgrading the package from the source of all the systems node-modules:

Find out where npm is installed and go into that folder

# Below command shows the destination (remove sed pipe to see the full path of npm-cli)
whereis npm | cut -c 6- | xargs readlink -f | sed 's/.\{19\}$//'

# Go in there and install it manually. In my case it was the folder below... 
# NOTE: on mac its in /usr/local/lib
cd /usr/lib
sudo npm install npm@latest

Behold the mighty one-liner for everybody (especially lazy people)

cd `whereis npm | cut -c 6- | xargs readlink -f | sed 's/.\{18\}$//'`; cd ..; sudo npm install npm@latest
  • This runs free of error for me (in contrast to other answers) however npm remains not updated – PandaWood Feb 18 '16 at 13:42
  • if you sudo it uses the npm folder of the root account, be aware of that. If you want to know where your npm is, simply type: whereis npm | xargs readlink -f. Maybe you just upgraded the wrong package there. I've added some info to my answer. – Tom Siwik Feb 18 '16 at 14:25

Any trick you do, don't use Git, I recommand running them in the Composer terminal. It'll surely work. It did for me by npm install npm -g.

To upgrade npm you need to update nodejs to the latest version which includes npm

In debian stretch and jessie use this script :

#install prerequisites
apt-get install apt-transport-https curl git lsb-release -y

#Install NodeJS from external repositories
DISTRO=$(lsb_release -c -s)
if [ "$DISTRO" == "stretch" ]
then
  DISTRO="jessie"
fi

if curl -f "https://deb.nodesource.com/node_7.x/dists/$DISTRO/Release" >/dev/null
then
  curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -
  echo "deb https://deb.nodesource.com/node_7.x $DISTRO main" > /etc/apt/sources.list.d/nodesource.list
  echo "deb-src https://deb.nodesource.com/node_7.x $DISTRO main" >> /etc/apt/sources.list.d/nodesource.list
  apt-get update
  # comment out the following line, if you installed nodejs 7 already (check with `apt-cache policy nodejs`)
  apt-get remove nodejs nodejs-legacy npm
  apt-get install nodejs -y
else
  echo -e "Your distribution is not supported by NodeJS. \nYou have to install a recent NodeJS version (>=4) manually. "
fi

In Ubuntu, you can use this script from https://deb.nodesource.com:

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

see the source first at https://github.com/nodesource/distributions/blob/master/deb/setup_7.x

upgrading to nodejs v0.12.7

 # Note the new setup script name for Node.js v0.12
 curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -

 # Then install with:
 sudo apt-get install -y nodejs

Source from nodesource.com

I was facing the problem. My current npm version was 3.3.12 but I tried sudo npm install npm -g, sudo npm update npm -g .. nothing worked.. while I npm --version I always get 3.3.12. I searched for directories in my Ubuntu 15.04 and found two version of npm in different directory.

  • v3.3.12 in /usr/local/lib/node_modules/npm
  • v3.6 in /usr/lib/node_modules/npm

So I did make a copy of 3.3.12 with mv npm npm_3312 while I was in older npm directory. Then I did sudo cp -r npm /usr/local/lib/node_modules/ while I was in '/usr/lib/node_modules' directory.. I made my npm --version and I got 3.6.0

:D

I don't see any reason to reinstall something that is ready there, just use update -g built into the package manager to update itself:

$ npm -v
2.15.1
$ sudo npm update -g npm
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
npm@3.10.9 /usr/local/lib/node_modules/npm
$ npm -v
3.10.9

Make sure that global flag -g is before the packages. I've had problems in the past with npm not registering the flag after the package list.

  • I had to restart the terminal for the change to reflect – inquisitive Nov 6 at 9:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.