57

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.

Thanks for the help

58

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).

  • 2
    Actually this is the best method. Reinstalling dpkg doesn't always help. – Danatela Dec 24 '15 at 10:36
  • 2
    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 '16 at 23:10
  • The best answer. Thanks! Correcting .prerm and .postrm files fix the problem. – fuser May 27 '17 at 9:28
40

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>
    
  • 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 '14 at 4:22
  • @Bijak You must have surely tried purging bsnes .Have you? – Registered User Mar 24 '14 at 4:26
  • Yep, with dpkg -P, apt-get purge, and with Synaptic – Bijak Mar 24 '14 at 12:39
  • Post the output of dpkg -l |grep bsnes – Registered User Mar 24 '14 at 12:51
  • rF bsnes 0.088-7 amd64 Accurate SNES/SuperFamicom emulator – Bijak Mar 24 '14 at 13:09
25

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.

  • 3
    Why is the only correct (and accepted) answer so far down the list with less than ⅓ of the votes of the top answer? +1 – David Foerster Mar 31 '17 at 23:02
  • 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 '17 at 20:13
  • 2
    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 '17 at 16:04
13

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
  • 6
    This removes all post-installation scripts not just that of the affected package. A bit too overzealous in my opinion. -1 – David Foerster Mar 31 '17 at 22:57
  • 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. – Ian Mackinnon Jan 6 '18 at 12:47
  • after a nightmare with kernal issues renaming the .postinst of just the affected kernel fixed the issue – Andy Apr 9 '18 at 20:00
13

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

  • 3
    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 '17 at 16:28
5

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

  • 2
    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 '16 at 5:27
2

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)

0
sudo apt-get install -f --reinstall coreutils init-system-helpers

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

protected by Community Oct 1 '17 at 19:25

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.