How to fix “trying to overwrite …, which is also in package …” issue in linux

APT/DPKG error because of fileoverwrite

Let’s see how to solve the problem of apt or dpkg failing to install packages due to errors like the following. This is happening because the new package you are trying to install is overwriting a file that is part of another installed package.

dpkg_error trying to overwrite '/usr/lib/x86_64-linux-gnu/libldacBT_enc.so.2.0.2.3', which is also in package libldac:amd64 2.0.2.3~r26478861

Sample Error Message

dpkg: error processing archive /tmp/apt-dpkg-install-rf5BLm/15-libldacbt-enc2_2.0.2.3.r4.gaf2dd23-5~ubuntu20.04_amd64.deb (--unpack):
 trying to overwrite '/usr/lib/x86_64-linux-gnu/libldacBT_enc.so.2.0.2.3', which is also in package libldac:amd64 2.0.2.3~r26478861
Preparing to unpack .../16-libldacbt-abr2_2.0.2.3.r4.gaf2dd23-5~ubuntu20.04_amd64.deb ...
Unpacking libldacbt-abr2:amd64 (2.0.2.3.r4.gaf2dd23-5~ubuntu20.04) over (2.0.2.3+git20200429+ed310a0-5) ...
dpkg: error processing archive /tmp/apt-dpkg-install-rf5BLm/16-libldacbt-abr2_2.0.2.3.r4.gaf2dd23-5~ubuntu20.04_amd64.deb (--unpack):
 trying to overwrite '/usr/lib/x86_64-linux-gnu/libldacBT_abr.so.2.0.2.3', which is also in package libldac:amd64 2.0.2.3~r26478861

How to resolve the “trying to overwrite” Issue?

There are multiple solutions below based on when the error occurred. If you got the issue while running an apt command, then check the apt section. If it happened while running a dpkg command, then check the dpkg section.

With DPKG

If the error occurred while you were executing the apt command, then use the command as follows.

sudo dpkg -i --force-overwrite theDebYouWantToInstall.deb
sudo apt-get --fix-broken install

This command will install the deb file with file overwriting when needed.

With APT

If the error occurred while you were executing the dpkg command, then use the command as follows.

sudo apt-get -o Dpkg::Options::="--force-overwrite" [YourAptActionCommand]

#An Example
sudo apt-get -o Dpkg::Options::="--force-overwrite" apt-get upgrade

#Another Example
sudo apt-get -o Dpkg::Options::="--force-overwrite" apt-get install nmap

This command will install the deb file with file overwriting when needed.

Muhammed Afsal Villan is an experienced full-stack developer, specialized in desktop and mobile application development. He also regularly publishes quality tutorials on his YouTube channel named 'Genuine Coder'. He likes to contribute to open-source projects and is always enthusiastic about new technologies.