Debian 13 Tutorial / Install and Remove Packages with APT and dpkg
Use Advanced Package Tool (APT) on Debian 13 to search, install and remove packages with repository and dependency handling; use dpkg tools to inspect package state and files. The remove operation can leave managed configuration files, while purge removes them too. This lab installs tree and rsync, runs a small example and compares what remains after remove and purge.
Understand APT and dpkg
A package is a unit that manages executable files, documents, configurations, and installation information together. One .deb package in Debian does not always correspond to a single command, and there are packages such as libraries or service configurations that users do not execute directly. Dependencies refer to the relationship where certain packages require other packages to function.
| Tools | Main Role | Purpose in This Course |
|---|---|---|
| apt | Interactive package management interface | Updating repository information, searching, installing, and removing |
| apt-get and apt-cache | Detailed Tasks and APT Tools for Scripts | Simulate deletion/installation plans or check candidate versions |
| dpkg and dpkg-query | Manage and query local package databases and .deb files | Check installation status, versions, file lists, and owning packages |
The apt manual explains apt as an interactive user interface. For automation, task-specific tools such as apt-get, which are more suitable for consistent output and behavior, are chosen.
The dpkg manual distinguishes between installation status and processing steps. Forcing the installation of a single .deb with dpkg does not replace resolving repository dependencies.
For a broader command overview, use the APT command reference for Debian and Ubuntu. Here the focus is the complete install, execute, remove and purge experiment on a dedicated Debian 13 VM.
Check Existing Packages and Limit the Lab Scope
Proceed on a dedicated Debian VM with a regular user who has sudo privileges. Recording server status makes it easy to compare before and after changes. If the two packages below are already in use, skip the deletion exercise or use a separate VM. Especially on production servers performing backup/synchronization with rsync, do not remove them as in this exercise.
dpkg-query -W -f='${binary:Package}\t${Version}\t${db:Status-Status}\n' tree rsync
apt-cache policy tree rsync
If a name is not installed, 'no packages found' may appear. In policy, Installed refers to the currently installed version, and Candidate refers to the installation candidate according to the current repository information. If there is no candidate, check the package name, repository, architecture, and the results of the list update. The verification environment is Debian 13.7 amd64, and at startup, both tree and rsync were absent.
Administrative commands are run with sudo. If they fail, check the error and stop at that step. The practice only installs two new packages and does not perform a full system update of the existing system.
Refresh Package Lists, Search and Install
'update' refreshes the package information in the repository. This command alone does not change installed programs to a new version. If errors related to DNS, signatures, or repository connections are output, do not pass them off as successful but check the cause.
sudo apt update
apt search '^tree$'
apt show tree
apt-cache policy tree rsync
sudo apt-get -s install --no-install-recommends tree rsync
'-s' is a simulation that shows the plan without actually installing. Read the packages that would be newly installed, removed or upgraded. If there were other package operations or repository updates after the simulation, check the actual plan again. In this VM, the new installations were the two packages tree and rsync, zero removals, and the necessary libraries were already installed. In other installation configurations, dependent packages may be added.
If the plan is appropriate, install the packages. --no-install-recommends is an option that does not automatically install recommended packages, while essential dependencies are still handled. If recommended packages are needed in a typical installation, this option can be omitted.
sudo apt install --no-install-recommends tree rsync
dpkg-query -W -f='${binary:Package}\t${Version}\t${db:Status-Status}\n' tree rsync
apt-mark showmanual tree rsync
In the final check, we look at whether the two packages are in the installed state and whether the two names requested for direct installation are listed as manually installed. The actual verification versions were tree 2.2.1-1 and rsync 3.5.0+ds1-0+deb13u1, and the versions were not forcibly fixed for installation. Tree package information and rsync package information can be used to check functions and dependencies.
Inspect Package Files and Run a Small Example
Test the installed command on a small temporary directory before using it across working data. Run the following in order in the same Debian shell so that demo_dir remains available for the later cleanup.
dpkg-query -S /usr/bin/tree
dpkg-query -L tree
demo_dir=$(mktemp -d /tmp/lf-apt-demo-09.XXXXXX)
mkdir "$demo_dir/docs" "$demo_dir/empty"
printf 'APT example\n' > "$demo_dir/docs/readme.txt"
tree -L 2 "$demo_dir"
-S shows which package provides a file, and -L shows the list of files in the installed package. The query result was tree: /usr/bin/tree. The tree output under the temporary path shows docs/readme.txt and the directory named empty, and the last line in the verification environment, including the lab’s top directory, was 3 directories, 1 file. The arbitrary string after the temporary directory changes with each execution.
The list of files managed by dpkg does not show all the data that the program creates later. If the command path is different from expected, check the actual execution target with command -v tree. There might be another command with the same name installed by the user that appears earlier in the PATH.
Observe Configuration Files After remove
tree has no suitable managed /etc configuration file for this comparison, so use the newly installed rsync package. No rsync daemon or remote synchronization job is configured; the lab service was inactive and no listener was opened on port 873. Run the backup check/copy line separately and stop if it fails. If the backup name already exists, preserve it and choose a fresh filename consistently, including in the final cleanup.
dpkg-query -W -f='${Conffiles}\n' rsync
sudo test ! -e /etc/default/rsync.before-course09 && sudo cp -a /etc/default/rsync /etc/default/rsync.before-course09
printf '\n# lf-course-09-marker\n' | sudo tee -a /etc/default/rsync
sudo apt-get -s remove tree rsync
If the backup file name already exists, use a new name. /etc/default/rsync is also included in the list of files of the Debian rsync package. Conffiles are the list of configuration files specifically managed by dpkg. Comment markers are added not to change the program's execution settings, but to check whether the modified settings are preserved.
After confirming that the targets in the simulated delete operation are only the two new exercise packages, proceed with the operation. If other work packages are removed together, stop.
sudo apt remove tree rsync
test ! -e /usr/bin/tree && printf 'tree executable removed\n'
test ! -e /usr/bin/rsync && printf 'rsync executable removed\n'
sudo grep -F 'lf-course-09-marker' /etc/default/rsync
dpkg-query -W -f='${db:Status-Status}\n' rsync
The executable file has disappeared, but the comment markers remain, and the status of rsync is shown as config-files. Just because the program is gone and the name appears in the dpkg query does not mean it is considered fully installed. At this stage, reinstalling allows you to use the remaining settings, but this does not mean that user data, cache, and runtime files for all programs are preserved in the same way.
Distinguish purge from autoremove
If the remaining settings are no longer needed, purge the practice package. If you are deleting actual operating settings, first prepare a separate backup and recovery method.
sudo apt-get -s purge rsync
sudo apt purge rsync
test ! -e /etc/default/rsync && printf 'rsync managed configuration removed\n'
sudo apt-get -s autoremove
In actual verification, the modified /etc/default/rsync remained after remove and was deleted after purge. However, a directly created backup /etc/default/rsync.before-course09 is not managed by the package, so it remains as is. Do not assume that purge will delete all settings, documents in the user home, or separate business data.
autoremove cleans up automatically installed packages that are no longer needed. It does not mean the same as purge. Since it is based on automatic installation labels and current dependency relationships, if there are packages in the list that are separately needed for operation, review manual labels and service dependencies. In this article, we only check the list and do not instruct to always execute sudo apt autoremove -y. The suggested deletions on the verification VM were zero.
When you need a local .deb file, you can resolve the necessary dependencies from the repository by specifying the path like sudo apt install ./package.deb after verifying the source. Installing files from unknown sources or ignoring dependencies with the --force option to eliminate errors is a separate issue. First, determine whether you can trust the package being installed and the package management scripts being executed.
Clean Up the Lab and Inspect APT History
Confirm that demo_dir still contains the temporary path created by this lab before removing it. If the value is empty or points elsewhere, do not delete anything. Remove the rsync backup only after confirming that it is the new lab file and is no longer needed; if you chose a different backup filename, adjust that command too.
printf '%s\n' "$demo_dir"
case "$demo_dir" in
/tmp/lf-apt-demo-09.*) rm -r -- "$demo_dir" ;;
*) printf 'Not a lab path; no deletion performed.\n' ;;
esac
sudo rm -- /etc/default/rsync.before-course09
sudo tail -n 40 /var/log/apt/history.log
Check the APT history for install, remove, and purge requests as well as modified packages. The history is a reference for identifying when a problem occurred and is not an automatic rollback command. To undo deletions, reinstall the packages and restore only the necessary configuration and data from your backups. By distinguishing between package installation success, the actual operation of commands, and whether configurations are preserved, you can apply the same criteria in future maintenance.




