149

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?

Oliver Salzburg
  • 4,463
  • 3
  • 24
  • 33

10 Answers10

164

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
Oliver Salzburg
  • 4,463
  • 3
  • 24
  • 33
65

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

47

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:

blade19899
  • 26,994
22

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

12

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
Tom Siwik
  • 243
  • 2
  • 7
1

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.

1

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.

SmujMaiku
  • 111
  • 2
1

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

rubo77
  • 34,024
  • 52
  • 172
  • 299
0

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: Node.js v0.12, io.js, and the NodeSource Linux Repositories | nodesource.com

Rémi B.
  • 984
  • 2
  • 9
  • 17
0

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