156

I'm trying to install mean.io boilerplate. It fails when running sudo npm install -g meanio@latest. Prior to failing it notes that it 'wants' npm version 1.4.x, whereas I have 1.2.18 installed. So I tried updating npm to the latest; several ways. Last of which was...

ubuntu@ip-xxx-xx-xx-xxx:~$ sudo npm install -g npm
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
npm@1.4.7 /usr/local/lib/node_modules/npm

ubuntu@ip-xxx-xx-xx-xxx:~$ npm --version
1.2.18

Why is it still showing version 1.2.18, when I just updated to 1.4.7?

Dharman
  • 30,962
  • 25
  • 85
  • 135
user3567174
  • 1,898
  • 2
  • 15
  • 18

22 Answers22

240

This will update npm using npm itself:

sudo npm install npm -g

If you are stuck, try sudo npm update npm -g. All credit goes to Tim Castelijns. I have tested it on ubuntu 14.04, npm 1.3.10


Note that if you are using nvm for managing multiple versions in your local dev environment for e.g. testing purposes, all your installed versions (listed by nvm ls) are in ~/.nvm, hence you just omit system wide installation (i.e. omit sudo):

npm install npm -g

DEBIAN ENTERPRISE

For full enterprise practice consider nodesource.com:

curl -sL https://deb.nodesource.com/setup | sudo bash -

as described here.

OTHER ENTERPRISE

For non-debian distributions check out on the node github wiki https://github.com/joyent/node/wiki/installing-node.js-via-package-manager download page https://nodejs.org/en/download/

For historical understanding: Chis Lea was maintaining his PPA but now joined forces with nodesource.

Andre Miras
  • 3,580
  • 44
  • 47
Yauhen Yakimovich
  • 13,635
  • 8
  • 60
  • 67
  • I'd recommend using a PPA repository so you let ubuntu package manager maintain the updates along with everything else. – henry74 Feb 05 '15 at 16:21
  • On sort of production servers, yes, maybe. But on dev you really want to guarantee you get stuff directly from the npm package maintainer, not the PPA maintainer – Yauhen Yakimovich Feb 06 '15 at 15:28
  • I disagree. You don't want dev to be different from production or you will run into deployment issues for different versions. – henry74 Feb 06 '15 at 19:17
  • 1
    Again depending on how paranoid you are, trusting Chis Lea's PPA might be questionable. There is always a decision possible on how much of the critical dependencies in the project one just offloads as a local copy. If you are not working in the bank but doing an open source project you might not even have a production to worry about :) I will link the node wiki here just listing all the alternatives. Personally I am not a big of PPA vs. github tags when it comes to code distribution. – Yauhen Yakimovich Feb 08 '15 at 22:56
  • Don't need to use Chris's PPA. Use the official one from node source. – henry74 Feb 09 '15 at 02:10
  • ok. check my last edit. Please feel free to fix it if required ;) – Yauhen Yakimovich Feb 09 '15 at 09:12
  • Apparently https://github.com/nvm-sh/nvm is a great option to manage multiple node versions. – Yauhen Yakimovich Feb 04 '23 at 18:19
52

if user3223763's answer doesn't works, you can try this:

sudo apt-get remove nodejs ^node-* nodejs-*
sudo apt-get autoremove
sudo apt-get clean
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install nodejs

Then :

curl https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | sh

After this, open a new terminal and check the npm version:

npm --version

EDIT / UPDATE :

Today the last nvm version is :

https://raw.githubusercontent.com/creationix/nvm/v0.25.4/install.sh

Thus the CURL command is: v0.25.4 instead of v0.13.1

curl https://raw.githubusercontent.com/creationix/nvm/v0.25.4/install.sh | sh

You can check https://github.com/creationix/nvm/releases to use the correct version for further upgrades

Daniel Loureiro
  • 4,595
  • 34
  • 48
  • 3
    user3223763 didn't work for me, neither did self updating npm. Thank you Daniel – hectorg87 Sep 30 '14 at 18:49
  • 3
    I can confirm this method helped update npm. Previous methods had libraries unmet dependencies or would update npm but would not update the path/environment to use the new location. – will824 Jun 10 '15 at 17:47
  • This also worked for me, the other answers did not on Ubuntu 14.0.4 – islanddave Jul 07 '16 at 15:12
  • 1
    This worked for me using Ubuntu 14.04 installed on a Chromebook with Crouton. Nothing else I'd found online worked – Niall Nov 22 '16 at 19:29
  • Does npm requires nvm for instalation? – alper May 12 '21 at 14:21
27

don't forget to close and start the terminal window again ;)

(at least if you want to check "npm --version" in the terminal)

sudo npm install npm -g

that did the trick for me, too

24

Tried the options above on Ubuntu 14.04, but they would constantly produce this error:

npm ERR! tar pack Error reading /root/tmp/npm-15864/1465947804069-0.4854120113886893/package

Then found this solution online:

1) Clean the cache of npm first:

sudo npm cache clean -f

2) Install n module of npm:

sudo npm install -g n

