The package system is broken error during Ubuntu package update [duplicate]

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.

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


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.


@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>

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.


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.