Working With Apt on Debian and Ubuntu

Debian and Ubuntu both use Advanced Package Tool (APT) or apt for their package management. apt is the successor to apt-get and the current package manager on both Debian and Ubuntu. This article covers how to make use of apt and apt-get, mentions aptitude, and aims to make basic system administration with command line much easier.

Apt vs. Apt-get

APT used to be a combination of tools which included apt-get. apt-get was the most common tool from the loosely affiliated collection, but was the only one “required” for updates (and usually the only one on a machine). APT basically wraps up the whole set into one tool, but also adds to some of the tools. There wasn’t really an easy way to search with apt-get. Most people used aptitude (aptitude search [search string]) from command line (CLI).

You can think of these as two versions of the same product. apt-get is still relevant and can be used, but apt is preferred on newer distributions.

History Class

I’ll make this sort and sweet. Apt is based on the general Advanced Package Tool framework which apt-get used on top of dpkg, and dpkg is the Debian Package. dpkg is the base format which is implemented as a deb file. This is basically a fancy zip file with a manifest of where files go and some controls for corruption. Red Hat and derivatives use rpm (Redhat Package Manager), and other distros either have their own format of choice, a tarball, or else some kind of source package control (Gentoo’s emerge).

dpkg has many low level tools which help in ways apt can’t. You don’t go to the breaker for a single light bulb having problems. dpkg gets far closer to the core OS and has tools which are more specialized than apt for certain functions because of this hierarchy. Look up more about the dpkg or deb format to learn about the tools available.

The Basic Commands: Update and Upgrade

All of these commands should be run as root. You either run this from a root terminal or with sudo.

The first command is:

sudo apt update

This command updates the package manifest. Basically, it tells you what is available to be installed and what you can upgrade.

