129

When I try to install npm with sudo apt-get install npm, I got following error:

E: Unable to locate package npm

Why can't apt find npm? I'm using Debian 9 and already did run

sudo apt-get install nodejs
ROOT
  • 11,363
  • 5
  • 30
  • 45
Jacs
  • 1,437
  • 4
  • 21
  • 31

10 Answers10

229

From the official Node.js documentation:

A Node.js package is also available in the official repo for Debian Sid (unstable), Jessie (testing) and Wheezy (wheezy-backports) as "nodejs". It only installs a nodejs binary.

So, if you only type sudo apt-get install nodejs , it does not install other goodies such as npm.

You need to type:

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs

Optional: install build tools

To compile and install native add-ons from npm you may also need to install build tools:

sudo apt-get install -y build-essential

More info: Docs NodeJs

Juanmabs22
  • 1,194
  • 9
  • 10
gokcand
  • 6,694
  • 2
  • 22
  • 38
  • 5
    @gokcand Do you have any idea why npm is not available in the Debian repository? – martinkunev Feb 28 '18 at 11:21
  • 1
    When I run this: `curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -` I get this output: `+ apt-get install -y lsb-release > /dev/null 2>&1` `Error executing command, exiting` – Levi Sep 20 '19 at 10:12
  • After trying this and many other suggestions (all that had merit), I closed the terminal out of frustration. I came back later to try it again and it was working right form the beginning. It seems that the part everyone left out was to open and close the terminal after install i.e Turn it on and off again ;) I should have known to try that. – Tyson Gibby Jul 26 '21 at 20:47
  • Worked for me while trying to install Node.js via Termux on Android 8. Thanks for the help. – AndyCODE Apr 11 '22 at 17:13
89

Encountered this in Ubuntu for Windows, try running first

sudo apt-get update
sudo apt-get upgrade

then

sudo apt-get install npm
Mobiletainment
  • 22,201
  • 9
  • 82
  • 98
Red
  • 1,141
  • 8
  • 13
  • 2
    Worked for me, confirmed by this article, section prerequisites : https://blogs.windows.com/buildingapps/2016/07/22/fun-with-the-windows-subsystem-for-linux/. It also recommend doing `sudo apt upgrade`. – Ambroise Rabier Mar 12 '19 at 14:19
37

I ran into the same issue on Debian 9.2, this is what I did to overcome it.

Installation

sudo apt install curl
curl -sL https://deb.nodesource.com/setup_6.x | sudo bash -
sudo apt-get install -y nodejs
sudo apt-get install -y npm

Check installed versions

node --version
npm --version

Originally sourced from "How to install Node.js LTS on Debian 9 stretch" http://linuxbsdos.com/2017/06/26/how-to-install-node-js-lts-on-debian-9-stretch/

Prolog
  • 2,698
  • 1
  • 18
  • 31
trentinb
  • 371
  • 3
  • 5
  • 8
    Is there no official Debian way of installing npm? npm says it's bundled with node.js, so why isn't it available when I do `apt install nodejs`? – 0__ Dec 28 '17 at 11:27
  • 2
    Worked for me! You can use `curl -sL https://deb.nodesource.com/setup_lts.x | sudo bash -` for latest lts version. – Datz Oct 17 '20 at 11:34
15

Your system can't find npm package because you haven't add nodejs repository to your system..

Try follow this installation step:
Add nodejs PPA repository to our system and python software properties too

sudo apt-get install curl python-software-properties 
// sudo apt-get install curl software-properties-common

curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -
sudo apt-get update

Then install npm

sudo apt-get install nodejs

Check if npm and node was installed and you're ready to use node.js

node -v
npm -v

If someone was failed to install nodejs.. Try remove the npm first, maybe the old installation was broken..

sudo apt-get remove nodejs
sudo apt-get remove npm

Check if npm or node folder still exist, delete it if you found them

which node
which npm
StefansArya
  • 2,802
  • 3
  • 24
  • 25
5

For Debian Stretch (Debian version 9), nodejs does not include npm, and to install it as a separate package, you have to enable stretch-backports.

echo 'deb http://deb.debian.org/debian stretch-backports main' |
sudo tee /etc/apt/sources.list.d/stretch-backports.list
apt-get update -y
apt-get -t stretch-backports install -y npm

In Buster (Debian 10), npm is a regular package, so going forward, this should just work. But some of us will still be stuck partially on Stretch boxes for some time to come.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    100% right. run "lsb_release -a" if you don't know if you are on Debian Stretch, and if so, follow this answer. – Rub Feb 15 '21 at 23:39
4

This will resolve your error. Run these commands in your terminal. These commands will add the older versions. You can update them later or you can change version here too before running these commands one by one.

sudo apt-get install build-essential
wget http://nodejs.org/dist/v0.8.16/node-v0.8.16.tar.gz
tar -xzf node-v0.8.16.tar.gz
cd node-v0.8.16/
./configure
make
sudo make install
Prolog
  • 2,698
  • 1
  • 18
  • 31
