Introduction
Ubuntu software is usually delivered as packages. Repositories publish package files plus metadata describing names, versions, dependencies, and download locations. apt works with repositories and resolves dependencies; dpkg manages the installed .deb package database at a lower level.
You will update metadata, search before installing, inspect an installed package, remove it, and install a deterministic local .deb. Every change uses a disposable practice package and can be verified from package state rather than terminal history.
Read Package State and Sources
In this step, you will distinguish repository configuration, available metadata, and installed package state.
Inspect enabled source definitions. Modern Ubuntu images may use .list or .sources files. This read-only pipeline searches both locations: grep -R searches recursively, -h hides source filenames, -s suppresses missing-file messages, and -E enables the grouped pattern. head limits the display to twelve matching lines:
grep -RhsE '^(deb |Types:|URIs:|Suites:)' /etc/apt/sources.list /etc/apt/sources.list.d 2>/dev/null | head -n 12
Ask dpkg whether Bash is installed. -W selects package records, while -f supplies a display format. ${Package}, ${Version}, and ${db:Status-Status} are dpkg fields, not shell variables, because the format is inside single quotes:
dpkg-query -W -f='package=${Package} version=${Version} status=${db:Status-Status}\n' bash
dpkg-query reads local installed state. In contrast, apt-cache policy compares the installed version with versions known from configured repositories:
apt-cache policy bash
Save a stable installed-state observation:
mkdir -p /home/labex/project/package-lab
dpkg-query -W -f='${Package} ${db:Status-Status}\n' bash > /home/labex/project/package-lab/bash-state.txt
cat /home/labex/project/package-lab/bash-state.txt
Refresh Repository Metadata
In this step, you will refresh the package index and understand what apt update changes.
Run:
sudo apt update
This downloads current repository metadata. It does not install newer versions of every package; apt upgrade is the separate operation that upgrades installed packages.
Inspect the metadata cache directory:
ls -ld /var/lib/apt/lists
find /var/lib/apt/lists -maxdepth 1 -type f | head
Record how many regular metadata files are present:
find /var/lib/apt/lists -maxdepth 1 -type f | wc -l | tr -d ' ' > /home/labex/project/package-lab/index-file-count.txt
cat /home/labex/project/package-lab/index-file-count.txt
Search, Inspect, and Install a Repository Package
In this step, you will investigate a package before installing it.
Search package names and descriptions for the tree-listing utility:
apt search '^tree$'
Read its metadata:
apt show tree
Install it with dependency handling. sudo provides the required administrative privilege, and -y automatically answers yes to this already-reviewed installation request:
sudo apt install -y tree
Verify both package state and command lookup, then use it:
dpkg-query -W -f='${Package} ${Version} ${db:Status-Status}\n' tree | tee /home/labex/project/package-lab/tree-installed.txt
command -v tree
tree -L 1 /home/labex/project/package-lab
List paths delivered by the package:
dpkg -L tree | head
Remove a Package and Verify the Result
In this step, you will remove the practice repository package and distinguish related cleanup operations.
Remove tree:
sudo apt remove -y tree
remove deletes package-installed programs but may retain package configuration. purge also removes package-owned configuration. autoremove removes dependencies that were installed automatically and are no longer needed; review its proposal before confirming on a real host.
Preview what dependency cleanup would do without changing the system. The --dry-run option performs the calculation but does not remove anything:
sudo apt autoremove --dry-run | tee /home/labex/project/package-lab/autoremove-preview.txt
Read the proposed actions before ever running apt autoremove without --dry-run, especially on a shared or production host.
The current Bash session may remember a command's old path. Clear that command-location cache, then verify the command is gone and record the package database state:
hash -r
command -v tree || echo "tree command is unavailable"
dpkg-query -W -f='${db:Status-Status}\n' tree 2>/dev/null || echo "not-installed" | tee /home/labex/project/package-lab/tree-removed.txt
Ensure a stable marker regardless of whether dpkg retains a residual record:
if dpkg-query -W -f='${db:Status-Status}' tree 2>/dev/null | grep -qx installed; then echo "still-installed"; else echo "not-installed"; fi > /home/labex/project/package-lab/tree-removed.txt
cat /home/labex/project/package-lab/tree-removed.txt
Inspect and Install a Local Deb Package
In this step, you will inspect a prepared .deb, install it through apt, run its command, and remove it cleanly.
Enter the workspace and inspect package metadata and payload without installing:
cd /home/labex/project/package-lab
dpkg-deb -I labex-greeter_1.0_all.deb
dpkg-deb -c labex-greeter_1.0_all.deb
The ./ prefix tells apt this is a local file rather than a repository package name:
sudo apt install -y ./labex-greeter_1.0_all.deb
Query and run it:
dpkg-query -W -f='${Package} ${Version} ${db:Status-Status}\n' labex-greeter
labex-greeter | tee local-package-output.txt
Remove the package after the test and prove the payload command is gone:
sudo apt remove -y labex-greeter
if command -v labex-greeter >/dev/null; then echo "still-installed"; else echo "local-package-removed"; fi > local-package-state.txt
cat local-package-state.txt
Summary
You distinguished repository metadata from installed state, refreshed apt indexes, investigated a package before installation, verified payload files and command lookup, removed a repository package, and completed the lifecycle of an inspected local .deb.



