How to install npm in alpine linux

83

18

So I can't get to install npm in alpine linux. I thought perhaps I can just do a apk add npm but apparently apk search npm returns nothing, even after a apk update. I'm experimenting with all this from the nginx:alpine docker image, i.e. docker run -it nginx:alpine /bin/sh

Edit 1: I can see how the nodejs:alpine dockerfile builds node, but I don't understand how it builds npm

Edit 2: now that I know that npm gets installed with nodejs on alpine, and just for clarification, the reason this wasn't evident to me at first is that on ubuntu 14.04 a sudo apt-get install nodejs would still require a sudo apt-get install npm (which installs development packages e.g. gcc)

shadi

Posted 2016-09-19T15:20:36.847

Reputation: 985

Answers

52

For the recent versions of Alpine (v3.8+) the correct way to install nodejs with npm is:

apk add --update nodejs npm

However, npm package depends on nodejs, so you can do:

apk add --update npm

Note: since Alpine 3.8 there is no nodejs-npm package.

Ruslan Isay

Posted 2016-09-19T15:20:36.847

Reputation: 636

1This should be the accepted answer now – Omar S. – 2019-05-04T12:59:11.380

180

I had an issue with the apk manager.

The package nodejs is no longer installing NPM (see pkgs.alpinelinux.org) You have to install nodejs-npm

apk add --update nodejs nodejs-npm

Faisal HUSSAIN

Posted 2016-09-19T15:20:36.847

Reputation: 1 901

I do not understand it. Npm should be the core dependency of node, npm install npm@latest might be impacted by the node version itself so they would not be compatible... – dmi3y – 2017-05-30T19:47:38.460

5This should be the accepted answer with the latest alpine image. npm was not installed for me with just nodejs – kevinc – 2017-07-29T15:35:59.437

Agreed, this is normally a dependency, but if you're installing nodejs manually (I was installing it from the 'edge' repo as well because as of now 6.7 is deprecated) then you need to install nodejs-npm separately

Here's what I ran: apk add nodejs=6.11.2-r0 nodejs-npm=6.11.2-r0 --update-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/main/ --allow-untrusted

– Anton Babushkin – 2017-08-22T23:53:04.103

5

Side question: while this answer works just fine, I can't seem to find nodejs-npm on https://pkgs.alpinelinux.org/packages. There is npm package which also does the job. Is it some kind of alias?

– Tad Lispy – 2018-07-25T14:28:10.953

29

I could be wrong, but I think npm is actually a dependency of nodejs.

I've never seen any flavor of package manager install npm alone. Always seems to come packaged with yum install nodejs, or apt-get install nodejs, or apk add --update nodejs.

Joseph Roberts

Posted 2016-09-19T15:20:36.847

Reputation: 407

1Welcome to Super User. I have no idea whether you're right, but just some quick feedback. Answers are intended to be definitive solutions. With a little more rep, you can post a comment with a helpful guess. For an answer post, research it first, so you're sure of the information, then post. And you can then cite a reference, which makes an even better answer. – fixer1234 – 2016-12-30T21:51:21.310

18this is no longer accepted, as apk does not installs npm by default when installing nodejs – Panthro – 2017-08-12T22:52:14.207

upvoted just because nobody provided a better answer – Alexander Mills – 2017-08-31T01:01:44.467

1I believe it is 'apk add --update nodejs-npm' for Alpine 3.6+ – Ali Cheaito – 2019-02-13T15:14:09.923

8

apk update && apk add nodejs installed the npm binary for me.

John Delaney

Posted 2016-09-19T15:20:36.847

Reputation: 81

not anymore.... – Panthro – 2017-08-12T22:50:16.810

1Upvoted. Still works – bholagabbar – 2017-09-13T16:45:40.727

2Disputing if something works should include versions of things... – Eric Swanson – 2019-01-03T22:00:55.370

4

The issue here is a recent one and is due to changes in Alpine's package repositories between v3.5 and v3.6 or edge.

In v3.5 nodejs included npm In v3.6 nodesjs does not include npm and the new nodejs-npm package exists.

See here for Alpine packages. To see what version of packages you are pulling from look at the contents of /etc/apk/repositories

Peter

Posted 2016-09-19T15:20:36.847

Reputation: 41

1

npm comes hand in hand with nodejs. In the case you cant install node with apk add nodejs, you need fix that first. Step 1 - do you have the community repo added to your /etc/apk/repositories list? If not, it is very useful to do so. Further details: https://wiki.alpinelinux.org/wiki/Enable_Community_Repository

vizmi

Posted 2016-09-19T15:20:36.847

Reputation: 119

3Can you explain the down vote? – vizmi – 2017-09-20T04:20:31.640

0

I have just had to this and can confirm that npm is not a dependency of node.js (at least right now on alpine) and must be installed seperately

i.e apk add --update npm

Samuel Dare

Posted 2016-09-19T15:20:36.847

Reputation: 101