Ubuntu 26.04 Tutorial / Remove, Hold, and Repair Packages Safely

Separate removal, holds, locks, and dependency failures when repairing Ubuntu packages. Inspect state and proposed changes first, simulate the operation, and then apply only the required action.

Understand removal scope

Operation Effect
remove Removes software while some system configuration can remain
purge Also removes package-managed configuration, not every user file
autoremove Removes automatically marked dependencies considered unnecessary
hold Prevents normal package changes and may block security updates

Use the nonessential practice package hello, recording whether it was already installed. Install it through the earlier APT workflow if needed. Do not use kernels, networking, or login packages for removal practice.

Simulate and review removal

apt-get -s remove hello
apt-get -s purge hello
apt-get -s autoremove

Compare the proposed removals. Do not proceed if unexpected driver or service packages appear. Apply only the reviewed scope, for example sudo apt remove hello, without bypassing confirmation with -y.

Inspect and reverse a package hold

(
set -eu
if [ "$(dpkg-query -W -f='${Status}' hello 2>/dev/null)" != "install ok installed" ]; then
    printf 'Install the nonessential hello lab package first.\n' >&2
    exit 1
fi
was_held=$(apt-mark showhold | grep -x hello || true)
sudo apt-mark hold hello
apt-mark showhold
if [ "$was_held" != hello ]; then
    sudo apt-mark unhold hello
fi
apt-mark showhold
)

The example records whether installed hello was already held and unholds it only if the lab introduced the hold. Preserve a pre-existing hold. Unholding does not install updates; after failure, inspect the output and actual state. Preserve other holds and document the reason, review date, and security effect of long-term holds.

Investigate running work before lock errors

systemctl status apt-daily.service apt-daily-upgrade.service --no-pager
ps -eo pid,etime,args | grep -E "[a]pt|[d]pkg|[u]nattended"

Wait if another update is progressing normally. Deleting lock files or killing package processes blindly can interrupt database operations. An inactive service alone does not prove a stale lock; inspect relevant processes and logs.

Repair unfinished configuration and dependencies

sudo dpkg --audit
apt-get -s --fix-broken install

For interrupted configuration, review logs and backups before using sudo dpkg --configure -a. It runs maintainer scripts and can affect services. Inspect the simulated dependency repair before deciding whether to run sudo apt-get --fix-broken install.

If the cause is an unsupported or mixed-release repository, correct the origin rather than repeating repair commands. Finish with sudo dpkg --audit, sudo apt-get check, and functional service checks. Package consistency does not establish successful application-data recovery.

The official package-management documentation explains removal and dependency handling.