Error: trying to overwrite foo, which is also in package bar

This is an example of an apt-get error caused by a file conflict, and how to resolve it:

Unpacking replacement kdebase-libs ...
dpkg: error processing /var/cache/apt/archives/kdebase-libs_4%3a2.2.2-14_i386.deb (--unpack):
trying to overwrite /usr/share/applnk/Settings/Sound/arts.desktop', which is also in package kdebase
dpkg-deb: subprocess paste killed by signal (Broken pipe)
Errors were encountered while processing:
/var/cache/apt/archives/kdebase-libs_4%3a2.2.2-14_i386.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

This happens because somebody made a bonehead packaging error, or a graceless transition of including a file in one package rather than the other. The fix is as follows:

  1. Note down the names of the two packages that are both trying to claim ownership of the same file. In this case, package kdebase-libs is trying to update a file previously installed by package kdebase.
  2. Stop and think for a moment about what's probably happening: A reasonable guess is that "arts.desktop" has simply been moved to kdebase-libs, and your package database is being mulish because it doesn't know this is OK.
  3. Do a "--force-overwrite" install of the new package, thus:

     dpkg -i --force-overwrite kdebase-libs_4%3a2.2.2-14_i386.deb
  4. Recall and resubmit your apt-get command that choked, now that there's no remaining conflict.

On very rare occasions, apt-get might refuse to do anything because some particular named package is in some broken state it can't deal with. This is the error message :

dkpg error processing  kdebase-libs_4%3a2.2.2-14_i386.deb (--install)
cannot access archive: No such file or directory
Errors were encountered while processing:  
kdebase-libs_4%3a2.2.2-14_i386.deb

In such cases, you can make good use of the fact that Debian uses a plain ASCII file (/var/lib/dpkg/status) for its package database, to excise the problem directly: Make a backup copy of /var/lib/dpkg/status, first. (I've never needed this, but it seems such an easy precaution,why not?) Now, open /var/lib/dpkg/status in your favourite text editor (i.e. editor !).

Search for "Package: <packagename>", which will bring up a series of non-blank lines comprising the package's status entry. Remove everything up to the first blank line. Save.

Now, the package's files, themselves, are still installed, but the Debian package records have selective amnesia: The system believes that package to be not installed. Therefore, apt-get will cease stumbling on the defective package entry: You can now do other apt-get operations, and reinstall the problem package (overwriting its installed files) at any future occasion, at your leisure.

If you wish to fix errors in a package's installation scripts, they're in /var/lib/dpkg/info as packagename.postinst, packagename.preinst, packagename.prerm, and/or packagename.postrm. Debug the error, edit the script, and then re-run dpkg -i packagename. (This approach is greatly to be preferred over using --force-something options or editing the package database directly.)