Linux and servers · Intermediate · 16 min
Control package versions with apt: hold, pin and clean up
By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · apt 3.2
You will read apt policy, install an exact package version, hold it, pin it by version and by origin, undo a downgrade with care, stop autoremove taking a package you need, and trace every change in the apt history log.
On this page
Most of the time you want the newest package the archive offers. Sometimes you do not: an app breaks on the next PostgreSQL client library, a vendor supports only one agent version, or a third-party repository ships something newer than Ubuntu's own build that you never asked for. apt gives you three tools for this. A hold freezes one package where it is. A pin, written in /etc/apt/preferences.d, changes which version apt prefers, by version number or by where the package comes from. And the manual/automatic marks decide what apt autoremove is allowed to take away.
You will practise all three on a tiny package called aptdemo that you build yourself, in two versions, served from two local repositories. That way every command does something visible and nothing on your server changes version by accident. Each step ends with the same idea applied to real packages.
Step 1 · Read what apt plans to do
Refresh the package lists, then ask apt how it ranks its sources:
sudo apt-get update
apt-cache policyEach source line starts with a number: its priority. On a stock Ubuntu 26.04 server you see something like 500 http://archive.ubuntu.com/ubuntu resolute-updates/main amd64 Packages for the normal pockets and 100 for resolute-backports. The 100 /var/lib/dpkg/status line stands for whatever is installed right now.
Now ask about one package:
apt-cache policy opensslThe output has three parts:
Installed:is the version on disk, or(none).Candidate:is the versionapt-get installorapt-get upgradewould pick.- The version table lists every version apt knows about, highest first, each with its priority and the sources that offer it.
***marks the installed one.
apt picks the candidate by priority first and version number second. The rules from apt_preferences(5) that matter day to day:
1000or more: install this version even if it is a downgrade.990: what a target release (-t) gets; preferred over normal sources.500: the default for every source.100: the installed version, and archives such as backports. Installed only when nothing better exists.- Below
0: never install this version.
Whatever the number, apt never downgrades an installed package unless some version has a priority of 1000 or more.
Check it worked
apt-cache policy openssl shows Installed: and Candidate: lines. If they differ, an upgrade is waiting for that package.
Step 2 · Build a practice repository with two versions
Install apt-utils, which provides apt-ftparchive to index the repository. dpkg-deb is already on every Ubuntu system.
sudo apt-get install -y apt-utilsDefine a small shell function that builds a package, then build three of them: a data package that aptdemo depends on, and aptdemo in versions 1.0-1 and 2.0-1. Everything is built in ~/apt-pin-demo, a folder you will delete at the end.
make_pkg() {
name=$1; version=$2; depends=$3
dir=~/apt-pin-demo/build/${name}_${version}
mkdir -p "$dir/DEBIAN" "$dir/usr/share/$name"
echo "$name $version" > "$dir/usr/share/$name/VERSION"
{
echo "Package: $name"
echo "Version: $version"
echo "Architecture: all"
echo "Maintainer: Practice <practice@example.invalid>"
if [ -n "$depends" ]; then echo "Depends: $depends"; fi
echo "Description: Practice package for apt holds and pins"
} > "$dir/DEBIAN/control"
dpkg-deb --build --root-owner-group "$dir" ~/apt-pin-demo/build/ > /dev/null
}
make_pkg aptdemo-data 1.0-1 ""
make_pkg aptdemo 1.0-1 aptdemo-data
make_pkg aptdemo 2.0-1 aptdemo-data
ls ~/apt-pin-demo/build/*.debPut version 1.0-1 and the data package in a stable repository and version 2.0-1 in an edge repository, under /srv/apt-practice so apt's download user can read them. Each repository gets a Packages index and a Release file that names its origin (Practice) and suite (stable or edge). Those two fields are what origin pins match on.
sudo mkdir -p /srv/apt-practice/stable /srv/apt-practice/edge
sudo cp ~/apt-pin-demo/build/aptdemo-data_1.0-1_all.deb ~/apt-pin-demo/build/aptdemo_1.0-1_all.deb /srv/apt-practice/stable/
sudo cp ~/apt-pin-demo/build/aptdemo_2.0-1_all.deb /srv/apt-practice/edge/
for suite in stable edge; do
sudo sh -c "cd /srv/apt-practice/$suite && apt-ftparchive packages . > Packages && apt-ftparchive -o APT::FTPArchive::Release::Origin=Practice -o APT::FTPArchive::Release::Suite=$suite release . > ../$suite.Release && mv ../$suite.Release Release"
doneRegister both as sources. Trusted: yes tells apt to accept them without a signature. That is acceptable for unsigned folders on your own disk and nowhere else: a repository on the internet always gets a signing key, as in the third-party repository tutorial.
sudo tee /etc/apt/sources.list.d/apt-practice.sources > /dev/null <<'EOF'
Types: deb
URIs: file:/srv/apt-practice/stable/
Suites: ./
Trusted: yes
Types: deb
URIs: file:/srv/apt-practice/edge/
Suites: ./
Trusted: yes
EOF
sudo apt-get update
apt-cache policy aptdemoCheck it worked
apt-cache policy aptdemo shows Installed: (none), Candidate: 2.0-1, and both versions at priority 500, one from each file:/srv/apt-practice source. Confirm the candidate:
apt-cache policy aptdemo | grep 'Candidate: 2.0-1'Step 3 · Install an exact version
Ask for a version with package=version. apt installs it even though a newer one is available:
sudo apt-get install -y aptdemo=1.0-1apt also installs aptdemo-data, because aptdemo depends on it, and marks it as automatically installed. Keep that in mind for the autoremove step.
A note on apt and apt-get. Both work, but apt is the interactive front end: its output is meant for people, can change between releases and prints a warning that it has no stable command-line interface when you pipe it. In scripts, cron jobs and configuration management, use apt-get and apt-cache, whose output and options are kept stable. This tutorial uses them throughout so you can paste any command into a script.
Check it worked
dpkg-query -W -f='${Version}\n' aptdemo | grep -x '1.0-1'
apt-mark showauto | grep -x aptdemo-dataThe first line prints 1.0-1, the second aptdemo-data. apt-cache policy aptdemo now shows *** next to 1.0-1 and still names 2.0-1 as the candidate.
Step 4 · Hold a package where it is
A hold tells apt not to upgrade, downgrade or remove a package as a side effect of anything else:
sudo apt-mark hold aptdemo
apt-mark showholdshowhold lists every held package. Next you run a normal upgrade. Be clear about what that means: sudo apt-get upgrade -y is a real upgrade of the whole server, not just the practice package. Every package with a newer version is downloaded and installed without a prompt. On a production box, or if you pay for data by the megabyte as on many prepaid connections, simulate it first. -s lists what would be installed, upgraded or held without downloading or changing anything:
apt-get -s upgradeLook for aptdemo among the packages kept back. When the list is one you are happy to install now, run the real upgrade. It upgrades the rest of the server as usual and leaves aptdemo alone:
sudo apt-get upgrade -yHolds are blunt. They block security fixes for that package too, and nothing reminds you they exist. Write down why you held a package, and check apt-mark showhold whenever you plan an upgrade. Release one with:
sudo apt-mark unhold aptdemoCheck it worked
The upgrade did not touch the held package:
dpkg-query -W -f='${Version}\n' aptdemo | grep -x '1.0-1'After unhold, apt-mark showhold no longer prints aptdemo.
Step 5 · Pin by version and by origin
Pins live in files under /etc/apt/preferences.d. Each stanza names packages, a match, and a priority. Unlike a hold, a pin still lets apt install updates that match it.
A version pin keeps aptdemo on the 1.0 line. The * is a glob, so 1.0-2 or 1.0.1-1 would still be allowed:
sudo tee /etc/apt/preferences.d/aptdemo-version.pref > /dev/null <<'EOF'
Package: aptdemo
Pin: version 1.0*
Pin-Priority: 1001
EOF
apt-cache policy aptdemoPriority 1001 means "this version, even if that requires a downgrade". Use 990 instead if you only want to prefer it and never go backwards.
Confirm the pin took effect:
apt-cache policy aptdemo | grep 'Candidate: 1.0-1'The candidate is now 1.0-1 and the version table shows 1001 against it. apt-get upgrade will leave the package where it is.
Pin a whole source
The more useful pin on servers works on a source, not a version. Here, the edge repository drops to priority 100, so its packages are installed only when you ask for them by name, the way Ubuntu treats backports. Remove the version pin first, so only one rule is in play:
sudo rm /etc/apt/preferences.d/aptdemo-version.pref
sudo tee /etc/apt/preferences.d/apt-practice-edge.pref > /dev/null <<'EOF'
Package: *
Pin: release o=Practice, a=edge
Pin-Priority: 100
EOF
apt-cache policy aptdemo | grep 'Candidate: 1.0-1'o= matches the Origin field of the repository's Release file and a= its suite. Both must match. Version 2.0-1 still exists, but at 100 it loses to 1.0-1, which the stable source offers at 500. Priority 100 does not block upgrades on its own: if 1.0-1 were only installed and no source offered it, 2.0-1 at 100 would still be taken as an upgrade. You can still take it on purpose by naming the suite:
sudo apt-get install -y aptdemo/edge
dpkg-query -W -f='${Version}\n' aptdemo | grep -x '2.0-1'The same pattern controls a third-party repository. Vendors often ask for the opposite: a high priority so that their builds win over Ubuntu's. A file such as /etc/apt/preferences.d/nginx.pref might read:
Package: *
Pin: origin nginx.org
Pin-Priority: 900Pin: origin matches the host name in the source URL; Pin: release o= matches the vendor name inside its Release file. Run apt-cache policy <package> after every pin you write and read the candidate it reports. A pin with a typo is silently ignored.
Check it worked
apt-cache policy aptdemo lists 2.0-1 with priority 100 from the edge source, and dpkg-query printed 2.0-1 after you asked for aptdemo/edge by name.
Step 6 · Downgrade only when you have to
Say version 2.0-1 broke something and you want 1.0-1 back.
Read this before you downgrade anything that matters:
- Package scripts are written for upgrades. A downgrade may leave configuration or data in a newer format than the old program understands. Databases are the classic case: never downgrade a database server package over its data directory.
- You lose the security fixes that came with the newer version.
- Dependencies can follow. apt may want to downgrade or remove other packages; read the list before you answer
y. - The old version has to be available. Ubuntu's mirrors keep the release version and the latest update, not every version in between, so the one you want may already be gone.
If you have a backup or a snapshot from before the upgrade, restoring it is usually safer. If a downgrade is still the right call, ask for the old version by number:
sudo apt-get install aptdemo=1.0-1apt shows the downgrade and waits for you to confirm. With -y it refuses with something like Packages were downgraded and -y was used without --allow-downgrades. In a script, say so explicitly:
sudo apt-get install -y --allow-downgrades aptdemo=1.0-1After a downgrade, add a hold or a version pin, or the next upgrade puts the new version straight back.
Check it worked
dpkg-query -W -f='${Version}\n' aptdemo | grep -x '1.0-1'Step 7 · Keep autoremove away from packages you need
apt records whether you asked for each package (manual) or it was pulled in as a dependency (automatic). When nothing depends on an automatic package any more, apt-get autoremove offers to remove it. Remove aptdemo and see:
sudo apt-get remove -y aptdemo
apt-get -s autoremove | grep '^Remv aptdemo-data'The simulated run prints Remv aptdemo-data [1.0-1]. That is exactly what happens on production servers when you remove a metapackage, or when a tool you installed by hand turns out to have arrived as someone else's dependency: autoremove proposes to take it, and you find out after the fact. If you want to keep the package, mark it manual:
sudo apt-mark manual aptdemo-dataRun apt-get -s autoremove before every real autoremove, and read its list. To find out why a package is installed at all, apt-cache rdepends --installed <package> shows what depends on it.
Check it worked
apt-mark showmanual | grep -x aptdemo-data
apt-get -s autoremoveThe first command prints aptdemo-data, and the simulated autoremove no longer mentions it.
Step 8 · See what changed and when
apt writes every transaction to /var/log/apt/history.log: when it started and ended, the exact command, and which packages were installed, upgraded, downgraded or removed, with old and new versions.
sudo grep -E '^(Start-Date|Commandline|Install|Upgrade|Downgrade|Remove):' /var/log/apt/history.log | tail -n 20Find the downgrade you made:
sudo grep '^Downgrade: .*aptdemo' /var/log/apt/history.logIt reads something like Downgrade: aptdemo:amd64 (2.0-1, 1.0-1), with your machine's architecture after the colon: old version first, new second. The log is rotated on the schedule set in /etc/logrotate.d/apt, so older entries end up in compressed files next to it. zgrep searches all of them at once:
sudo zgrep -h 'Commandline' /var/log/apt/history.log* | tail -n 5When something broke "after last week's updates", this is where you start: find the date, read which versions moved, and decide whether to pin, hold or roll forward. /var/log/apt/term.log has the full terminal output of each run, and /var/log/dpkg.log has dpkg's own lower-level record.
Check it worked
The Downgrade: line names aptdemo with 2.0-1, 1.0-1, and the Commandline: lines show the commands you ran in this tutorial.
Step 9 · Clean up
Remove the practice packages, the pin, the sources file and the repositories, then refresh the lists so apt forgets them:
sudo apt-get purge -y aptdemo-data
sudo rm /etc/apt/preferences.d/apt-practice-edge.pref /etc/apt/sources.list.d/apt-practice.sources
sudo rm -r /srv/apt-practice
rm -r ~/apt-pin-demo
sudo apt-get updateKeep apt-utils if you like; it is small and other tools use it. Remove it with sudo apt-get remove apt-utils if you installed it only for this.
Check it worked
apt-cache policy aptdemo
apt-mark showhold
ls /etc/apt/preferences.d/apt-cache policy aptdemo prints nothing, showhold prints nothing, and no practice .pref file is left. The history log keeps its record of what you did, which is the point of it.
Next steps
- Let security fixes install themselves while your holds and pins stay in force: /tutorials/automatic-security-updates-ubuntu
- Add a vendor repository with its own signing key, then pin it: /tutorials/add-a-third-party-apt-repository-safely
- Find out whether the updates you just installed need a reboot: /tutorials/know-when-ubuntu-needs-a-reboot-after-updates
- Free the space old package files take up: /tutorials/find-what-is-using-disk-space-linux
Related tutorials
- Write safer Bash scripts with strict mode and trapsYou will build a small Bash release script that stops on the first error, says which line failed, survives file names with spaces and always removes its temporary files.
- Write least-privilege sudo rules with visudoYou will replace blanket sudo access with checked rule files in /etc/sudoers.d that let two operators restart one service and edit one file, give an automation account a password-free command list, and audit what every account can run.
- Upgrade a server from Ubuntu 24.04 LTS to 26.04 LTSYou will take a backed-up Ubuntu 24.04 server through do-release-upgrade to 26.04 LTS over SSH with a fallback login, then confirm services, repositories and language runtimes work on the new release.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials