Ubuntu 18.04 Server / apt: install, remove, and upgrade packages
On Ubuntu 18.04 Server, use apt update to refresh package indexes and apt install, apt remove, and apt upgrade to install, remove, and upgrade packages. Refreshing an index does not upgrade programs; review the proposed package changes before removing packages or performing a system-wide package upgrade.
What APT does and what to check first
APT (Advanced Package Tool) is a collection of tools that manages software using package and dependency information from repositories. Dependencies are libraries or other packages that a program also needs. These examples cover the APT 1.6 series in Ubuntu 18.04; actual versions and proposed changes depend on your repositories and installed packages.
Standard security maintenance for Ubuntu 18.04 LTS ended in May 2023. For an existing server, also check Ubuntu Pro expanded security maintenance coverage or your operating system upgrade plan. Changing mirrors or running apt upgrade does not extend support. See the official Ubuntu release cycle.
Installing, removing, and refreshing indexes normally require sudo privileges; searching and displaying information do not. On a production server, back up settings and important data and schedule changes with possible service restarts in mind. Initially, omit automatic confirmation with -y so you can review the changes yourself.
apt is an interface intended for interactive terminal use. For automation, prefer apt-get and apt-cache when output and behavior compatibility matter. In particular, apt upgrade and apt-get upgrade do not have the same default dependency behavior. See the Ubuntu 18.04 apt manual.
Search for and install a package
The example uses tree, which displays directory structures. Substitute the package you actually need on your server, but first inspect its description and candidate version.
- Download current package information from the repositories. If this reports errors, resolve them before continuing.
sudo apt update
- Search for the package and inspect its details.
apt-cache policydistinguishes the installed version, candidate version, and package sources.
apt search tree
apt show tree
apt-cache policy tree
apt search also searches descriptions, so results may include packages without tree in their names. Inspect descriptions, dependencies, and sizes with apt show, then compare Installed and Candidate in apt-cache policy. If there is no candidate, check repository configuration and index refresh results first. See the apt-cache manual.
- Simulate installation to review packages that would be installed, upgraded, or removed, then perform the installation.
sudo apt-get --simulate install tree
--simulate does not actually change packages. Its plan reflects the state at that moment, so also read the confirmation screen immediately before the real installation. If unexpected removals appear, stop and investigate dependencies and repositories.
sudo apt install tree
- After installation, verify the package registration and whether the executable runs.
dpkg-query -W tree
tree --version
The first command displays the registered package name and version; the second displays the executable’s version information. If no newer version needs installing, APT may report that the package is already current. Required dependencies are installed together. The apt-get manual explains package operations and simulation options.
Refresh indexes and upgrade packages
update refreshes repository indexes without changing installed programs. Use the following commands to inspect available upgrades.
sudo apt update
apt list --upgradable
apt list --installed
apt list --upgradable shows packages with available upgrades, while apt list --installed shows installed packages. apt list on its own can list packages matching name criteria, but its output may be extensive.
Use sudo apt upgrade for a normal upgrade of installed packages. In Ubuntu 18.04, apt upgrade can install new dependencies when necessary but does not remove existing packages. By contrast, default apt-get upgrade also avoids installing new packages and may therefore keep some upgrades back.
sudo apt upgrade
sudo apt full-upgrade can remove existing packages to resolve dependencies. Do not run it automatically merely because a normal upgrade kept packages back: first inspect the installation and removal plan and its effect on important services. It is not a distribution upgrade command for moving Ubuntu 18.04 to another Ubuntu release.
If updates remain, check intentionally held packages with apt-mark showhold. To update only selected packages or limit changes to security updates, see upgrading specific packages or security updates. Afterwards, check apt list --upgradable again and assess separately whether running services or the kernel need restarting.
Remove packages and clean dependencies or caches
Choose a removal method according to which settings you want to keep and what should be removed. The following commands are alternatives, not a sequence that you must run in full.
| Command | What it removes |
|---|---|
apt remove tree |
Removes the package, normally leaving configuration files managed by the package. |
apt purge tree |
Removes the package and its managed configuration files. This does not mean that all files in users’ home directories are deleted. |
apt autoremove |
Removes automatically installed dependencies that APT now considers unnecessary. |
For example, simulate removal of tree to inspect everything that would be removed, then perform the actual removal. Prefix the table’s commands that make changes with sudo, as below.
sudo apt-get --simulate remove tree
sudo apt remove tree
If you also want to remove configuration files, choose sudo apt purge tree after reviewing the need. An accidentally removed package can be reinstalled with sudo apt install tree, but reinstalling may not recover customized configuration deleted by purge; keep a backup.
Review the list before cleaning dependencies too. Do not proceed if it includes tools you use directly or packages required for operations.
sudo apt-get --simulate autoremove
sudo apt autoremove
Mark a package you still need as manually installed with sudo apt-mark manual PACKAGE, even if it is proposed for automatic removal. Replace PACKAGE with the actual name. This flag does not repair unrelated dependency problems. See the apt-mark manual.
To remove downloaded package files while retaining installed programs, use sudo apt clean. sudo apt autoclean removes cached package files that can no longer be downloaded. Neither removes installed packages as autoremove does.
Check repositories and change mirrors
Ubuntu 18.04 repositories are usually configured in /etc/apt/sources.list and individual files under /etc/apt/sources.list.d/. If downloads are slow or a mirror fails, identify the host named in the error and the actual configuration file first.
cat /etc/apt/sources.list
ls /etc/apt/sources.list.d/
Choose mirrors from the official Ubuntu archive mirror list. This differs from the CD mirror list for installation ISOs. Check synchronization status and whether the mirror serves bionic as well as location; no particular mirror is guaranteed to be the fastest.
- Back up the file you will actually edit. If the backup below already exists, use another name to preserve it.
sudo cp -a /etc/apt/sources.list /etc/apt/sources.list.backup
- Open the file in the default editor and change only the relevant Ubuntu mirror URI. If the entry is in a separate file, back up and edit that file instead.
sudoedit /etc/apt/sources.list
The following line illustrates the format; do not replace the entire file with it. Preserve the needed release suites, such as bionic and bionic-updates, and existing components such as main and universe. Do not apply a global replacement to security, Ubuntu Pro, or third-party repository addresses.
deb http://archive.ubuntu.com/ubuntu bionic main restricted universe multiverse
- After saving, refresh indexes and check the candidate version and source of the required package. Do not install or upgrade while errors remain.
sudo apt update
apt-cache policy tree
See the Ubuntu 18.04 sources.list manual for repository syntax. A 404 error is not a reason to replace bionic with another Ubuntu release’s codename. Check the mirror’s coverage and the currently supported repository path first.
Troubleshoot errors and understand recovery
- Package not found: check spelling, errors from apt update, required repository components, and supported architectures. A command cannot make a package appear in a repository that does not provide it.
- Lock errors: check whether another apt or dpkg operation or an automatic update is running and wait for it to finish normally. Do not begin by deleting lock files or forcibly terminating the process.
- Signature or Release file errors: check the repository URI, release, key configuration, and server clock. Do not bypass authentication or add a repository without verifying trust.
If a previous package operation really was interrupted and no other management job is running, the following sequence can address incomplete configuration and inspect dependency repair. The first command actually performs pending configuration, so read its output. The second only shows the repair plan.
sudo dpkg --configure -a
sudo apt-get --simulate --fix-broken install
Run sudo apt --fix-broken install only after confirming that repair is needed and the plan does not unexpectedly remove important packages. Do not repeatedly apply repair commands to errors with unrelated causes.
To undo only a mirror change, restore the saved configuration file and refresh package indexes. If you edited another file, use that file’s backup.
sudo cp -a /etc/apt/sources.list.backup /etc/apt/sources.list
sudo apt update
This restores repository settings only. It does not roll back packages already installed, removed, or upgraded, or restore service data. To reverse package changes on a production server, use separately prepared configuration and data backups or snapshots and the service’s recovery procedure.




