dotlinux blog

Speed Up Ubuntu – Clean Unused Packages with Terminal

Ubuntu is celebrated for its lightning-fast performance and user-friendly interface—especially when you first install it. But over time, even the snappiest Ubuntu setup can start to lag. You might notice longer boot times, slower package manager operations, or a dwindling amount of free disk space. The culprit? Unused packages, residual files, and outdated kernels that accumulate from months of installing apps, updating your system, and experimenting with software.

While GUI tools like BleachBit exist for cleaning, the terminal offers a more efficient, granular, and powerful way to strip out unnecessary bloat. This guide will walk you through every step of cleaning unused packages to revitalize your Ubuntu system, with clear explanations of each command so you know exactly what you’re doing.


2026-03

Table of Contents#

  1. Why Unused Packages Drain Your Ubuntu’s Performance
  2. Prerequisites
  3. Step-by-Step Guide to Clean Unused Packages with Terminal
  4. Advanced Cleaning Tips for Power Users
  5. Post-Cleanup Checks to Ensure System Health
  6. Precautions to Avoid Accidental Damage
  7. Conclusion
  8. References

1. Why Unused Packages Drain Your Ubuntu’s Performance#

Before diving into commands, let’s understand how unused packages hurt your system:

a. Disk Space Bloat#

Every time you install an app, Ubuntu stores cached .deb files, dependent libraries, and configuration files. When you uninstall an app, many of these residual files stick around. Over months, this adds up—old kernels alone can take up 1-2 GB of space in the /boot partition.

b. Slow Package Manager Operations#

A cluttered APT package database means apt (Ubuntu’s default package manager) has to sift through thousands of unused entries to find what it needs. This makes updates, installs, and searches take longer.

c. System Instability Risks#

Outdated or orphaned packages (dependencies left behind after their parent app is removed) can cause conflicts with newer software. This might lead to unexpected crashes, broken apps, or errors during system updates.

d. Longer Boot Times#

Old kernel versions listed in the GRUB menu force your system to load more options during boot, increasing startup time. They also take up space in the /boot partition, which is often small and can fill up quickly.


2. Prerequisites#

Before you start cleaning, ensure you have:

  • Basic Terminal Knowledge: You’ll need to type commands into the terminal. Open it quickly with the shortcut Ctrl + Alt + T.
  • Sudo Privileges: Most cleaning commands require admin access. You’ll need to prefix them with sudo and enter your password when prompted.
  • Backup Important Data: While the commands in this guide are safe when followed correctly, it’s always wise to back up critical files (e.g., documents, photos) to an external drive or cloud storage.

3. Step-by-Step Guide to Clean Unused Packages with Terminal#

Let’s start with the core commands to clean up your system. Each step includes an explanation so you understand its purpose.

Step 1: Update Package Lists First#

Before cleaning, make sure your system has the latest information about package states. This ensures apt knows which packages are unused or outdated.

sudo apt update

Type your password (it won’t be visible on the screen) and press Enter.

Step 2: Clean APT Cache#

Ubuntu’s APT package manager stores downloaded .deb files in /var/cache/apt/archives/ to avoid re-downloading them. Over time, this cache grows large and includes outdated files.

Option A: Remove All Cached .deb Files#

Use this command to clear the entire cache (safe when you need maximum space):

sudo apt clean

Option B: Remove Only Outdated Cached Files#

If you want to keep cached files for currently available packages (to speed up future installs), use:

sudo apt autoclean

Step 3: Remove Unused Dependencies#

When you install an app, Ubuntu often installs additional libraries (dependencies) to make it work. If you uninstall the app, these dependencies are no longer needed—unless another app uses them.

sudo apt autoremove

This command lists all unused dependencies and asks for confirmation before removing them. To also delete their configuration files (freeing up more space and reducing clutter), add the --purge flag:

sudo apt autoremove --purge

Pro Tip: If you see a package in the list that you want to keep (e.g., a library you use for a custom script), cancel the command with Ctrl + C, then mark it as manually installed:

sudo apt-mark manual <package-name>

Re-run autoremove after marking the package.

Step 4: Remove Orphaned Packages#

