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
[email protected] /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
[email protected] /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?

6
  • Are you sure there is a newer version available? Because for me, 1.3.10 is the latest version available in official repo. Commented Dec 17, 2014 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 Commented Dec 17, 2014 at 9:53
  • Try, $ sudo npm install npm -g
    – BDRSuite
    Commented Dec 17, 2014 at 10:14
  • @vembutech: I did. Unless you're suggesting that the -g should be placed somewhere else on the command line. Commented Dec 17, 2014 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
    Commented May 16, 2016 at 18:08

10 Answers 10

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
[email protected] /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
[email protected] /usr/lib/node_modules/npm
vagrant@box:~$ npm -v
2.1.12
11
  • 14
    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
    Commented Jan 29, 2015 at 12:34
  • 16
    I just closed and reopened my shell instance to see the updated version.
    – absynce
    Commented Apr 7, 2015 at 15:13
  • 1
    For me it worked from the first time. Commented Jul 18, 2016 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
    Commented Aug 26, 2016 at 12:06
  • 1
    running: npm i -g npm instead of npm install -g npm seems to take effect from the first time. Commented Jun 16, 2019 at 1: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

8
  • this worked for me on 14.04 Thanks! -- prnt.sc/bybbao Commented Jul 27, 2016 at 19:45
  • This worked for me,Thank you. All other answer did not work.
    – Amalaric
    Commented Oct 6, 2016 at 11:06
  • Perfect and clear answer it worked for me
    – mrroboaat
    Commented Nov 1, 2016 at 16:10
  • Windows 10 Bash 14.04 this worked for me after updating
    – mrpatg
    Commented Dec 30, 2016 at 7:41
  • 4
    hash -r save my ass. XD
    – Lin Du
    Commented Jul 21, 2017 at 9:21
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:

7
  • I know you're trying to help, but that is exactly what it says in my question. Commented Dec 17, 2014 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
    Commented Dec 17, 2014 at 10:46
  • @OliverSalzburg, I updated my answer with multiple option on how to upgrade!
    – blade19899
    Commented Dec 17, 2014 at 10:55
  • 1
    awesome, using n worked well for me (debian sid) Commented Aug 4, 2015 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
    Commented May 22, 2017 at 15: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

2
  • I did this but EC Ubuntu is still showing older version of npm, how to fix it ?
    – vikramvi
    Commented Oct 1, 2019 at 7:10
  • Just relog to your account.
    – Mr. Panda
    Commented Oct 20, 2021 at 11:55
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
2
  • This runs free of error for me (in contrast to other answers) however npm remains not updated
    – PandaWood
    Commented Feb 18, 2016 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
    Commented Feb 18, 2016 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
[email protected] /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.

1
  • I had to restart the terminal for the change to reflect Commented Nov 6, 2018 at 9:56
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

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

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

Not the answer you're looking for? Browse other questions tagged .