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?

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
  • 14
    If you recently reconfigured to stop sudo, as suggested here: http://justjs.com/posts/npm-link-developing-your-own-npm-modules-without-tears, 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
  • 16
    I just closed and reopened my shell instance to see the updated version. – absynce Apr 07 '15 at 15:13
  • 1
    For me it worked from the first time. – Daniil Iaitskov Jul 18 '16 at 07: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
  • @Ufos For me, I do need to install npm separately. What dist are you using? Thanks – user1032531 Nov 27 '16 at 18:08
  • @user1032531 xubuntu 14.04. I also thought I do have to, and tried to install it separately which only led to npm not working at all. Later I realized that after installing node I simply had to restart the terminal.

    For the installation I used curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - sudo apt-get install -y nodejs

    – Ufos Nov 28 '16 at 09:36
  • The reason for why it does not pick it up straight away is explained in my answer, below. TL;DR Bash caches the path of each command you use, so if you move it, you have to update it's cache. – Asfand Qazi Mar 24 '17 at 16:01
  • sudo npm upgrade -g npm, i used and also solved my problem – sunil Feb 22 '18 at 13:03
  • You should never use sudo with npm -g, instead configure your permissions correctly. – Dimitri Kopriwa Apr 24 '18 at 08:05
  • 1
    running: npm i -g npm instead of npm install -g npm seems to take effect from the first time. – Miloud Eloumri Jun 16 '19 at 01:47
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

46

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,704
  • I know you're trying to help, but that is exactly what it says in my question. – Oliver Salzburg 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
  • 1
    awesome, using n worked well for me (debian sid) – Michel Feldheim Aug 04 '15 at 13:55
  • 1
    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
  • This worked for me after having tried a various approaches. I had to start a new session to refresh everything because node -v was throwing an error, but on starting a new terminal it worked with the updated version – Vass Mar 26 '21 at 01:10
  • This is still an issue somehow, 9 years later. Way harder updating npm than it should be – 15Step Mar 20 '23 at 18:53
23

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
  • 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
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
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
  • 32,486
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.
  • 944
  • 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