3) Begin the installation by selecting the version of node to install: stable or latest, we will use stable here:

sudo n stable

4) Check the version of node:

node -v

5) Check the version of npm:

npm -v
Kostyantyn
  • 5,041
  • 3
  • 34
  • 30
16

Looks like you are using Ubuntu (from the command line you show). To update npm and nodejs you can use the PPA repository:

sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs npm
Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
user3723763
  • 366
  • 2
  • 9
  • 3
    `E: The repository 'http://ppa.launchpad.net/chris-lea/node.js/ubuntu bionic Release' does not have a Release file. N: Updating from such a repository can't be done securely, and is therefore disabled by default. N: See apt-secure(8) manpage for repository creation and user configuration details. ` for Ubuntu 18.04 – fIwJlxSzApHEZIl Jun 28 '18 at 21:05
  • 1
    Err:11 http://ppa.launchpad.net/chris-lea/node.js/ubuntu bionic Release 404 Not Found [IP: 91.189.95.83 80] – Kiran Reddy Oct 14 '18 at 02:11
  • 1
    to get rid of the annoying error run : rm /etc/apt/sources.list.d/cooperjona-ubuntu-nitrotasks-bionic.list – Abdelghani AINOUSS Oct 24 '18 at 21:05
11

To get the latest stable version just run

npm install npm@latest -g

It worked just fine for me!

Andre Paschoal
  • 332
  • 2
  • 7
6

You can try:

sudo npm i -g npm
Pang
  • 9,564
  • 146
  • 81
  • 122
jitendra rajput
  • 612
  • 10
  • 15
5

You can use npm package manager:

npm install npm@latest

This installs npm using itself @ latest version.

Community
  • 1
  • 1
Justin Liu
  • 581
  • 6
  • 21
3

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

Rémi Becheras
  • 14,902
  • 14
  • 51
  • 81
3

For me It worked with following commands

  1. $curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
  2. $apt install nodejs

Refer : https://linuxhint.com/how-to-update-npm-packages/

3

NPM was returning the old version after running $ sudo npm install npm -g.

Restarting the terminal (i.e. close and open again) fixed the issue for me and $ npm --version began returning the expected version.

* @Rimian mentions the need to reload the terminal in a comment of another answer.

Daniel
  • 2,380
  • 29
  • 44
3

Very simple

For Linux or Mac

sudo npm install npm@latest -g

For Windows: Run your command prompt as administrator and run the command below

npm install npm@latest -g

MD SHAYON
  • 7,001
  • 45
  • 38
2

Check your node version node -v and your npm version npm -v Then To update your npm, type this into your terminal : sudo npm install npm@latest -g

N.B: Debian Based OS{ubuntu or Linux mint}

2
sudo npm install npm@latest -g

This worked for me in ubuntu 18.04

JL0PD
  • 3,698
  • 2
  • 15
  • 23
1

In case you want to update npm to a specific version, you can use this :

npm install npm@version-number

Hardie
  • 33
  • 5
1
npm i -g npm

      Update available 4.5.0 → 4.6.1    │
   │     Run npm i -g npm to update 

this is what npm recommends when in terminal, second piece is directly copied and pasted from my terminal

note: im using a mac

  • The question is specifically about ubuntu, not osx. If I tried your command in ubuntu, `npm` will fails with `Error: Cannot find module 'process-nextick-args'`. You should remove your answer. – Anthony Kong Nov 03 '17 at 01:16
1

Check your node version node -v and your npm version npm -v Then To update your npm, type this into your terminal: npm install npm@latest -g

Hope I could help. Regards

Nicollas
  • 250
  • 2
  • 9
0

This is what worked for me on ubuntu curl -L https://www.npmjs.com/install.sh | sh

Sheshnath
  • 301
  • 3
  • 10
0
nvm install-latest-npm

if you happen to use nvm

muenalan
  • 588
  • 4
  • 8
0
  1. Clean the cache of npm first:

    sudo npm cache clean -f

  2. Install n module of npm:

    sudo npm install -g n

  3. Begin the installation by selecting the version of node to install: stable or latest, we will use stable here:

    sudo n stable

  4. Check the version of the node:

    node -v

  5. Check the version of npm:

    npm -v

I hope both the node and npm version is updated now. If it's not updated then please restart your machine and check it again :)

  • why do folks use the "n" package, when all you have to do is `npm install npm@latest` ? what's the advantage ? – Fattie Aug 14 '22 at 18:00
0

Updating npm and node.js

You may get latest versions from snap store

sudo apt remove nodejs ^node-* nodejs-*
snap install node --classic

This on Ubuntu 20.04.6 LTS gave me

node -v  ==> v18.17.1
npm -v   ==> 9.6.7

Note that the latest npm version may also need a newer node.js version. Also, running on Ubuntu LTS version vs the newest release also may limit the node/npm versions that run on your setup.

elomage
  • 4,334
  • 2
  • 27
  • 23
-2

For Ubuntu 18.04

npm install latest-version

I hope its for you also

user2642281
  • 125
  • 1
  • 4