From time to time, Ubuntu users encounter errors from apt
or apt-get
, of the form:
The package some-package needs to be reinstalled, but I can't find an archive for it.
I want to know what causes this error so that I can reproduce it and try to find safe ways of fixing it, even when the broken package is important and has many dependencies. This bit of the APT source code might give a bit of a clue, but I don't know how packages get the status that causes the error.
How can I produce this error for testing on my (Ubuntu MATE 17.10) system?
This is prompted by this recent Ask Ubuntu question and my long-standing discomfort with closing questions about this error against Apt/Synaptic needs to reinstall package but can't find the archive for it, whose answer uses dpkg --force-all
to remove the offending package. Anonymous feedback suggests this solution has worked for many users, but I am not confident that using --force-all
won't cause later problems, or that it's a good idea to use this method to remove important packages like APT.
The error "Apt/Synaptic needs to reinstall package but can't find the archive for it" is at times non-specific.
The most common time I see this error is when upgrading Ubuntu from one version to another .
Note: Ubuntu is converting from apt-get to apt , but I am using apt-get in this post. For additional information see
man apt
and https://www.debian.org/doc/manuals/debian-reference/ch02.en.htmlThe most common cause of this error is when a user has installed a package, and then removed the .deb, typically with
sudo apt-get clean
http://manpages.ubuntu.com/manpages/zesty/man8/apt-get.8.html
Then, at some point,
dpkg --reconfigure
is called, either by the user, or by upgrade, or at some point the user tries to reinstall the package is reinstalledsudo apt-get --reinstall foo
or some variation.If apt cannot locate the .deb in the repository, either because the packages was removed (rare), the repository was removed from the system, or upgrade and the package is not in the new repository, you will get the error "Apt/Synaptic needs to reinstall package but can't find the archive for it"
But the error can come from other causes.
Quoting the relevant sections from the dpkg man page ( See http://manpages.ubuntu.com/manpages/trusty/man1/dpkg.1.html for details )
If the man page(s) are tl;dr -> As a part of installation, .deb / dpkg / apt runs pre / post install scripts and other functions . If these installation / removal scripts fail for any variety of reasons, a package will be marked as "half-installed" (or some state other than not installed / installed) . In that unclean state you may sometimes also see the error "Apt/Synaptic needs to reinstall package but can't find the archive for it" . In this example, the problem is not a missing archive, but a problem in the pre/post install scripts that can not be resolved by re running them. Thus the error is at times non specific.
How to manage broken packages
First try to fix dependencies first by making sure the appropriate ppa / repositories are enabled, universe for example, or whatever repositories are needed.
How do I enable the "Universe" repository?
What are PPAs and how do I use them?
Then the infamous
Read any output and error messages and post the command and output here if you need help.
Try to reconfigure
This will run the config scripts. You may specify a package rather than -a, but -a is more helpful when you are having problems.
Read any output and error messages and post the command and output here if you need help.
Try to look at any failing script and if possible fix the error / problem in the script.
Try to remove the offending package, by force if necessary. You have to be very careful here you you can force removal of a critical package or worse set of critical packages. The following commands go from polite to less polite to outright force. Try them in order.
If any of those work, run
sudo apt-get update && sudo apt-get upgrade
, you may need to runsudo apt-get -f install
and/orsudo dpkg --configure -a
as well.If that fails, you will then need to manually remove the packages. This may involve finding all the components on your system and manually deleting them.
Use the following procedure
Remove the dpkg information (see above)
Be careful you do not remove more than needed here.
Finally remove the offending package
You should then be able to use apt
Only if necessary find and manually remove any files remaining on the system. This is a manual procedure and may include config files in
/etc
or man pages or shared data. Use find or locate to identify potential remaining files.Obviously you should take great care when deleting system files. Do not delete things you do not understand and do not use
rm -Rf
without understanding what that command will do.rm -i
is probably better as the-i
option asks for confirmation.Recreate this problem
You may be able to reproduce this error message by manually editing the dpkg status file of any package in
/var/lib/dpkg/info
and changing the status to half installedEdit the Status line so it reads
Then try to run
apt-get
.If that does not work, install a package from a ppa, run apt-get clean, remove the ppa, and then try to reinstall or reconfigure the package.
Sorry for the long post, but I hope this gives you insight into the cause and resolution of this complex error message "Apt/Synaptic needs to reinstall package but can't find the archive for it"