Ign:1 http://deb.debian.org/debian stretch InRelease
Get:2 http://deb.debian.org/debian stretch-updates InRelease [91.0 kB]
Get:3 http://deb.debian.org/debian stretch Release [118 kB]
Get:4 http://deb.debian.org/debian stretch Release.gpg [2,365 B]
Get:5 http://ftp.debian.org/debian stretch-backports InRelease [91.8 kB]
Get:6 http://deb.debian.org/debian stretch-updates/main amd64 Packages.diff/Index [11.6 kB]
Get:7 http://deb.debian.org/debian stretch-updates/main amd64 Packages 2019-09-18-2012.01.pdiff [337 B]
Get:7 http://deb.debian.org/debian stretch-updates/main amd64 Packages 2019-09-18-2012.01.pdiff [337 B]
Get:8 http://deb.debian.org/debian stretch/main amd64 Packages [7,086 kB]
Get:9 http://deb.debian.org/debian stretch/main Translation-en [5,385 kB]
Get:10 http://ftp.debian.org/debian stretch-backports/main amd64 Packages.diff/Index [27.8 kB]
Get:11 http://ftp.debian.org/debian stretch-backports/main Translation-en.diff/Index [27.8 kB]
Get:12 http://ftp.debian.org/debian stretch-backports/non-free amd64 Packages.diff/Index [27.8 kB]
Get:13 http://deb.debian.org/debian stretch/contrib amd64 Packages [50.9 kB]
Get:14 http://deb.debian.org/debian stretch/non-free amd64 Packages [78.3 kB]
Get:15 http://deb.debian.org/debian stretch/non-free Translation-en [80.3 kB]
…
Fetched 14.0 MB in 7s (1,901 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
83 packages can be upgraded. Run 'apt list --upgradable' to see them.

If you run apt list –upgradable, you’ll see a list of the packages. In order to upgrade, run the following:

sudo apt upgrade

This will look something like follows:

Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
  fuse libfuse2
Use 'sudo apt autoremove' to remove them.
The following packages will be upgraded:
  base-files dbus debian-archive-keyring e2fslibs e2fsprogs ghostscript gnupg gnupg-agent gpgv icu-devtools imagemagick imagemagick-6-common imagemagick-6.q16 ldap-utils libc-bin libc-dev-bin libc-l10n libc6 libc6-dev libcomerr2
  libcups2 libcupsfilters1 libcupsimage2 libdbus-1-3 libdns-export162 libexpat1 libgd3 libglib2.0-0 libglib2.0-data libgs9 libgs9-common libicu-dev libicu57 libisc-export160 libjs-jquery libldap-2.4-2 libldap-common
  libmagickcore-6.q16-3 libmagickcore-6.q16-3-extra libmagickwand-6.q16-3 libmariadbclient18 libnghttp2-14 libopenjp2-7 libpng-dev libpng-tools libpng16-16 libsox-dev libsox-fmt-all libsox-fmt-alsa libsox-fmt-ao libsox-fmt-base
  libsox-fmt-mp3 libsox-fmt-oss libsox-fmt-pulse libsox2 libss2 libssh2-1 libssl1.0.2 libssl1.1 libsystemd0 libudev1 libzmq3-dev libzmq5 linux-libc-dev locales multiarch-support openssh-client openssl patch rsync sox sudo systemd
  systemd-sysv tzdata udev unzip vim vim-common vim-runtime vim-tiny wget xxd
83 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 80.4 MB of archives.
After this operation, 182 kB of additional disk space will be used.
Do you want to continue? [Y/n]

Hit Y and then enter to confirm, and anything else to exit out. The process will then continue and download and apply the packages as necessary.

...
Get:83 http://deb.debian.org/debian stretch/main amd64 unzip amd64 6.0-21+deb9u2 [172 kB]
Fetched 80.4 MB in 9s (8,183 kB/s)
Extracting templates from packages: 100%
Preconfiguring packages ...
(Reading database ... 52636 files and directories currently installed.)
Preparing to unpack .../base-files_9.9+deb9u11_amd64.deb ...
Unpacking base-files (9.9+deb9u11) over (9.9+deb9u7) ...
Setting up base-files (9.9+deb9u11) ...
Installing new version of config file /etc/debian_version ...

Upgrade vs. Dist-Upgrade

We saw what apt upgrade did, but there’s another command which is: apt dist-upgrade. The difference is subtle, but apt upgrade will never remove a package while apt dist-upgrade might. dist-upgrade will upgrade flagged packages to the highest version possible (at the expense of packages included but not specifically requested), while upgrade upgrades packages flagged as safe to not cause a conflict only. They look and act the same, but one can accidentally nuke your OS, so be careful. I personally haven’t run dist-upgrade in over 3 years, and even then, it was a very specific situation.

Fixing Things

If your install gets interrupted, you want to make use of: sudo apt -f install. We’ll get into sudo apt install [package] later, but the former command tries to fix the last state of the package manager. You don’t usually have to run this unless an install breaks or something happens during installation, like a power outage. If the machine doesn’t lose power, there will probably be more to deal with than just this command.

Cleaning Up Upgrades

We previously saw:

The following packages were automatically installed and are no longer required:
  fuse libfuse2

Sometimes, when you upgrade or install new packages or remove old ones, you end up with packages which were pulled in that you don’t need. In order to remove this, run the following:

sudo apt autoremove

This cleans up the old packages and looks something like this:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
  fuse libfuse2
0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded.
After this operation, 488 kB disk space will be freed.
Do you want to continue? [Y/n]

Hit Y and enter like you did for upgrades.

Another way to clean up space is to run:

sudo apt clean

This will clean up all cached packages as located in: /var/cache/apt/archives/

These should not be manually removed! You can also run: sudo apt autoclean to only remove deb files for outdated packages only. If you maintain regularly, this won’t net you much space.

Installing and Uninstalling

apt uses several commands like install, remove, and purge. These can all take multiple packages as necessary.

apt install

sudo apt install [package]

This installs the selected package.

If the package is installed, you will see something like:

sudo apt install vim
Reading package lists... Done
Building dependency tree
Reading state information... Done
vim is already the newest version (2:8.0.0197-4+deb9u3).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

If it isn’t, you’ll be prompted after it shows you a list of what will be included:

sudo apt install emacs
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  emacs24 emacs24-bin-common emacs24-common emacs24-el emacsen-common gconf-service gconf2-common gnutls-bin libdbus-glib-1-2 libfribidi0 libgconf-2-4 libgif7 libgnutls-dane0 liblockfile-bin liblockfile1 libm17n-0 libopts25 libotf0
  libunbound2 libxft2 m17n-db
Suggested packages:
  emacs24-common-non-dfsg ncurses-term dns-root-data m17n-docs gawk
The following NEW packages will be installed:
  emacs emacs24 emacs24-bin-common emacs24-common emacs24-el emacsen-common gconf-service gconf2-common gnutls-bin libdbus-glib-1-2 libfribidi0 libgconf-2-4 libgif7 libgnutls-dane0 liblockfile-bin liblockfile1 libm17n-0 libopts25
  libotf0 libunbound2 libxft2 m17n-db
0 upgraded, 22 newly installed, 0 to remove and 0 not upgraded.
Need to get 37.1 MB of archives.
After this operation, 119 MB of additional disk space will be used.
Do you want to continue? [Y/n]

apt remove and apt purge

This just removes the selected package. The difference is that remove only removes the packaged parts and purge removes the package and all relevant configurations and cache.

sudo apt remove [package]
sudo apt purge [package]

An example of this:

sudo apt purge ed
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
  ed*
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 106 kB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 52624 files and directories currently installed.)
Removing ed (1.10-2.1) ...

