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.

Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
Bijak
  • 1,619
  • 2
  • 12
  • 7

10 Answers10

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

  • 2
    Actually this is the best method. Reinstalling dpkg doesn't always help. – Danatela Dec 24 '15 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 06 '16 at 23:10
  • 3
    The best answer. Thanks! Correcting .prerm and .postrm files fix the problem. – fuser May 27 '17 at 09: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 05 '19 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. – Arda Basoglu Sep 29 '22 at 19:23
  • @ArdaBasoglu Ensure to understand what you commented out, or it may lead to other problems. – Skippy le Grand Gourou Sep 30 '22 at 08:17
  • Fantastic! Thanks – Sergio Abreu Sep 28 '23 at 03:41
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>
    
Registered User
  • 9,631
  • 14
  • 53
  • 85
  • 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 '14 at 04:22
  • @Bijak You must have surely tried purging bsnes .Have you? – Registered User Mar 24 '14 at 04: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
  • Well that means you haven't removed bsnes. try sudo apt-get purge --force-yes bsnes – Registered User Mar 24 '14 at 13:11
  • It exits with an error as soon as it tries to process bsnes (and it does the same thing for the broken Chrome package too). The same error as pasted in the original question – Bijak Mar 24 '14 at 13:57
  • E: Command line option --configure is not understood Isn't --configure only an option for dpkg? I don't see it in the apt-get man page. But dpkg --configure is precisely the step where the error occurs when you do something with apt-get – Bijak Mar 24 '14 at 15:25
  • Sorry, It was a mistake. Run sudo dpkg --configure bsnes – Registered User Mar 24 '14 at 16:03
  • Yeah, that fails, and is the step at which the apt-get commands fail too. 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 – Bijak Mar 24 '14 at 16:05
  • sudo apt-get --force-yes remove fixed a botched install of oracle-java7-installer on Linux Mint 18.1 for me. – Ben Mordecai Jun 25 '17 at 19:29
  • apt-get wouldn't work for me, because it complained about missing a dependency, which I couldn't install, because /boot was out of space. The dpkg way worked for me though - thanks!! – poshaughnessy Sep 21 '17 at 14:15
  • sudo dpkg -r evdi-dkms solved it for me! That package was "stuck" in a failed installation state, causing sudo apt upgrade to not work, and all sudo apt install commands for anything else to fail. – Gabriel Staples Sep 29 '23 at 02:54
50

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

Hamed
  • 780
  • 8
  • 11
  • 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 '17 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 02 '20 at 19:50
  • Thank you very much for helping resolve this issue quickly. – Stephan Mar 31 '22 at 08:49
  • The only solution. Other 20 solutions are dummy commands for camuflage – sixtytrees Jul 21 '23 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.

Bijak
  • 1,619
  • 2
  • 12
  • 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 – David Foerster Mar 31 '17 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 '17 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 '17 at 16:04
  • 1
    This doesn't actually work. – NelsonGon Sep 07 '19 at 09:13
  • The only one that worked for me of so many others – Himanshu Feb 28 '20 at 06:34
  • I had a problem with an older version of package that was interfering with my current version. It wasn't getting uninstalled properly because I tried to install the newer version. This helped correct the older installation. then I removed it completely and installed the new version – Shravya Boggarapu Oct 26 '20 at 17:01
  • Great find! This helped a lot, as did the response from Hamed! – Stephan Mar 31 '22 at 08:49
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
Zanna
  • 70,465
Mike B
  • 321
  • 14
    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
  • 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. – Ian Mackinnon Jan 06 '18 at 12:47
  • after a nightmare with kernal issues renaming the .postinst of just the affected kernel fixed the issue – Andy Apr 09 '18 at 20:00
  • This ( rm *.postinst ) worked for me after trying a dozen other things. Thanks! – mr.zog Apr 11 '22 at 15:43
  • 1
    I did a mv of just the single python3 postinst that was causing problems. Thanks. – Ryan Dec 11 '23 at 14:47
4

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

saiarcot895
  • 10,757
  • 2
  • 36
  • 39
  • 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 '16 at 05: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.

rusty
  • 16,327
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.