The "Could not mark packages as held" error occurs when apt-mark fails to prevent a package from being automatically upgraded. This typically happens due to package state issues, dpkg database corruption, or administrative permission problems, and can be resolved by fixing package states, using alternative methods to hold packages, or clearing dpkg status.
This error means the APT package manager was unable to update the package hold status in its database. A "held" package is one that has been explicitly marked to prevent automatic upgrades, which is useful for maintaining compatibility or stability with specific software versions. The error indicates that something is preventing APT from writing or updating the hold information in the dpkg status database. This can occur due to several reasons: the package isn't installed on the system, the dpkg database is corrupted or inconsistent, the system doesn't have proper write permissions to the dpkg directory, or there are stale lock files preventing updates to the package database.
The apt-mark hold command only works on packages that are already installed on your system. First, check if the package is installed:
dpkg -l | grep package-nameOr more directly:
apt list --installed | grep package-nameIf the package is not installed, you must install it first before you can mark it as held:
sudo apt install package-nameOnce the package is installed, try marking it as held again:
sudo apt-mark hold package-nameIf packages are in an incomplete or unconfigured state, this can prevent hold operations. Complete the configuration of all packages:
sudo dpkg --configure -aThis command tells dpkg to configure all packages that are waiting to be configured. After running this, attempt to hold the package again:
sudo apt-mark hold package-nameStale lock files can prevent updates to the package database. Check for and remove them:
sudo rm -f /var/lib/dpkg/lock
sudo rm -f /var/lib/dpkg/lock-frontend
sudo rm -f /var/cache/apt/archives/lockAfter removing the lock files, reconfigure packages and try again:
sudo dpkg --configure -a
sudo apt-mark hold package-nameBe careful when removing lock files - ensure no apt operations are running when you do this.
If apt-mark continues to fail, you can use the lower-level dpkg command to mark a package as held:
echo "package-name hold" | sudo dpkg --set-selectionsTo verify the package is now held:
dpkg --get-selections | grep package-nameThe output should show the package with "hold" status. This method works directly with dpkg and sometimes succeeds when apt-mark fails.
Sometimes the issue stems from apt having stale or incomplete package information. Update the package lists:
sudo apt updateYou can also clear the apt cache to force a fresh sync:
sudo apt clean
sudo apt autocleanAfter cleaning, run apt update again and attempt to hold the package:
sudo apt update
sudo apt-mark hold package-nameThe dpkg status database can become corrupted. You can check and potentially repair it by:
sudo apt install aptitude
sudo aptitude install package-nameAptitude has a more robust dependency resolver that can sometimes work around minor database inconsistencies. Alternatively, if you have a backup of the dpkg status file, you can restore it:
sudo cp /var/lib/dpkg/status /var/lib/dpkg/status.bakIf the dpkg database is severely corrupted, consult system recovery procedures or consider using dpkg repair tools.
As a last resort, you can remove and reinstall the package, which should resolve any state issues:
sudo apt remove package-name
sudo apt install package-name
sudo apt-mark hold package-nameThe remove operation cleans up any corrupted state, and reinstalling gives the package a fresh installation state that can be properly held. After successful installation, the hold operation should work.
Package Hold States: Understanding package hold states is important. When you mark a package as held, it will not be included in automatic upgrades (apt upgrade, apt dist-upgrade) but can still be manually installed with apt install. Use apt-mark showhold to view all held packages on your system.
dpkg and apt Relationship: While apt-mark is the preferred interface, the underlying data is stored in the dpkg status database at /var/lib/dpkg/status. If apt-mark fails, the dpkg --set-selections method accesses the same database through dpkg directly, which sometimes works around issues.
Preventing Automatic Upgrades vs. Version Pinning: A simple hold just prevents all upgrades. If you want to pin a package to a specific version, you'll need to use dpkg pinning with /etc/apt/preferences.d/ instead of apt-mark.
Recovery from Corrupted dpkg Database: In severe cases where the dpkg database is corrupted, you may need to use sudo apt --fix-missing install or sudo apt install -f to repair the database. For severe corruption, professional system repair or reinstallation may be necessary.
E: Could not connect to proxy server
Could not connect to proxy server
E: Package 'package:i386' has no installation candidate
How to fix "Package package:i386 has no installation candidate" in apt
E: The value 'value' is invalid for APT::Default-Release
How to fix invalid APT::Default-Release value in APT
dpkg: error: unable to create new file 'path': Permission denied
How to fix dpkg permission denied errors in APT
subprocess installed post-removal script returned error exit status 1
How to fix "subprocess installed post-removal script returned error exit status 1" in APT