135

I was using this script to install basic software, but had to interrupt because of slow internet speed. Now when I hit $ sudo apt-get install npm , I get following error

yask123@yaskslaptop:~$ sudo apt-get installed npm
E: Invalid operation installed
yask123@yaskslaptop:~$ sudo apt-get install npm
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 npm : Depends: nodejs but it is not going to be installed
       Depends: node-abbrev (>= 1.0.4) but it is not going to be installed
       Depends: node-ansi (>= 0.3.0-2) but it is not going to be installed
       Depends: node-ansi-color-table but it is not going to be installed
       Depends: node-archy but it is not going to be installed
       Depends: node-block-stream but it is not going to be installed
       Depends: node-fstream (>= 0.1.22) but it is not going to be installed
       Depends: node-fstream-ignore but it is not going to be installed
       Depends: node-github-url-from-git but it is not going to be installed
       Depends: node-glob (>= 3.1.21) but it is not going to be installed
       Depends: node-graceful-fs (>= 2.0.0) but it is not going to be installed
       Depends: node-inherits but it is not going to be installed
       Depends: node-ini (>= 1.1.0) but it is not going to be installed
       Depends: node-lockfile but it is not going to be installed
       Depends: node-lru-cache (>= 2.3.0) but it is not going to be installed
       Depends: node-minimatch (>= 0.2.11) but it is not going to be installed
       Depends: node-mkdirp (>= 0.3.3) but it is not going to be installed
       Depends: node-gyp (>= 0.10.9) but it is not going to be installed
       Depends: node-nopt (>= 3.0.1) but it is not going to be installed
       Depends: node-npmlog but it is not going to be installed
       Depends: node-once but it is not going to be installed
       Depends: node-osenv but it is not going to be installed
       Depends: node-read but it is not going to be installed
       Depends: node-read-package-json (>= 1.1.0) but it is not going to be installed
       Depends: node-request (>= 2.25.0) but it is not going to be installed
       Depends: node-retry but it is not going to be installed
       Depends: node-rimraf (>= 2.2.2) but it is not going to be installed
       Depends: node-semver (>= 2.1.0) but it is not going to be installed
       Depends: node-sha but it is not going to be installed
       Depends: node-slide but it is not going to be installed
       Depends: node-tar (>= 0.1.18) but it is not going to be installed
       Depends: node-underscore but it is not going to be installed
       Depends: node-which but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
3
  • 6
    Off-topic, try askubuntu.com
    – Yu Hao
    Oct 26, 2014 at 8:54
  • 7
    I think this occurs because nodejs gets installed from deb.nodesource.com while npm comes from ubuntu. Usually mixing sources should be OK, but for some reason the nodesource versions don't get marked as fulfilling the npm dependancy. You can see this by comparing apt-cache policy npm with apt-cache policy nodejs. The aptitude answer worked for me, but it makes me nervous to mix apt and aptitude.
    – Quantum7
    Apr 1, 2019 at 18:39
  • 3
    The comment of @Quantum7 must be the answer! I would defenitely vote up. It helped me to identify the problem. Removing deb.nodesource.com solves the error.
    – desa
    Aug 22, 2019 at 21:44

17 Answers 17

181

If sudo apt-get install -f <package-name> doesn't work, try aptitude:

sudo apt-get install aptitude
sudo aptitude install <package-name>

Aptitude will try to resolve the problem.

As an example, in my case, I still receive some error when try to install libcurl4-openssl-dev:

sudo apt-get install -f libcurl4-openssl-dev

So i try aptitude, it turns out I have to downgrade some packages.

The following actions will resolve these dependencies:

    Keep the following packages at their current version:
    1)     libyaml-dev [Not Installed]                        

Accept this solution? [Y/n/q/? (n)

The following actions will resolve these dependencies:

    Downgrade the following packages:                                
    1)     libyaml-0-2 [0.1.4-3ubuntu3.1 (now) -> 0.1.4-3ubuntu3 (trusty)]