Priyank lohan
  • 483
  • 1
  • 6
  • 17
  • Thanks, this: 'sudo apt-get install build-essential' fixed it for me – Wes Jul 23 '19 at 20:12
  • Installing from source means that you will not get any security updates unless you fetch them yourself and build a new local version whenever there is a new patch available. – tripleee Feb 16 '21 at 05:52
2

in my jenkins/jenkins docker sudo always generates error:

bash: sudo: command not found

I needed update repo list with:

curl -sL https://deb.nodesource.com/setup_10.x | apt-get update

then,

 apt-get install nodejs

All the command line results like this:

root@76e6f92724d1:/# curl -sL https://deb.nodesource.com/setup_10.x | apt-get update
Ign:1 http://deb.debian.org/debian stretch InRelease
Get:2 http://security.debian.org/debian-security stretch/updates InRelease [94.3 kB]
Get:3 http://deb.debian.org/debian stretch-updates InRelease [91.0 kB]
Get:4 http://deb.debian.org/debian stretch Release [118 kB]
Get:5 http://security.debian.org/debian-security stretch/updates/main amd64 Packages [520 kB]
Get:6 http://deb.debian.org/debian stretch-updates/main amd64 Packages [27.9 kB]
Get:8 http://deb.debian.org/debian stretch Release.gpg [2410 B]
Get:9 http://deb.debian.org/debian stretch/main amd64 Packages [7083 kB]
Get:7 https://packagecloud.io/github/git-lfs/debian stretch InRelease [23.2 kB]
Get:10 https://packagecloud.io/github/git-lfs/debian stretch/main amd64 Packages [4675 B]
Fetched 7965 kB in 20s (393 kB/s)
Reading package lists... Done
root@76e6f92724d1:/#  apt-get install nodejs
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libicu57 libuv1
The following NEW packages will be installed:
  libicu57 libuv1 nodejs
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 11.2 MB of archives.
After this operation, 45.2 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://deb.debian.org/debian stretch/main amd64 libicu57 amd64 57.1-6+deb9u3 [7705 kB]
Get:2 http://deb.debian.org/debian stretch/main amd64 libuv1 amd64 1.9.1-3 [84.4 kB]
Get:3 http://deb.debian.org/debian stretch/main amd64 nodejs amd64 4.8.2~dfsg-1 [3440 kB]
Fetched 11.2 MB in 26s (418 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libicu57:amd64.
(Reading database ... 12488 files and directories currently installed.)
Preparing to unpack .../libicu57_57.1-6+deb9u3_amd64.deb ...
Unpacking libicu57:amd64 (57.1-6+deb9u3) ...
Selecting previously unselected package libuv1:amd64.
Preparing to unpack .../libuv1_1.9.1-3_amd64.deb ...
Unpacking libuv1:amd64 (1.9.1-3) ...
Selecting previously unselected package nodejs.
Preparing to unpack .../nodejs_4.8.2~dfsg-1_amd64.deb ...
Unpacking nodejs (4.8.2~dfsg-1) ...
Setting up libuv1:amd64 (1.9.1-3) ...
Setting up libicu57:amd64 (57.1-6+deb9u3) ...
Processing triggers for libc-bin (2.24-11+deb9u4) ...
Setting up nodejs (4.8.2~dfsg-1) ...
update-alternatives: using /usr/bin/nodejs to provide /usr/bin/js (js) in auto mode
uzay95
  • 16,052
  • 31
  • 116
  • 182
0

Download the the repository key with:

curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -

Then setup the repository:

sudo sh -c "echo deb https://deb.nodesource.com/node_8.x cosmic main \
                 > /etc/apt/sources.list.d/nodesource.list"
sudo apt-get update
sudo apt-get install nodejs
tripleee
  • 175,061
  • 34
  • 275
  • 318
F.furq
  • 1
  • 1
-1

I had a similar issue and this is what worked for me.

Add the NodeSource package signing key:

curl -sSL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -
# wget can also be used:
# wget --quiet -O - https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -

Add the desired NodeSource repository:

# Replace with the branch of Node.js or io.js you want to install: node_6.x, node_12.x, etc...
VERSION=node_12.x
# The below command will set this correctly, but if lsb_release isn't available, you can set it manually:
# - For Debian distributions: jessie, sid, etc...
# - For Ubuntu distributions: xenial, bionic, etc...
# - For Debian or Ubuntu derived distributions your best option is to use the codename corresponding to the upstream release your distribution is based off. This is an advanced scenario and unsupported if your distribution is not listed as supported per earlier in this README.
DISTRO="$(lsb_release -s -c)"
echo "deb https://deb.nodesource.com/$VERSION $DISTRO main" | sudo tee /etc/apt/sources.list.d/nodesource.list
echo "deb-src https://deb.nodesource.com/$VERSION $DISTRO main" | sudo tee -a /etc/apt/sources.list.d/nodesource.list

Update package lists and install Node.js:

sudo apt-get update
sudo apt-get install nodejs
tripleee
  • 175,061
  • 34
  • 275
  • 318
-5

If you have installed nodejs, then you also have npm. Npm comes with node.

diogo
  • 69
  • 1
  • 8