apt search

This is one of the few commands which cannot be used with apt-get. Something like aptitude can be installed and used where apt may not be available (much older distributions). This is also one of the few commands which can’t take multiple arguments for the packages.

apt search [query]

An example looks like follows:

sudo apt search usbprog
Sorting... Done
Full Text Search... Done
libusbprog-dev/oldstable 0.2.0-2.2+b2 amd64
  Development files for libusbprog

libusbprog0v5/oldstable 0.2.0-2.2+b2 amd64
  Library for programming the USBprog hardware

usbprog/oldstable 0.2.0-2.2+b2 amd64
  Firmware programming tool for the USBprog hardware

usbprog-gui/oldstable 0.2.0-2.2+b2 amd64
  GUI firmware programming tool for the USBprog hardware

Package Sources

The file located at: /etc/apt/sources.list contains the file to add repositories or options to in order to adjust package sources. The folder located at: /etc/apt/sources.list.d/ contains multiple files which can be used to compartmentalize added repositories. We’ll go over both approaches.

In order to edit the sources.list file, run the following: sudo nano -w /etc/apt/sources.list. This will bring up the nano editor which is usually installed on pretty much any modern Linux distro. The instructions are on the screen. This is where you want your core OS repositories (Debian, or Ubuntu, with whatever base OS additions like Mint or some other derivative). On older Debian or Ubuntu derivatives, you may only have the file (/etc/apt/sources.list), so you would want to put all repositories in it.

It looks like follows on my system:

/etc/apt/sources.list
# 

# deb cdrom:[Debian GNU/Linux 8.0.0 _Jessie_ - Official amd64 NETINST Binary-1 20150425-12:50]/ jessie main

# deb cdrom:[Debian GNU/Linux 8.0.0 _Jessie_ - Official amd64 NETINST Binary-1 20150425-12:50]/ jessie main

# deb http://httpredir.debian.org/debian/ unstable main contrib non-free

deb http://ftp.us.debian.org/debian/ unstable main contrib non-free
deb http://httpredir.debian.org/debian unstable main contrib non-free
# deb http://mirrors.xmission.com unstable main contrib non-free
# deb-src http://ftp.us.debian.org/debian/ sid main

# deb http://security.debian.org/ sid/updates main contrib non-free
# deb-src http://security.debian.org/ sid/updates main

# jessie-updates, previously known as 'volatile'
# deb http://ftp.us.debian.org/debian/ sid-updates main contrib non-free
# deb-src http://ftp.us.debian.org/debian/ sid-updates main

# deb http://qgis.org/debian unstable main
#deb http://qgis.org/debian sid main

deb http://download.opensuse.org/repositories/isv:/ownCloud:/desktop/Debian_9.0/ /

deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main

These will differ heavily from Debian to Ubuntu and between versions. As you can see, you can add comments with # and you can comment out unnecessary mirrors or repositories. I lived somewhere with a very questionable internet connection, so some mirrors worked more reliably than others, but you rarely need more than 1. I have also done several version changes using this method, as well as adding custom packages from PPA’s and other custom software.

You can use: ls /etc/apt/sources.list.d/ to see what is available in the /etc/apt/sources.list.d/ folder. You can either edit these with nano as necessary or make a new one with: sudo nano -w /etc/apt/sources.list.d/[repository name].list. This can be named whatever you want that you’ll remember. Most repositories will give you the commands to either add it, or the entries you need to add.

Once you make your changes, refresh the package list cache by running: sudo apt update, and your package list cache will be updated. The more entries, the longer this can take, and the more entries, the more likely you are to run into incompatible packages. Try to avoid adding PPA’s where possible unless you have tested them or know what you’re adding.

Going Forward

This is just a small sampling of what apt can do. See the man (manual) pages for more with either man apt or online. Learning to combine apt and dpkg will make you far more able to admin your Debian or Ubuntu box.

See my article here for how to add third-party repositories or PPA’s using apt.

Featured image by Johannes Plenio from Pixabay