Accept this solution? [Y/n/q/?] (Y)
7
  • 14
    aptitude did the trick for me. Had to try a couple solutions down from the initial one, but it eventually got there. Great tip!!
    – Stan
    Mar 31, 2016 at 2:32
  • 1
    aptitude may corrupt the whole system, using apt and aptitude together might remove all the packages if you are not using carefully enough! Jun 5, 2017 at 19:56
  • 2
    I had a really messy dependency problem. Tried many commands with apt-get including -f install with no avail. The simple "sudo aptitude install" completely resolved the issue.
    – timeon
    Dec 17, 2017 at 22:03
  • 1
    Enter '.' when asked "Accept this solution [Y/n/q/?]" to get next solution.
    – VishnuVS
    Apr 19, 2019 at 9:28
  • 1
    If you get sudo: aptitude: command not found error just install aptitude using sudo apt-get install aptitude Feb 24, 2021 at 14:19
67

First of all try this

sudo apt-get update
sudo apt-get clean
sudo apt-get autoremove

If error still persists then do this

sudo apt --fix-broken install
sudo apt-get update && sudo apt-get upgrade
sudo dpkg --configure -a
sudo apt-get install -f

Afterwards try this again:

sudo apt-get install npm

But if it still couldn't resolve issues check for the dependencies using sudo dpkg --configure -a and remove them one-by-one . Let's say dependencies are on npm then go for this ,

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

Then go to /etc/apt/sources.list.d and remove any node list if you have. Then do a

sudo apt-get update

Then check for the dependencies problem again using sudo dpkg --configure -a and if it's all clear then you are done . Later on install npm again using this

v=8   # set to 4, 5, 6, ... as needed
curl -sL https://deb.nodesource.com/setup_$v.x | sudo -E bash -

Then install the Node.js package.

sudo apt-get install -y nodejs

The answer above will work for general cases also(for dependencies on other packages like django ,etc) just after first two processes use the same process for the package you are facing dependency with.

5
  • 1
    Nodesource doesn't work for me; skipping curl -sL https://... and just doing sudo apt-get install -y nodejs works Apr 3, 2021 at 15:54
  • 1
    Went through it all, nothing worked, except the last bit of advice whereby removing nodesource.list from /etc/apt/sources.list.d fixed it all! Jun 19, 2022 at 8:37
  • 1
    rm /etc/apt/sources.list.d/nodesource.list* solved the issue. After, I could run apt install npm with success.
    – Octave
    Oct 24, 2022 at 13:42
  • @Octave You are most welcome to edit the answer if required. Oct 27, 2022 at 9:17
  • I would do it with pleasure but stackoverflow says there are too many pending edits for this answer. Will retry later.
    – Octave
    Nov 14, 2022 at 19:52
34

The command to have Ubuntu fix unmet dependencies and broken packages is

sudo apt-get install -f

from the man page:

-f, --fix-broken Fix; attempt to correct a system with broken dependencies in place. This option, when used with install/remove, can omit any packages to permit APT to deduce a likely solution. If packages are specified, these have to completely correct the problem. The option is sometimes necessary when running APT for the first time; APT itself does not allow broken package dependencies to exist on a system. It is possible that a system's dependency structure can be so corrupt as to require manual intervention (which usually means using dselect(1) or dpkg --remove to eliminate some of the offending packages)

Ubuntu will try to fix itself when you run the command. When it completes, you can test if it worked by running the command again, and you should receive output similar to:

Reading package lists... Done Building dependency tree Reading state information... Done 0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.

2
  • 4
    in my case, sudo apt-get -f remove removed the broken package Mar 31, 2016 at 10:23
  • 2
    Not working: E: Sub-process /usr/bin/dpkg returned an error code (1)
    – tribbloid
    May 9, 2021 at 21:09
28

I came to this situation when I installed node js from the latest stable release.

curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -

It seems this version already comes with a prepackaged NPM. So when I tried to install NPM again, I got this error. If anyone is installing Nodejs in this manner then, it's not needed to install NPM separately.

