Unable to install "bat". Error: trying to overwrite '/usr/.crates2.json', which is also in package ripgrep 11.0.2-1build1

I'm trying to install the package bat on Kubuntu. This is my current system:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.1 LTS
Release:        20.04
Codename:       focal

When I try to install "bat", I get this message:

$ sudo apt install -y bat
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  gir1.2-appindicator3-0.1 gir1.2-gst-plugins-base-1.0 gir1.2-gstreamer-1.0 gir1.2-keybinder-3.0 gir1.2-wnck-3.0 gnome-session-canberra libappindicator3-1
  libcanberra-gtk3-0 libcanberra-gtk3-module libkeybinder-3.0-0 libwnck-3-0 libwnck-3-common libxres1 python3-gi-cairo
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  bat
0 upgraded, 1 newly installed, 0 to remove and 21 not upgraded.
Need to get 0 B/1.585 kB of archives.
After this operation, 4.325 kB of additional disk space will be used.
(Reading database ... 278687 files and directories currently installed.)
Preparing to unpack .../bat_0.12.1-1build1_amd64.deb ...
Unpacking bat (0.12.1-1build1) ...
dpkg: error processing archive /var/cache/apt/archives/bat_0.12.1-1build1_amd64.deb (--unpack):
 trying to overwrite '/usr/.crates2.json', which is also in package ripgrep 11.0.2-1build1
dpkg-deb: error: paste subprocess was killed by signal (Broken pipe)
Errors were encountered while processing:
 /var/cache/apt/archives/bat_0.12.1-1build1_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

How can I fix my system to correctly install bat?


Solution 1:

Apparently, this is an issue with the Rust build tools. The issue has already been fixed, and the packages can be installed normally on Ubuntu 20.10, but the fix has not yet arrived to Ubuntu 20.04.

Issue for bat: https://github.com/sharkdp/bat/issues/938

Issue for Ubuntu: https://bugs.launchpad.net/ubuntu/+source/rust-bat/+bug/1868517

According to the Ubuntu issue, /usr/.crates2.json is a "stray" file, and not important. Simply removing it is not enough, because package management still knows that it should be there.

As a workaround, you can ignore the issue and overwrite the file with the command (this command is from the comments in the above bat issue):

sudo apt install -o Dpkg::Options::="--force-overwrite" bat ripgrep

Solution 2:

I install bat first, then fail installing ripgrep.

Solved:

sudo dpkg -i --force-overwrite /var/cache/apt/archives/ripgrep_11.0.2-1build1_amd64.deb

this may varies: /var/cache/apt/archives/ripgrep_11.0.2-1build1_amd64.deb