How can I reproduce the error "package... needs to be reinstalled, but I can't find an archive for it" for testing?
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.
Solution 1:
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.html
The 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
clean
clean clears out the local repository of retrieved package files. It removes everything but the lock file from /var/cache/apt/archives/ and /var/cache/apt/archives/partial/
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 reinstalled sudo 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 )
INFORMATION ABOUT PACKAGES dpkg maintains some usable information about available packages. The information is divided in three classes: states, selection states and flags. These values are intended to be changed mainly with dselect.
Package states
not-installed The package is not installed on your system. config-files Only the configuration files of the package exist on the system. half-installed The installation of the package has been started, but not completed for some reason. unpacked The package is unpacked, but not configured. half-configured The package is unpacked and configuration has been started, but not yet completed for some reason. triggers-awaited The package awaits trigger processing by another package. triggers-pending The package has been triggered. installed The package is unpacked and configured OK.
ACTIONS
-i, --install package-file... Install the package. If --recursive or -R option is specified, package-file must refer to a directory instead. Installation consists of the following steps: 1. Extract the control files of the new package. 2. If another version of the same package was installed before the new installation, execute prerm script of the old package. 3. Run preinst script, if provided by the package. 4. Unpack the new files, and at the same time back up the old files, so that if something goes wrong, they can be restored. 5. If another version of the same package was installed before the new installation, execute the postrm script of the old package. Note that this script is executed after the preinst script of the new package, because new files are written at the same time old files are removed. 6. Configure the package. See --configure for detailed information about how this is done.
--configure package...|-a|--pending
Configure a package which has been unpacked but not yet configured. If -a or --pending is given instead of package, all unpacked but unconfigured packages are configured. To reconfigure a package which has already been configured, try the dpkg-reconfigure(8) command instead. Configuring consists of the following steps: 1. Unpack the conffiles, and at the same time back up the old conffiles, so that they can be restored if something goes wrong. 2. Run postinst script, if provided by the package. /var/lib/dpkg/status Statuses of available packages. This file contains information about whether a package is marked for removing or not, whether it is installed or not, etc. See section INFORMATION ABOUT PACKAGES for more info.
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
sudo apt-get install -f
Read any output and error messages and post the command and output here if you need help.
-
Try to reconfigure
sudo dpkg --configure -a
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.
sudo dpkg --remove $broken_package sudo dpkg --remove --force-remove-reinstreq $broken_package sudo dpkg --remove --force-all $broken_package
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)
cd /var/lib/dpkg/info sudo rm -i package_name*
Be careful you do not remove more than needed here.
Finally remove the offending package
sudo dpkg --remove --force-remove-reinstreq package_name
You should then be able to use apt
sudo apt-get update sudo apt-get install -f sudo apt-get upgrade
-
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 installed
sudo nano /var/lib/dpkg/info/some_package
Edit the Status line so it reads
Status: purge reinstreq half-installed
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"