The following packages have unmet dependencies:
 npm : Depends: nodejs but it is not going to be installed
       Depends: node-abbrev (>= 1.0.4) but it is not going to be installed
       Depends: node-ansi (>= 0.3.0-2) but it is not going to be installed
       Depends: node-ansi-color-table but it is not going to be installed
       Depends: node-archy but it is not going to be installed
       Depends: node-block-stream but it is not going to be installed
       Depends: node-fstream (>= 0.1.22) but it is not going to be installed
       Depends: node-fstream-ignore but it is not going to be installed
       Depends: node-github-url-from-git but it is not going to be installed
       Depends: node-glob (>= 3.1.21) but it is not going to be installed
       Depends: node-graceful-fs (>= 2.0.0) but it is not going to be installed
       Depends: node-inherits but it is not going to be installed
       Depends: node-ini (>= 1.1.0) but it is not going to be installed
       Depends: node-lockfile but it is not going to be installed
       Depends: node-lru-cache (>= 2.3.0) but it is not going to be installed
       Depends: node-minimatch (>= 0.2.11) but it is not going to be installed
       Depends: node-mkdirp (>= 0.3.3) but it is not going to be installed
       Depends: node-gyp (>= 0.10.9) but it is not going to be installed
       Depends: node-nopt (>= 3.0.1) but it is not going to be installed
       Depends: node-npmlog but it is not going to be installed
       Depends: node-once but it is not going to be installed
       Depends: node-osenv but it is not going to be installed
       Depends: node-read but it is not going to be installed
       Depends: node-read-package-json (>= 1.1.0) but it is not going to be installed
       Depends: node-request (>= 2.25.0) but it is not going to be installed
       Depends: node-retry but it is not going to be installed
       Depends: node-rimraf (>= 2.2.2) but it is not going to be installed
       Depends: node-semver (>= 2.1.0) but it is not going to be installed
       Depends: node-sha but it is not going to be installed
       Depends: node-slide but it is not going to be installed
       Depends: node-tar (>= 0.1.18) but it is not going to be installed
       Depends: node-underscore but it is not going to be installed
       Depends: node-which but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

22

Installing nodejs will install npm ... so just remove nodejs then reinstall it: $ sudo apt-get remove nodejs

$ sudo apt-get --purge remove nodejs node npm
$ sudo apt-get clean
$ sudo apt-get autoclean
$ sudo apt-get -f install
$ sudo apt-get autoremove
0
16

I tried lots of method but below work like charm....

After this command run these :-

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

Now check…

node -v
npm -v
3
  • 1
    I am using GCE (google compute engine) and this answer worked for me. Aug 9, 2020 at 9:30
  • 1
    Me too, after having tried everything only this approach worked. And instantly.
    – Nillus
    Aug 3, 2021 at 5:36
  • This answer hard-codes a specific version. Check github.com/nodesource/distributions#debinstall for all available versions and the command line to install them.
    – Eric J.
    Jan 20, 2022 at 18:34
9
sudo apt install aptitude

Then

sudo aptitude install npm

Source: https://askubuntu.com/a/978353/458947

1
  • I am using GCE (google compute engine) with an Ubuntu docker image and this worked for me. Please note you'll have to use aptitude install -y npm , while docker build or it will abort automatically. Apr 22 at 16:28
5

This is a bug in the npm package regarding dependencies : https://askubuntu.com/questions/1088662/npm-depends-node-gyp-0-10-9-but-it-is-not-going-to-be-installed

Bugs have been reported. The above may not work depending what you have installed already, at least it didn't for me on an up to date Ubuntu 18.04 LTS.

I followed the suggested dependencies and installed them as the above link suggests:

sudo apt-get install nodejs-dev node-gyp libssl1.0-dev

and then

sudo apt-get install npm

Please subscribe to the bug if you're affected:

bugs.launchpad.net/ubuntu/+source/npm/+bug/1517491
bugs.launchpad.net/ubuntu/+source/npm/+bug/1809828

