apt Command in Ubuntu: Install, Update, and Remove Packages

Aidin

Basket of package boxes illustration for an apt command guide

Hi everyone. Almost every Linux tutorial eventually says “install it with apt”, and on Ubuntu that one command handles finding, downloading, installing, and removing software. In this guide I use apt in the Ubuntu 26.04 terminal to refresh the package list, look up a package before installing it, install two tools at once, and then remove one of them three different ways. The removal part is where I found something most guides skip: even after apt purge and apt autoremove, configuration files were still sitting in /etc.

Ubuntu 26.04 ships apt 3, so the output looks a little different from older screenshots you may have seen, with REMOVING: and Summary: blocks instead of long sentences. Everything below is what my terminal actually printed.

If you have not read it yet, the previous article on zip and unzip in the Linux terminal ends with an apt command for installing those tools.

Distro  : Ubuntu 26.04.1 LTS (Desktop)
Packages: htop, lynx (text web browser)

Why apt Needs sudo

Running apt install without sudo

apt install without sudo failing with are you root

I started the way many beginners do, without sudo.

apt install htop

apt refused with two errors (number 1 is the command, number 2 the errors). Installing software writes to system folders that only root can change, so apt cannot even take the dpkg lock file. The question at the end, are you root?, is the hint. Every apt command that changes the system needs sudo in front of it, while commands that only read, like apt search and apt show, work without it.

Refreshing the Package List

sudo apt update

sudo apt update reporting 123 packages can be upgraded
sudo apt update

apt update does not install anything. It downloads the latest list of available packages from the Ubuntu repositories, here the resolute, resolute-updates, and resolute-security suites of Ubuntu 26.04 (number 1). At the end it tells you how many installed packages have a newer version waiting, 123 on this desktop (number 2). Run it before installing anything so apt does not try to fetch a version that no longer exists on the server.

See what can be upgraded

Listing upgradable packages with apt list --upgradable
apt list --upgradable 2>/dev/null | head -n 8

apt list –upgradable shows each package with the new version and the version you have now (number 1). I limited it to the first lines with head, and 2>/dev/null hides the warning apt prints when its output goes into a pipe. To install all of those updates you would run sudo apt upgrade. I left them alone here to keep the screenshots focused on installing and removing.

Checking a Package Before Installing

apt search and apt show

Finding htop with apt search and apt show
apt search --names-only ^htop
apt show htop 2>/dev/null | head -n 8

apt search on its own also matches descriptions and returns a long list, so –names-only with ^htop keeps only package names that start with htop (number 1). apt show then displays the details, including the version that will be installed, 3.4.1-5build2 (number 2). This is a quick way to confirm the package name is right before you install it.

Installing Packages

sudo apt install with two packages

Installing htop and lynx with their dependencies
sudo apt install -y htop lynx

You can install several packages in one command by listing them. The -y option answers yes to the confirmation question in advance. apt downloaded about 1.9 MB (number 1) and then set up htop and lynx, plus three packages I never asked for: bzip2, lynx-common, and mailcap (number 2). These are dependencies, and they matter later when lynx is removed.

Typo in the package name

Unable to locate package error caused by a typo
sudo apt install htopp

A single extra letter gives Unable to locate package (numbers 1 and 2). apt only knows the exact names in its list, so check the spelling with apt search. If the name is correct and you still see this error, run sudo apt update first.

Removing Packages: remove, purge, and autoremove

sudo apt remove

Removing lynx with sudo apt remove
sudo apt remove -y lynx

apt removes only lynx itself (number 2) and points out that lynx-common and mailcap were installed automatically and are no longer needed (number 1). It does not touch them on its own.

What remove leaves behind

rc status and /etc/lynx left after apt remove
dpkg -l lynx | tail -n 1
ls /etc/lynx

dpkg still lists lynx with the status rc (number 1). It means removed, config files remain. The /etc/lynx folder is also still there with lynx.cfg and lynx.lss (number 2). This is intentional. If you reinstall later, your old settings come back.

sudo apt purge

Purging lynx with sudo apt purge

I installed lynx again and then removed it with purge instead.

sudo apt purge -y lynx

The star next to lynx* in the REMOVING list marks a purge (number 1), and apt reports Purging configuration files for lynx (number 2).

Purge done, but /etc/lynx is still there

dpkg -S showing lynx.cfg belongs to lynx-common
dpkg -l lynx | tail -n 1
dpkg -S /etc/lynx/lynx.cfg

The status is now un, which means lynx is completely gone (number 1). However, /etc/lynx still existed, so I asked dpkg which package owns lynx.cfg. The answer was lynx-common, not lynx (number 2). Purging lynx cleaned only the files that belong to lynx, and the configuration lives in a dependency.

sudo apt autoremove

sudo apt autoremove removing lynx-common and mailcap
sudo apt autoremove -y

autoremove removes the packages that were installed only as dependencies and are no longer needed, here lynx-common and mailcap (number 1), freeing 3,847 kB (number 2).

autoremove does not purge

Config files still present after autoremove
ls /etc/lynx
dpkg -l lynx-common | tail -n 1

The surprise: /etc/lynx was still there (number 1), and lynx-common now had the rc status (number 2). Plain autoremove behaves like remove, so it keeps configuration files.

Purge the leftover dependency

Purging lynx-common to delete its configuration
sudo apt purge -y lynx-common
ls /etc/lynx

Purging lynx-common by name finally removed its configuration files (numbers 1 and 2).

The /etc/lynx folder is finally gone

ls now answers No such file or directory (number 1), so nothing from lynx is left on the system.

My experience: To get the same result in fewer steps, run sudo apt purge lynx followed by sudo apt autoremove –purge. The –purge option makes autoremove delete configuration files too, so no rc entries are left behind.

Common Problems and How to Fix Them

are you root?

You ran an apt command that changes the system without sudo. Run it again with sudo in front.

Unable to locate package

Either the name is misspelled or the package list is outdated. Check the name with apt search –names-only and run sudo apt update.

Could not get lock /var/lib/dpkg/lock-frontend

Another program is already installing something, often the automatic updates that run shortly after the desktop starts. Wait a few minutes until it finishes and try again. Do not delete the lock file.

Settings are still there after removing an app

apt remove keeps configuration files on purpose. Use apt purge, and check with dpkg -S which package owns the leftover file, since it may belong to a dependency.

That covers the apt basics on Ubuntu: update, check, install, and the difference between remove, purge, and autoremove, including the configuration files that survive them. Next in the series is sudo itself, the command that makes all of this work. If apt shows you an error I did not cover, paste it in the comments.

Leave a Comment