Installing/upgrading npm

How do I install npm itself, or upgrade from an existing install?

1 Like
  • The preferred way of installing a fresh version of npm is to use the node.js installer itself.

  • The preferred way of upgrading npm is to use npm i -g npm@latest.

  • [Windows] Upgrading npm on Windows can fail due to issues with the node.js installer. You can use npm-windows-upgrade to try to bypass these issues.

  • [Linux/OSX] You can install or upgrade npm manually using curl http://npmjs.org/install.sh | sh.

The npm CLI team does not officially support installation through OS package managers like apt-get or Homebrew. You’re kind of on your own if you use one of those or some other method for installing or upgrading npm itself.

3 Likes

As of today, curl http://npmjs.org/install.sh returns a 301 which curl by default does not follow. Instead, use

  • curl -L http://npmjs.org/install.sh
    or
  • curl https://www.npmjs.com/install.sh

PS: actually, I posted prematurely, no luck even with the correct URL/curl options. Running the advertised command (on a contemporary Linux installation) results in:

npm ERR! path /usr/local/bin/npm
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall unlink
npm ERR! Error: EACCES: permission denied, unlink '/usr/local/bin/npm'
npm ERR!  { [Error: EACCES: permission denied, unlink '/usr/local/bin/npm']
npm ERR!   cause:
npm ERR!    { Error: EACCES: permission denied, unlink '/usr/local/bin/npm'
npm ERR!      errno: -13,
npm ERR!      code: 'EACCES',
npm ERR!      syscall: 'unlink',
npm ERR!      path: '/usr/local/bin/npm' },
npm ERR!   stack:
npm ERR!    "Error: EACCES: permission denied, unlink '/usr/local/bin/npm'",
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'unlink',
npm ERR!   path: '/usr/local/bin/npm' }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).

The only supported install option for npm for Linux tries to write to /usr/local, which according to the relevant Filesystem Hierarchy Standard 3.0 is “for use by the system administrator when installing software locally,” yet “running the command again as root/Administrator” is “not recommended.”

This reads like a Catch-22, but I am aware that the original author of npm recommends (or at least at some point recommended) to take ownership of /usr/local, however, this appears to violate the letter and spirit of the FHS governing Linux.

There are alternatives such as node version managers, choosing a different prefix, etc. that can reliably install node/npm for the current user — but wouldn’t it be better if node.js/npm worked out of the box with at least one set of official instructions and respected the conventions of the system(s) on which it runs?