202

For example:

$ sudo apt-get install curl
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
bsh : Depends: libjline-java but it is not going to be installed
groovy : Depends: libjline-java but it is not going to be installed
rhino : Depends: libjline-java but it is not going to be installed
E: Unmet dependencies. 

Try 'apt-get -f install' with no packages (or specify a solution).

I get the same or similar errors when I attempt to install clojure1.3, leiningen, and several other packages.

When I try the suggestion made in the error message, this is what happens:

$ sudo apt-get -f install 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Correcting dependencies... Done
The following packages were automatically installed and are no longer required:
  diffstat linux-headers-3.2.0-26-generic linux-headers-3.2.0-26 dh-apparmor dkms html2text libmail-sendmail-perl libsys-hostname-long-perl
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  libjline-java
Suggested packages:
  libjline-java-doc
The following NEW packages will be installed:
  libjline-java
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
23 not fully installed or removed.
Need to get 0 B/72.0 kB of archives.
After this operation, 129 kB of additional disk space will be used.
Do you want to continue [Y/n]? Y
(Reading database ... 226243 files and directories currently installed.)
Unpacking libjline-java (from .../libjline-java_1.0-1_all.deb) ...
dpkg: error processing /var/cache/apt/archives/libjline-java_1.0-1_all.deb (--unpack):
 trying to overwrite '/usr/share/java/jline.jar', which is also in package scala 2.9.2-400
Errors were encountered while processing:
 /var/cache/apt/archives/libjline-java_1.0-1_all.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

$ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
You might want to run 'apt-get -f install' to correct these.
The following packages have unmet dependencies:
 bsh : Depends: libjline-java but it is not installed
 groovy : Depends: libjline-java but it is not installed
 rhino : Depends: libjline-java but it is not installed
E: Unmet dependencies. Try using -f.
Anwar
  • 76,649

6 Answers6

179

Warning: This answer is dangerous and may lead to a broken system (because this will effectively install the package but new problems may arise when both the packages try to use the same library/file/binary). Use Avinash Raj's answer instead. Also consider reporting a bug for both conflicting packages.


You have to force overwrite the files causing issues:

sudo dpkg -i --force-overwrite <file-path>

In your case it would be:

sudo dpkg -i --force-overwrite /var/cache/apt/archives/libjline-java_1.0-1_all.deb

Check that everything is fixed by running:

sudo apt-get --fix-broken install

If you still have problems, rerun the first step with any of the dpkg: error processing (...) remaining.

Solution found on webupd8

crxyz
  • 159
