The "Cannot set to hold" error occurs when attempting to hold an uninstalled package using apt-mark. This is intentional behavior by dpkg—held status only works on installed packages. Uninstalled packages can be locked from installation using APT preferences instead.
Fixes E: Cannot set to hold: package 'package-name' is not installed
# First install the package
sudo apt install package-name
# Then place a hold on it
sudo apt-mark hold package-name
# Verify the hold is set
apt-mark showhold# First install the packagesudo apt install package-name# Then place a hold on itsudo apt-mark hold package-name# Verify the hold is setapt-mark showholdE: Cannot set to hold: package 'package-name' is not installed
This error message appears when you try to place a "hold" on a package that is not currently installed on your system. The `apt-mark hold` command is designed to prevent installed packages from being automatically upgraded or removed during system updates. Since the package doesn't exist on your system yet, APT cannot place a hold on it. The underlying issue is that `dpkg` (the low-level package management system) will silently ignore hold requests for uninstalled packages. Even if the hold appears to be set initially, dpkg cleans it up the next time the package database is modified. This is intentional behavior by the dpkg developers—a hold on a non-existent package is meaningless, so it's automatically discarded.
The simplest solution is to install the package before placing a hold on it. Once installed, apt-mark hold will work correctly.
# First install the package
sudo apt install package-name
# Then place a hold on it
sudo apt-mark hold package-name
# Verify the hold is set
apt-mark showholdThis ensures the package exists in your system's package database, allowing the hold to be properly recorded and respected by dpkg.
To prevent a package from being installed in the first place, use APT preferences with a negative priority instead of trying to hold an uninstalled package.
Create or edit a file like /etc/apt/preferences.d/block-package:
sudo nano /etc/apt/preferences.d/block-packageAdd these lines to block the package:
Package: package-name
Pin: release *
Pin-Priority: -1Save the file (Ctrl+O, Enter, Ctrl+X in nano). The priority of -1 prevents the package from being installed under any circumstances, even if another package depends on it.
Test that the package cannot be installed:
sudo apt update
sudo apt install package-nameAPT should report that it cannot install the package due to the preferences restriction. You should see a message like "Unable to install package-name" with an explanation that the package is held/pinned to a lower priority.
If you change your mind and want to allow the package to be installed, remove the preferences file:
# For apt preferences:
sudo rm /etc/apt/preferences.d/block-package
sudo apt update
# For held packages (already installed):
sudo apt-mark unhold package-nameAfter removing the block, the package can be installed or upgraded normally.
apt-mark vs dpkg methods: The apt-mark hold command is a frontend to dpkg's selection mechanism. The underlying dpkg system automatically cleans up hold entries for packages that are not installed, which is why this error occurs.
APT Preferences system: The /etc/apt/preferences system is more powerful than simple holds. You can set priorities per package, per distribution, per origin, or combinations thereof. Using Pin-Priority: -1 creates a "forever excluded" state that persists even if dependencies change.
Using aptitude as alternative: The aptitude package manager has its own hold mechanism that can work with uninstalled packages. Use sudo aptitude hold package-name for packages you want to block, though aptitude hold also has limitations with uninstalled packages.
Inheritance holds: If a dependency chain brings in a package automatically, neither holds nor preferences will prevent it in all cases. Dependencies always trump holds. If you need to prevent a package that's a dependency, you may need to use a different package that provides the same functionality or modify your installation strategy entirely.
Distribution upgrades: Be cautious using apt preferences with distribution upgrades (e.g., focal→jammy). The preferences system may prevent important packages from being updated during the upgrade process. Review your preferences file before running major distribution upgrades.