Skip to main content

Installing software: apt and dpkg

Raspberry Pi OS installs software as packages from online repositories. Two tools:

  • apt: downloads packages and everything they need, then installs them. This is what you use day to day.
  • dpkg: the lower-level tool underneath, which installs and configures each package. You use it to inspect packages and to repair installs.

apt: everyday commands​

sudo apt update # refresh the list of available packages (do this first)
sudo apt upgrade -y # upgrade what is installed
sudo apt install apache2 -y # install (-y = answer yes)
sudo apt install --reinstall php # put back a package's files
sudo apt remove phpmyadmin # uninstall, KEEP its config and saved answers
sudo apt purge phpmyadmin # uninstall AND delete its config and saved answers
sudo apt autoremove -y # remove packages nothing needs any more
apt search mariadb # find packages
apt show apache2 # details of a package
apt list --installed | grep php # what is installed

update only refreshes the list; it installs nothing. If install says Unable to locate package, run update first.

remove vs purge​

Program filesConfig files in /etcSaved installer answersYour data (databases, web pages)
removedeletedkeptkeptkept
purgedeleteddeleteddeletedusually kept (MariaDB asks)

So after remove + install, the old (maybe broken) config comes back. To really start over, purge.

dpkg: looking inside​

dpkg -l | grep phpmyadmin # installed? "ii" = fine, "rc" = removed but config left, "iF"/"iU" = broken
dpkg -L apache2 # every file a package installed
dpkg -S /usr/sbin/apache2 # which package owns this file
sudo dpkg --verify apache2 # files that changed since install (damaged or edited)

Repairing a broken install​

An install cut off by a crash, power loss or Ctrl+C leaves packages half-configured. Fix them in this order:

sudo dpkg --configure -a # finish whatever was interrupted
sudo apt --fix-broken install # fetch missing dependencies
sudo apt update && sudo apt upgrade

If a package's files are damaged: sudo apt install --reinstall NAME.

If a package's config file is damaged or deleted, reinstall alone does not replace it (apt never overwrites config). Move it away and ask apt to restore missing config files:

sudo mv /etc/apache2/apache2.conf ~/apache2.conf.broken
sudo apt install --reinstall -o Dpkg::Options::="--force-confmiss" apache2

Packages that ask questions​

Some packages ask questions while installing: phpMyAdmin (which web server?), tzdata (which time zone?). The answers are saved, and you can change them later with dpkg-reconfigure. That has its own page: dpkg-reconfigure.

pip is not apt​

Python libraries for your own code (pyserial, pymysql, flask) are installed with pip inside the venv, not with apt. See Python, venv and Thonny. Some are also available as python3-... apt packages for the system Python.

Common errors​

ErrorFix
Could not open lock file /var/lib/dpkg/lock-frontend ... Are you root?Add sudo.
Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1234 (apt)Another install or the automatic updater is running. Wait a few minutes. Do not delete lock files. If it never ends: ps aux | grep -i apt, and reboot if it is stuck.
dpkg was interrupted, you must manually run 'sudo dpkg --configure -a'Run exactly that.
Unable to locate package xyzsudo apt update; check the spelling.
Release file ... is not valid yet (invalid for another Nd Nh)The Pi's clock is behind: set the date.
Temporary failure resolving 'deb.debian.org'No internet or DNS. ping -c 3 8.8.8.8 and ping -c 3 google.com (networking).
The following packages have unmet dependenciessudo apt --fix-broken install.
Hash Sum mismatch / File has unexpected sizeBad download (often a lab proxy). sudo apt clean && sudo apt update.
No space left on deviceCard full: sudo apt clean, then free space.
E: Sub-process /usr/bin/dpkg returned an error code (1)Scroll up: the real error is a few lines above. Often a service that failed to start during install.
Install hangs at a blue screenIt is waiting for you: use Tab/Space/Enter. A tiny terminal window can hide it; make it bigger.