danjjl
  • 6,465
  • 11
    This will effectively install the package but the problem remains when both package tries to use the same library/file/binary. – Braiam May 11 '14 at 23:11
  • 21
    Goodness, no, that's a recipe for a broken system... – fkraiem Jul 14 '16 at 01:41
  • "force-overwrite" probably should at least have some kind of warning/disclaimer – reducing activity Nov 14 '19 at 21:31
  • awesome, this should be the accepted answer. – Wizard Nov 29 '20 at 08:27
  • Could you provide more information of --force-overwrite? Doesn't seems to be present on current dpkg – Pablo Bianchi Nov 06 '21 at 19:26
  • THIS ANSWER IS DANGEROUS! Use Avinash Raj's answer instead, and then file a bug report with BOTH package maintainers. In my case the conflict was between mono-devel and discodos. I uninstalled discodos. – Amedee Van Gasse Feb 16 '22 at 15:19
  • The conflicting file could just be a man page (what I'm not sure of, is if apt only reports the first conflicting file or reports all of them, i.e. maybe it exits with error because of a man page, your run it again with --force-overwrite and then in overwrites some important binary files) – golimar Mar 25 '22 at 07:51
  • This did help me after a messy upgrade to Ubuntu 22.04. But yeah, dangerous stuff. Have a boot USB ready when doing this. – Lizozom Feb 23 '23 at 14:42
  • Oh you saved my nerves man. Python-numpy was driving my nuts. I even deleted everything related to it and still had no success. Thanx. – DimiDak May 03 '23 at 14:15
  • I used this command when the conflicting file was a background JPG file. I thought that couldn't lead to the biggest problems. – IIVQ Aug 01 '23 at 12:49
  • This was the only thing that worked for me when a direct package upgrade (no second package involved, probably just a buggy script) failed on debian testing. Thank you so much. – ChrisB Aug 06 '23 at 11:22
  • This seems to have worked for me since I was switching from Wayland to non-Wayland and wanted to get Espanso working again: sudo apt install ./espanso-debian-x11-amd64.deb -o Dpkg::Options::="--force-overwrite" – Ryan Mar 29 '24 at 18:48
165

Please don't go for the danjjl answer directly, if you face this kind of "trying to overwrite" error, is likely that you have conflicting packages that need to be solved first.

The immediate fix for the issue is to remove the conflicting package that is undesirable, in this case scala,

sudo dpkg -P scala

Next it would be recommended to submit a bug report with the respective package maintainers. This normally imply adding a Conflict: package line in the control file.

Also see this answer for more detailed explanation about this error.

Avinash Raj
  • 78,556
  • 7
    Not so sure about this advice. I think it depends on the situation. In the link to your earlier posting, there is a conflict from two versions of the same software. I agree there that one should remove the older software first. However, if there is a problem with how two different programs were packaged and the file that is being overwritten is identical or otherwise harmless, then I don't see a problem with the advice. No, not ideal...but it's probably a bit extreme to ask someone to not go for the a particular answer if the alternative is to wait until the problem is bug fixed... – Ray Dec 02 '15 at 13:11
  • @Ray in that case a bug report may be in order... but I doubt software from Ubuntu repositories has that problem, since they pull from Debian and Debian tests for these kind of problems exhaustively. – Braiam Jul 14 '16 at 01:35
  • 1
    @Braiam It's been a long time since I used Debian. Perhaps it's better now, but I saw similar problems with Debian. It isn't because of lack of exhaustive testing, but sometimes it is hard to test every possible scenario. No matter how much testing is performed, something can slip through. I'm not against writing a bug report, but "easier said that done". After all, it may be hard to summarize all of the conflicting packages. And, if bugs are not "hot" (i.e., they don't affect a lot of people), you'll just end up waiting. – Ray Jul 14 '16 at 01:48
  • 1
    Perhaps it would be better if the original answer was modified so that it said, "Please don't do because of (some reason)." So that someone can choose to ignore it once they know the pros and cons. As it stand, the answer above just says don't do it. – Ray Jul 14 '16 at 01:52
  • @Ray umm... when building a package dpkg stores a list of files (dpkg -L package) and simply looking for collisions should be enough for first measure. And by exhaustive testing I meant real world testing, unstable and testing are there precisely for this kind of stuff – Braiam Jul 14 '16 at 02:16
  • @Ray you really don't want two versions of the same package. You don't. It only gives more problems in the long run, this answer makes sure you understand that and its advise is to be taken seriously, not "ignored" – Braiam Jul 14 '16 at 02:21
  • About conflicts -- I don't know how dpkg works. You got me there. But I've seen it a few times (i.e., very rarely...once a year or two?) to think that things do get missed. As for your second point, I agree with you that it's not good to have two version of the same package. I just think that the original statement to not follow someone else's advice is a bit heavy-handed. There is a time and place for --force-overwrite and explaining it by offering the pros/cons seems more helpful than saying don't follow someone's advice that works. – Ray Jul 14 '16 at 02:28
  • And just to clarify, I noticed that you edited the answer recently. So, I don't know what it said 8 months ago when I first wrote my comment. If you're editing the answer and improving it, then great! But as you do, of course, what I'm saying is becoming less and less relevant. – Ray Jul 14 '16 at 02:29
  • In my case, I used checkinstall to create a custom version of GTK3, which in debian is split into several packages. As I wasn't sure how many packages libgtk3 was split into, I made a single package for all of it. This package will be replaced automatically when libgtk-3-0 gets updated, so I had a valid use case for this. – Wyatt Ward Jan 26 '17 at 13:44
  • 2
    FYI This solution worked for me. Offending package in my case was libc6-dev-i386 – FractalSpace Jan 29 '18 at 16:51
  • I'm getting nvidia-418 conflicting with nvidia-390. How am I supposed to upgrade drivers then? – Aaron Franke Jul 13 '19 at 02:18
  • uninstall all the nvidi* drivers first and then install 418 version. – Avinash Raj Jul 13 '19 at 02:22
  • After hours of wrestling, THIS worked for me. Offending package: libkf5akonadi-data. Thanks – dargaud Oct 23 '19 at 20:25
  • it seems to work for me, I did sudo dpkg -P libaudqt2 which is the old package, and after that sudo apt --fix-broken install worked (previously it didn't work). – Yan King Yin Apr 28 '20 at 04:44
  • This rescued me with some Qt dev packages – Dirk Hartzer Waldeck Aug 07 '20 at 01:42
  • In my case the conflict was between mono-devel and discodos. I uninstalled discodos. Works for me. – Amedee Van Gasse Feb 16 '22 at 15:21
  • I added a warning to that dangerous answer. It's in the edit queue, so if anyone with more rep could approve my edit, that would be nice. Most of my StackExchange rep is on StackOverflow so I can't do direct edits here. – Amedee Van Gasse Feb 16 '22 at 15:31
  • Worked for
    npacking libpython3.7-stdlib:amd64 (3.7.13-1+focal3) over (3.7.13-1+focal1) ...
    dpkg: error processing archive /var/cache/apt/archives/libpython3.7-stdlib_3.7.13-1+focal3_amd64.deb (--unpack):
     trying to overwrite '/usr/lib/python3.7/distutils/__init__.py', which is also in package python3.7-distutils 3.7.13-1+focal1
    

    by running sudo dpkg -P python3.7-distutils

    Thanks a lot and have a great weekend :D

    – Raphaël Duchaîne Apr 29 '22 at 19:26
88

@danjjl's command works for .deb files. I found this command works with apt/apt-get:

sudo apt-get -o Dpkg::Options::="--force-overwrite" install <package-name>
Byte Commander
  • 107,489
David Xia
  • 1,105
3

Not sure that is a global fix as I had the same issue, but with a libglx-mesa file in the cache conflicting with Nvidia-390. I removed the file and issued an apt install -f -y, let that finished and then proceeded with the apt update, upgrade and autoremove in that sequence.

I got the error while executing an apt dist-upgrade on Ubuntu 18.04 and continued the process again, no more issues and that seemed to be "my" fix.

Commands:

sudo su -
mv /var/cache/apt/archives/libglx-mesa0_18.0.0~rc5-1ubuntu1_amd64.deb ~
apt install -f -y
apt update -y
apt upgrade -y
apt autoremove -y
apt dist-upgrade

PS: Forcing an overwrite was a bad idea that I tried and I got lucky. But it's better to find the root cause, and fix it. I still have to investigate but the error lead me to move the file out the way.

3

So dpkg --force-overwrite is the hacky workaround that has been presented here in various forms. But dpkg-divert(8) is the intended way to handle this situation.

3

For Debian, Ubuntu, etc., let aptitude handle it for you`:

sudo apt update
sudo apt install aptitude

sudo aptitude install my_package_name

Then choose the necessary options. Ex: for me I choose No, Yes, Yes,

to choose to downgrade the troublesome package. Then it installs and

works!