119

I went to install bsnes the other day and, for whatever reason, the installation failed. Now, I cannot update, install new packages, or do basically any apt-get commands as they all try to process this broken package and fail. Attempting to install a new package also just dooms it to the same fate.

The error I get is:

Setting up google-chrome-stable (33.0.1750.152-1) ...
/var/lib/dpkg/info/google-chrome-stable.postinst: 124: /var/lib/dpkg/info/google-chrome-stable.postinst: update-alternatives: not found
dpkg: error processing google-chrome-stable (--configure):
 subprocess installed post-installation script returned error exit status 127
Setting up bsnes (0.088-7) ...
/var/lib/dpkg/info/bsnes.postinst: 5: /var/lib/dpkg/info/bsnes.postinst: update-alternatives: not found
dpkg: error processing bsnes (--configure):
 subprocess installed post-installation script returned error exit status 127
Errors were encountered while processing:
 google-chrome-stable
 bsnes
E: Sub-process /usr/bin/dpkg returned an error code (1)

I have been searching on Google and here on Ask Ubuntu but have not found a working solution.

The commonly suggested fix is to run the following:

sudo apt-get clean && sudo apt-get autoremove
sudo apt-get -f install
sudo dpkg --configure -a

This however does not work. The apt-get commands all fail with the same error as above and the dpkg command just doesn't help. The other thing they often suggest to purge it via Synaptic or the command line, which also fails.

10 Answers 10

108

For advanced users, use at your own risks.

According to the following error message :

subprocess installed post-installation script returned error exit status 127

You may want to edit /var/lib/dpkg/info/[package_name].postinst and comment everything (or better yet, try to understand it and identify the issue), then try apt-get again.

Note that though in this particular question the message concerns "post-installation", it could have mentioned e.g. "pre-removal" or "post-removal" instead (in which cases the extension of the file to be edited would have been .prerm or .postrm).

7
  • 2
    Actually this is the best method. Reinstalling dpkg doesn't always help.
    – Danatela
    Dec 24, 2015 at 10:36
  • 3
    Thanks heaps. I had to fix /var/lib/dpkg/info/[package_name].prerm but your post gave me the right direction. Definitely for advanced users though, this is risky!
    – sмurf
    Jan 6, 2016 at 23:10
  • 3
    The best answer. Thanks! Correcting .prerm and .postrm files fix the problem.
    – fuser
    May 27, 2017 at 9:28
  • 3
    Finally something that works. Deleting .prerm file for that package is also an option. It also works for .prerm files etc finally an almost equivalent of rpm -e --noscripts :)
    – rogerdpack
    Nov 5, 2019 at 18:50
  • I couldn't remove dokku because of failed instalation on Ubuntu 22.04.1 and this was preventing me doing sudo apt-get update && sudo apt-get upgrade. Then I commented everything in /var/lib/dpkg/info/dokku.prerm and the problem is gone. Sep 29, 2022 at 19:23
67

Other than those which you have already posted, there are few other commands which can be helpful.

  • Autoclean clears out the local repository of retrieved package files.

    sudo apt-get autoclean
    
  • Force installation/removal of packages. ☠Use with caution

    sudo apt-get --force-yes install <pkgname>
    

    and

    sudo apt-get --force-yes remove <pkgname>
    

Also as always, you can use dpkg to install, remove and purge packages.

  • Install

    sudo dpkg -i <pkgname>
    
  • Remove

    sudo dpkg -r <pkgname>
    
  • Purge

    sudo dpkg -P <pkgname>
    
13
  • 2
    Both dpkg and the apt-get --force-yes commands return the same error as I was getting before. Autoclean doesn't return an error but also doesn't seem to do anything with the two broken packages.
    – Bijak
    Mar 24, 2014 at 4:22
  • @Bijak You must have surely tried purging bsnes .Have you? Mar 24, 2014 at 4:26
  • Yep, with dpkg -P, apt-get purge, and with Synaptic
    – Bijak
    Mar 24, 2014 at 12:39
  • Post the output of dpkg -l |grep bsnes Mar 24, 2014 at 12:51
  • rF bsnes 0.088-7 amd64 Accurate SNES/SuperFamicom emulator
    – Bijak
    Mar 24, 2014 at 13:09
51

You can remove the package file in this /var/lib/dpkg/info/ path and update the source.

sudo rm /var/lib/dpkg/info/[package_name].*
sudo dpkg --configure -a
sudo apt-get update

Then, reinstall your package sudo apt-get install [package_name]

Doc