5
  • thanks so much of all this answers yours worked for me. Jul 1, 2020 at 15:30
  • For me, I get: E: Package 'libssl1.0-dev' has no installation candidate Nov 23, 2020 at 21:37
  • That may have many causes, like no internet, no apt-update, different version of Ubuntu, etc. In Ubuntu 20.04 the package seems to be called libssl-dev . Try apt search libssl and see how yours may be named. Nov 23, 2020 at 22:26
  • E: Package 'libssl1.0-dev' has no installation candidate
    – alper
    Mar 14, 2022 at 10:10
  • see reply above,search for the package name, it may be different Aug 21 at 14:56
4

First, run

sudo apt-get install nodejs-dev node-gyp libssl1.0-dev

then run

sudo apt install npm
2
  • 2
    how is this in any way an improvement to the answer above? It's a copy of my answer without explanation. Mar 30, 2022 at 11:58
  • I needed sudo apt-get install node-gyp Feb 19 at 19:19
1

This worked for me on Pop OS:

Force an overwrite for the Node package (change nodejs_16.9.0-deb-1nodesource1_amd64.deb with your version):

sudo dpkg -i --force-overwrite /var/cache/apt/archives/nodejs_16.9.0-deb-1nodesource1_amd64.deb

Fix the rest of the broken install:

sudo apt -f install

Make sure the rest of your system is properly up to date:

sudo apt update
sudo apt dist-upgrade

credits to matigo : https://askubuntu.com/questions/1362719/error-upgrading-nodejs

1
  • --force-overwrite helped in my case, also. This extreme step seems to be required from time to time for different packages. It was related to libclang-common-15-dev for me. I saw the solution via this answer: askubuntu.com/a/266912/347966
    – Hari
    May 21 at 4:39
0

I just solved this issue. The problem was in version conflict. Nodejs 10 installed with npm. So before installing nodejs - remove old npm. Or remove new node -> remove npm -> install node again.

This is the only way which helped me.

0

What resolved it for me was:

$ apt update
$ apt install nodejs

I had nodejs installed from the official Debian repositories. This one was installed by unattended-upgrades. Before that it was installed from NodeSource. The one coming from NodeSource has npm in the nodejs package.

0

Ubuntu 22.04 has upgraded libssl1 to libssl3 so you need to add Ubuntu 21.10 source and force the installation of libssl1.

Try this on the terminal:

echo "deb http://security.ubuntu.com/ubuntu impish-security main" | sudo tee /etc/apt/sources.list.d/impish-security.list

sudo apt-get update
sudo apt-get install libssl1.1
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jun 15, 2022 at 16:41
0

somehow sudo apt install apt solved the issue for me

0

I came across with the same problem. I followed the following steps for the solution: First I removed nodejs and npm. Then, using the information on the webpage "NodeSource Node.js Binary Distributions" related with my operating system, I setup nodejs and npm. The webpage "NodeSource Node.js Binary Distributions" is in the following link:

https://github.com/nodesource/distributions/blob/master/README.md

The installation can be checked using the command given in the "Tests" section with the following link:

https://github.com/nodesource/distributions/blob/master/README.md#tests

0

I solved this like so:

  119  mkdir ~/.npm-global
  120  npm config set prefix '~/.npm-global'
  121  vi ~/.profile
    ## add .npm-global to start of your PATH var
  122  source ~/.profile

Then reinstall a new copy of npm in there:

npm install -g npm

That way, it wont use npm from /usr/lib/node_modules any more.

Also, if that doesn't work, make sure windows folders are not included in your linux PATH

-7

Node comes with npm installed so you should have a version of npm. However, npm gets updated more frequently than Node does, so you'll want to make sure it's the latest version.

Try

sudo npm install npm -g
1
  • I landed here while trying sudo apt-get install -y nodejs npm, (works fine for Ubuntu's default version of Node). All I had to do was sudo apt-get install -y nodejs instead and my nodejs : Conflicts: npm went away. Thanks for the hint!
    – theSparky
    Aug 30 at 21:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.