apt autoremove catches most unused dependencies, but some orphaned packages (dependencies whose parent app was never installed or was removed long ago) might slip through the cracks. To find and remove them, use deborphan:

First, install deborphan:

sudo apt install deborphan

Then, list all orphaned packages:

sudo deborphan

To remove them (including config files):

sudo apt purge $(deborphan)

Step 5: Remove Old Kernel Versions#

Old kernel versions are one of the biggest space hogs in Ubuntu. They take up space in /boot and slow down the GRUB menu.

First, check your current running kernel (don’t remove this one!):

uname -r

Output example: 5.15.0-76-generic

Next, list all installed kernels:

dpkg --list | grep linux-image

Look for lines starting with ii (meaning installed) that don’t match your current kernel version.

Remove old kernels and their headers (replace <version> with the kernel number):

sudo apt purge linux-image-<version>-generic linux-headers-<version>-generic

Alternatively, use this command to automatically remove all unused kernels (safer for beginners):

sudo apt autoremove --purge

After removing kernels, update the GRUB boot menu:

sudo update-grub

Step 6: Fix Broken Packages#

If your system has partially installed or broken packages, they can cause errors during cleaning. Fix them with:

sudo apt --fix-broken install

To reconfigure any broken package configurations:

sudo dpkg --configure -a

4. Advanced Cleaning Tips for Power Users#

For even deeper cleaning, try these advanced commands:

a. Remove Residual Configuration Files#

When you uninstall a package with apt remove, its configuration files often remain. These are marked with rc in the dpkg list. To remove them:

sudo apt purge $(dpkg --list | grep '^rc' | awk '{print $2}')

b. Use BleachBit (Terminal Version)#

BleachBit is a popular cleaning tool that goes beyond package cleaning—it removes browser caches, system logs, temp files, and more. Install the terminal version:

sudo apt install bleachbit

Clean system-wide bloat (safe but thorough):

sudo bleachbit --clean system

To clean user-specific data (e.g., browser cache):

bleachbit --clean user

c. Use Aptitude for Granular Control#

Aptitude is a more advanced package manager that offers better control over orphaned packages. Install it:

sudo apt install aptitude

List orphaned packages:

aptitude search '~o'

Remove them (including config files):

sudo aptitude purge '~o'

5. Post-Cleanup Checks to Ensure System Health#

After cleaning, verify your system is in good shape:

  1. Check Free Disk Space:

    df -h

    Look at the / partition to see how much space you freed up.

  2. Verify No Broken Packages:

    sudo apt check

    If no errors appear, your package database is healthy.

  3. Reboot Your System: If you removed old kernels or made major changes, reboot to apply updates:

    sudo reboot

6. Precautions to Avoid Accidental Damage#

While these commands are safe if used correctly, follow these rules to stay out of trouble:

  • Never Use rm -rf for Package Cleaning: This command deletes files permanently and can erase critical system files if misused. Stick to apt commands.
  • Double-Check Packages Before Removing: Always read the list of packages to be removed by autoremove or purge before pressing Y.
  • Don’t Remove Your Current Kernel: Always run uname -r to confirm your active kernel before purging any kernel packages.
  • Avoid Over-Cleaning: Use autoclean instead of clean for regular maintenance. clean is only necessary when you need to free up a lot of space.
  • Backup Config Files: If you’re purging packages with --purge, back up any custom config files (e.g., ~/.bashrc, /etc/nginx/nginx.conf) first.

7. Conclusion#

Cleaning unused packages is one of the easiest and most effective ways to speed up Ubuntu without reinstalling the system. By using the terminal commands in this guide, you can free up disk space, reduce system clutter, speed up package manager operations, and improve overall stability.

Make cleaning a regular habit—do it once a month or after major system updates to keep your Ubuntu running like new. For beginners, sticking to apt autoremove, autoclean, and kernel removal steps will suffice. Power users can explore advanced tools like deborphan and BleachBit for deeper cleaning.


References#

  1. Ubuntu Official Documentation: APT Commands
  2. Deborphan Man Page
  3. BleachBit Official Guide
  4. Aptitude Documentation