4
  • 6
    Actually you had better use sudo rm /var/lib/dpkg/info/[pakege_name].*. Otherwise you will also remove packages that start with the same name. Anyways, thanks for this, it was the only solution that worked for me
    – smac89
    Jun 21, 2017 at 16:28
  • Thanks a lot, I spent 30 minutes trying to reverse my install of an incorrect version of libc6 before I found this answer
    – yeah22
    Nov 2, 2020 at 19:50
  • Thank you very much for helping resolve this issue quickly.
    – Stephan
    Mar 31, 2022 at 8:49
  • The only solution. Other 20 solutions are dummy commands for camuflage
    – sixtytrees
    Jul 21, 2023 at 15:34
32

So, after more googling and really carefully reading through the error messages again it seems that dpkg needed to be reinstalled.

sudo apt-get install --reinstall dpkg

What tipped off that this was the problem (if anyone searches and sees this) is that update-alternatives: not found was in the error message. As soon as dpkg was reinstalled, the other packages proceeded to install normally automatically.

7
  • 4
    Why is the only correct (and accepted) answer so far down the list with less than ⅓ of the votes of the top answer? +1 Mar 31, 2017 at 23:02
  • 1
    for one: because it doesn't work for packages which can no longer be downloaded "Reinstallation of linux-image-extra-4.4.0-65-generic is not possible, it cannot be downloaded."
    – Joe
    May 19, 2017 at 20:13
  • 7
    This is probably the least likely solution for anyone coming here based on the title of the question. But there are a lot of reasons one can end up with this problem, so there will be a lot of proposed "this works for me" solutions.
    – michael
    May 26, 2017 at 16:04
  • 1
    This doesn't actually work.
    – NelsonGon
    Sep 7, 2019 at 9:13
  • The only one that worked for me of so many others
    – Himanshu
    Feb 28, 2020 at 6:34
18

If all else fails. What I did was I first changed the directory to:

cd /var/lib/dpkg/info

then I removed everything with .postinst:

sudo rm *.postinst

then update repository

sudo apt-get update

then everything went back to normal when I did:

sudo apt-get --force-yes install openjdk-7-jre-headless
5
  • 14
    This removes all post-installation scripts not just that of the affected package. A bit too overzealous in my opinion. -1 Mar 31, 2017 at 22:57
  • 1
    If it's easy to identify and remove only the particular post-install script causing a problem this can be a very convenient way to remove a failing package. Jan 6, 2018 at 12:47
  • after a nightmare with kernal issues renaming the .postinst of just the affected kernel fixed the issue
    – Andy
    Apr 9, 2018 at 20:00
  • This ( rm *.postinst ) worked for me after trying a dozen other things. Thanks!
    – mr.zog
    Apr 11, 2022 at 15:43
  • 1
    I did a mv of just the single python3 postinst that was causing problems. Thanks.
    – Ryan
    Dec 11, 2023 at 14:47
4

If all else fails, you can manually remove the package through dpkg. Running sudo dpkg -P bsnes should purge bsnes.

1
  • 4
    dpkg: error processing package libcuda1-340 (--purge): package is in a very bad inconsistent state; you should reinstall it before attempting a removal
    – endolith
    Nov 14, 2016 at 5:27
4

In my case apt-get install -f worked. I was installing mysql-workbench-community

Previously tried apt autoclean

I saw that dpkg error due to some missing dependencies which could be corrected by installing in forced mode( installing dependencies)

2

I had a similar situation with multiple packages that weren't fully installed and I didn't know what they were. To fix it I followed this answer: https://unix.stackexchange.com/a/114193

The steps were:

  1. use sudo dpkg -C to get the list of all the packages that are in an incomplete state of installation.
  2. Edit the list it provides so that it's only 1 line of just the package names.
  3. run sudo dpkg --configure [list of packages separated by spaces] on all those packages.
  4. run sudo dpkg --configure --pending to catch the few packages for which step #3 was insufficient.

However, I do wonder if perhaps step #4 is the only step that's actually needed, or perhaps sudo dpkg --configure -a && sudo dpkg --configure --pending was the single line that would have done the trick. In any case, the 4 steps above solved the problem for me.

2

Try this:

sudo dpkg -l | grep '^.[^i]'

Then for each packages which have something other than ii in the first and second column, or an ..R in the third column, remove them using:

sudo dpkg --remove --force-all package_name  ## You might need to do this a lot
sudo apt-get clean
sudo apt-get update
sudo apt-get --fix-broken install
sudo apt-get autoremove

Hopefully, this should fix it.

2
  • The i character is usually installed state or desired state. There is a description at: How to interpret the status of dpkg (–list)
    – rusty
    Jan 18, 2023 at 3:00
  • sudo apt-get --fix-broken install by itself might fix things for you (it did for me).
    – rsenna
    Jun 5, 2023 at 19:05
1
sudo apt-get install -f --reinstall coreutils init-system-helpers

After running above, you can install/upgrade/remove/purge as usual for any packages.

1
  • 1
    This worked in your specific case. But won't work in every case.
    – Error404
    Feb 6, 2022 at 9:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .