Command Functions:
The apt-mark command provides several operations for managing package states:
manual
: Mark packages as manually installed
auto
: Mark packages as automatically installed
hold
: Place packages on hold, preventing them from being upgraded
unhold
: Remove hold state from packages, allowing them to be upgraded again
showmanual
: Display packages marked as manually installed
showauto
: Display packages marked as automatically installed
showhold
: Display packages marked as held
Package Installation States:
Understanding the difference between manual and auto installation flags:
- Manually installed: Packages explicitly installed by the user; will not be automatically removed
- Automatically installed: Packages installed as dependencies; may be removed by
apt autoremove
if no longer needed
Package Selection States:
The hold/unhold operations modify the dpkg selection state:
- Hold: Package will not be upgraded, downgraded, or removed until explicitly specified
- Install: Normal state, package will be upgraded as needed
- Deinstall: Package marked for removal (but config files will be kept)
- Purge: Package marked for complete removal including configuration files
Usage Scenarios:
- System Maintenance: Marking packages as manually installed to prevent them from being auto-removed
- Stability: Holding packages at specific versions to maintain system stability
- Cleanup: Marking unnecessary packages as auto to help identify candidates for removal
- Migration: Exporting a list of manually installed packages for reinstallation on another system
- Troubleshooting: Holding problematic packages until compatible updates are available
Common Usage Patterns:
- Hold kernel packages to prevent unintended kernel updates:
sudo apt-mark hold linux-image-$(uname -r)
- Fix packages wrongly marked as auto:
sudo apt-mark manual package-name
- Keep track of what you explicitly installed:
apt-mark showmanual > ~/manual-packages.txt
- Prepare for autoremove:
apt-mark showauto
to see what might be removed
- Hold back packages during a dist-upgrade:
sudo apt-mark hold package-name
before upgrading
Integration with APT:
- Package states set by apt-mark affect
apt
, apt-get
, and other APT tools
- Packages marked as "hold" will be ignored by
apt upgrade
and apt-get upgrade
- Packages marked as "auto" may be suggested for removal by
apt autoremove
- The
apt
command can also hold packages with apt-get install package-name/stable
State Files:
apt-mark modifies these files to track package states:
/var/lib/apt/extended_states
: Stores auto/manual installation state
/var/lib/dpkg/status
: Contains package hold information
Exporting and Importing Package States:
You can use apt-mark to backup and restore package states:
- Export manually installed packages:
apt-mark showmanual > ~/manual-packages.txt
- Export held packages:
apt-mark showhold > ~/held-packages.txt
- Import manually installed packages:
cat ~/manual-packages.txt | xargs sudo apt-mark manual
- Import held packages:
cat ~/held-packages.txt | xargs sudo apt-mark hold
Advanced Filtering:
- Mark all Python packages as manually installed:
apt-mark showauto | grep ^python | xargs sudo apt-mark manual
- Hold all kernel packages:
dpkg --list | grep linux-image | awk '{print $2}' | xargs sudo apt-mark hold
- Find core packages marked as auto:
apt-mark showauto | grep -E 'ubuntu-desktop|linux-generic'
Important Notes:
- Use
sudo
when modifying package states (manual, auto, hold, unhold)
- Reading package states (showmanual, showauto, showhold) does not require root privileges
- The
apt-mark hold
command is useful for pinning packages to specific versions
- Be careful when holding kernel packages as this might prevent security updates
- Packages marked as "auto" that are no longer needed can be removed with
apt autoremove
- Using the
--simulate
option lets you preview what would happen without making changes
- Package states are maintained across upgrades, including distribution upgrades