apt-mark

package managementlinux
The apt-mark command is one of the most frequently used commands in Linux/Unix-like operating systems. apt-mark The apt-mark command is used to mark packages as manually or automatically installed and to manipulate the dpkg selections, such as hold, unhold, install, or remove.

Quick Reference

Command Name:

apt-mark

Category:

package management

Platform:

linux

Basic Usage:

apt-mark [options] [arguments]

Common Use Cases

  • 1

    Version locking

    Prevent specific packages from being upgraded to maintain stability

  • 2

    Dependency management

    Control which packages are marked as manually or automatically installed

  • 3

    System migration

    Export package selection states for replication on other systems

  • 4

    Package protection

    Protect critical packages from accidental removal during cleanup

Syntax

apt-mark [options] {auto|manual|hold|unhold|showauto|showmanual|showhold} [package...]

Options

Option Description
-f, --file=FILE Read/Write package selections from/to the specified file
-h, --help Show help message
-v, --version Show the program version
-c, --config-file=FILE Use an alternate configuration file
-o, --option=STRING Set a configuration option
-q, --quiet Quiet mode - minimal output
-s, --simulate No action; perform a simulation of events that would occur
--no-act Same as --simulate

Examples

How to Use These Examples

The examples below show common ways to use the apt-mark command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

Mark a package as manually installed
sudo apt-mark manual package-name
Mark a package as automatically installed
sudo apt-mark auto package-name
Prevent a package from being upgraded
sudo apt-mark hold package-name
Allow a held package to be upgraded again
sudo apt-mark unhold package-name
Show all manually installed packages
apt-mark showmanual
Show all automatically installed packages
apt-mark showauto
Show all held packages
apt-mark showhold

Advanced Examples:

Mark all automatically installed packages that match a pattern as manually installed
apt-mark showauto | grep 'python3' | xargs sudo apt-mark manual
Mark all dependencies of a package as automatically installed
sudo apt-mark auto $(apt-cache depends package-name | grep Depends | cut -d: -f2)
Hold multiple packages at once
sudo apt-mark hold package1 package2 package3
Show packages that would be automatically removed
apt-mark showauto | xargs apt-get -s autoremove
Preserve manually installed packages when reinstalling the system
apt-mark showmanual > manual-packages.txt
# Later, restore with: # xargs sudo apt-mark manual < manual-packages.txt
Find and hold all kernel packages to prevent automatic updates
dpkg -l 'linux-image-*' | grep '^ii' | awk '{print $2}' | xargs sudo apt-mark hold
Mark all currently installed packages as manually installed
apt-mark showmanual | xargs sudo apt-mark manual

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

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

Tips & Tricks

1

Use the auto option to mark packages as automatically installed

2

Use the manual option to mark packages as manually installed

3

Use the install option to mark packages as installed

4

Use the remove option to mark packages as removed

5

Use the purge option to mark packages as purged

Common Use Cases

Version locking

Prevent specific packages from being upgraded to maintain stability

Dependency management

Control which packages are marked as manually or automatically installed

System migration

Export package selection states for replication on other systems

Package protection

Protect critical packages from accidental removal during cleanup

Troubleshooting

Hold problematic packages at working versions until bugs are fixed

Related Commands

These commands are frequently used alongside apt-mark or serve similar purposes:

Use Cases

1

Version locking

Prevent specific packages from being upgraded to maintain stability

2

Dependency management

Control which packages are marked as manually or automatically installed

3

System migration

Export package selection states for replication on other systems

4

Package protection

Protect critical packages from accidental removal during cleanup

5

Troubleshooting

Hold problematic packages at working versions until bugs are fixed

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the apt-mark command works in different scenarios.

$ apt-mark
View All Commands