Installed a .deb with dpkg, now apt is angry about missing dependencies. Or maybe you have 2GB of old kernels sitting around. Let’s clean house.

Fix broken dependencies

When dpkg install breaks apt:

1
2
3
4
5
6
7
8
# Fix missing dependencies after dpkg install
sudo apt --fix-broken install

# Or the shorter version
sudo apt install -f

# Update package lists (with retry on failure)
sudo apt update --fix-missing

Common scenario:

1
2
3
4
5
6
7
# You installed a .deb manually
sudo dpkg -i some-package.deb
# Error: dependency problems

# Fix it
sudo apt install -f
# This installs the missing dependencies

Remove old and unused packages

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Remove packages that were auto-installed but no longer needed
sudo apt autoremove

# Also remove their config files
sudo apt autoremove --purge

# Clean up downloaded package files (.deb cache)
sudo apt clean

# Remove only outdated packages from cache
sudo apt autoclean

Check for outdated packages

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Update package list
sudo apt update

# List upgradable packages
apt list --upgradable

# Upgrade all packages
sudo apt upgrade

# Upgrade with aggressive dependency resolution
sudo apt full-upgrade

Find large or old packages

1
2
3
4
5
6
7
8
# Show installed package sizes
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | tail -20

# Find old kernels (keep 1-2 latest)
dpkg --list | grep linux-image

# Remove old kernels (Ubuntu keeps 2 recent by default)
sudo apt autoremove --purge

Complete maintenance routine

1
2
3
4
5
# Full cleanup one-liner
sudo apt update && \
sudo apt upgrade -y && \
sudo apt autoremove --purge -y && \
sudo apt autoclean

Check system health

1
2
3
4
5
6
7
8
# Check for broken packages
sudo dpkg --configure -a

# Verify package integrity
sudo apt check

# See what would be removed by autoremove (dry run)
sudo apt autoremove --dry-run

Reconfigure broken packages

1
2
3
4
5
6
7
8
# Reconfigure all unpacked but not configured packages
sudo dpkg --configure -a

# Reconfigure specific package
sudo dpkg-reconfigure <package-name>

# Force reinstall broken package
sudo apt install --reinstall <package-name>

Common apt vs dpkg workflows

Installing local .deb files (correct way)

1
2
3
4
5
6
# Method 1: Use apt (handles dependencies)
sudo apt install ./package.deb

# Method 2: dpkg then fix dependencies
sudo dpkg -i package.deb
sudo apt install -f

Finding which package provides a file

1
2
3
4
5
# Search for package containing a file
apt-file search /path/to/file

# Or use dpkg for installed packages
dpkg -S /path/to/file

Hold/unhold packages (prevent updates)

1
2
3
4
5
6
7
8
# Prevent package from being updated
sudo apt-mark hold <package-name>

# Allow updates again
sudo apt-mark unhold <package-name>

# List held packages
apt-mark showhold

Check package information

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Show package details
apt show <package-name>

# List files installed by package
dpkg -L <package-name>

# Check if package is installed
dpkg -l | grep <package-name>

# Show package version
apt policy <package-name>

Completely remove package and configs

1
2
3
4
5
6
7
8
# Remove package but keep configs
sudo apt remove <package-name>

# Remove package AND configs
sudo apt purge <package-name>

# Remove + cleanup dependencies
sudo apt purge <package-name> && sudo apt autoremove --purge

Pro tip: Run sudo apt update --fix-missing && sudo apt install -f whenever apt complains about broken dependencies or missing packages. This combo fixes 90% of apt issues. Follow up with sudo apt autoremove --purge monthly to keep your system lean.