apt and dpkg ruined after botched xplico installation

I tried to install the version of xplico for Ubuntu 16.04 in Ubuntu 21.10 with sudo bash -c 'echo "deb http://repo.xplico.org/ $(lsb_release -s -c) main" >> /etc/apt/sources.list' && sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 791C25CE && sudo apt update && sudo apt install xplico and then I got a `post-removal script subprocess returned error exit status 127 error.

After a failed xplico install now I'm left with this error message and I can't install or remove other applications because xplico has to be removed first:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
  xplico
0 upgraded, 0 newly installed, 1 to remove and 52 not upgraded.
1 not fully installed or removed.
After this operation, 86.8 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 213087 files and directories currently installed.)
Removing xplico (1.2.2) ...
/var/lib/dpkg/info/xplico.postrm: 23: /etc/apache2: Permission denied
dpkg: error processing package xplico (--remove):
 installed xplico package post-removal script subprocess returned error exit status 127
dpkg: too many errors, stopping
Errors were encountered while processing:
 xplico
Processing was halted because there were too many errors.
E: Sub-process /usr/bin/dpkg returned an error code (1)

what should I do?


You need to remove the xplico package, but the version of xplico that you installed is for Ubuntu 16.04 and you are running Ubuntu 21.10. Maybe that's why you got a post-removal script subprocess returned error exit status 127 error message. exit status 127 is the exit code for an expired key. The expired key is from 2016 and I didn't find any way of updating it, but you can regain normal use of apt and dpkg without updating the expired key.

  1. Remove the expired key that you added when trying to install xplico.

    sudo apt-key del 791C25CE
    
  2. Edit /var/lib/dpkg/info/xplico.postrm (if it exists) in nano text editor with sudoedit /var/lib/dpkg/info/xplico.postrm and replace its contents with:

    #!/bin/bash
    set -e
    /bin/true
    

    Nano text editor keyboard shortcuts
    Use the keyboard combination Ctrl + O and after that press Enter to save the file to its current location.
    Use the keyboard combination Ctrl + X to exit nano.

  3. Edit your /var/lib/dpkg/status by removing xplico with its description and save your changes to /var/lib/dpkg/status. Then run sudo dpkg --configure -a && sudo apt -f install.

  4. The above command might not do the trick. If so, run the following additional commands to force uninstall xplico.

    sudo mv /var/lib/dpkg/info/postinst /var/lib/dpkg/info/postinst.bak
    sudo mv /var/lib/dpkg/info/postrm /var/lib/dpkg/info/postrm.bak
    sudo mv /var/lib/dpkg/info/preinst /var/lib/dpkg/info/preinst.bak
    sudo dpkg --remove --force-remove-reinstreq xplico
    

Then try to update the system with sudo apt update