Working with Package Management (Debian/Ubuntu)
1. Updating Package Lists
| Command | Description | Notes |
|---|---|---|
| sudo apt update | Fetches the latest package lists from repositories | Updates local package index to reflect available packages |
| sudo apt list --upgradable | Lists packages that can be upgraded | Shows packages with newer versions available |
| apt list --upgradable | Lists upgradable packages for current user only | Does not require root privileges |
Example: Updating package lists and checking for upgrades
$ sudo apt update
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [257 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [109 kB]
...
$ apt list --upgradable
Listing... Done
firefox/focal-updates 84.0.2+build3-0ubuntu0.20.04.1 amd64 [upgradable from: 84.0.1+build2-0ubuntu0.20.04.1]
Note: Running
apt update does not install updates, it only refreshes the local package list.2. Upgrading Packages
| Command | Description | Notes |
|---|---|---|
| sudo apt update | Fetches the latest package lists from repositories | Run before upgrading to ensure latest versions are available |
| sudo apt upgrade | Upgrades all installed packages to their latest versions | Does not install/remove packages, only upgrades existing ones |
| sudo apt full-upgrade | Upgrades packages and handles dependency changes | May remove or install new packages to resolve dependencies |
| sudo apt list --upgradable | Lists packages that can be upgraded | Useful for checking what will change before upgrading |
Note: Use
apt full-upgrade if you want to allow package removals or installations as part of the upgrade process.
Warning: Running
apt upgrade without apt update may result in outdated package information.
3. Installing Packages
| Command | Description | Example |
|---|---|---|
| apt install | Install a package and its dependencies | sudo apt install nginx |
| apt install <package>=<version> | Install a specific version of a package | sudo apt install nodejs=18.17.0-1nodesource1 |
| apt install <package1> <package2> | Install multiple packages | sudo apt install git curl wget |
Example: Installing a package with dependencies
sudo apt install apache2
This command installs Apache web server along with all required dependencies like libapr1, libaprutil1, etc.
Note: Use
sudo to run commands with root privileges.
Warning: Installing packages without updating the package list first may lead to outdated or missing dependencies.
Installation Summary
- apt install is the primary command for installing packages.
- Use
apt install <package>=<version>to specify a version. - Multiple packages can be installed in one command.
4. Removing Packages
| Command | Description | Notes |
|---|---|---|
| apt remove <package-name> | Removes the specified package but keeps configuration files | Useful for temporarily disabling a package |
| apt purge <package-name> | Removes the package and all associated configuration files | Completely cleans up package state |
| apt autoremove | Removes packages that were automatically installed as dependencies but are no longer needed | Helps clean up orphaned packages |
Note: Use
apt autoremove after removing packages to clean up unused dependencies.
5. Searching for Packages
| Command | Description | Example |
|---|---|---|
| apt search | Searches for packages matching a pattern | apt search python3 |
| apt-cache search | Searches package descriptions and names using cache | apt-cache search nodejs |
| aptitude search | Advanced search with more options than apt | aptitude search '~nnode' |
Note: The
apt search command searches all available repositories and can be slow. For faster results, use apt-cache search.
6. Showing Package Information
| Command | Description | Example |
|---|---|---|
| apt show | Display detailed information about a package | apt show nginx |
| dpkg -s | Show status information for a package | dpkg -s nginx |
| apt list --installed | List all installed packages | apt list --installed | head -n 10 |
| apt-cache show | Display package information from cache | apt-cache show python3 |
Example: Displaying package info with apt show
$ apt show nginx
Package: nginx
Version: 1.18.0-0ubuntu1
Priority: optional
Section: httpd
Origin: Ubuntu 20.04 focal-updates
Maintainer: Ubuntu Developers <ubuntu-devel@lists.ubuntu.com>
Architecture: amd64
Depends: libc6 (>= 2.28), libpcre3 (>= 2:8.39), libssl1.1 (>= 1.1.1), zlib1g (>= 1:1.2.11)
Description: A high-performance web server and a reverse proxy server
Nginx is an HTTP and reverse proxy server, a mail proxy server, and a generic
TCP/UDP proxy server. It is known for its high performance, stability,
simple configuration, and low resource usage.
Homepage: http://nginx.org/
Example: Listing installed packages
$ apt list --installed | grep -E "(nginx|apache)"
nginx/focal-updates,now 1.18.0-0ubuntu1 amd64 [installed]
apache2/focal-updates,now 2.4.41-4ubuntu2 amd64 [installed]
7. Listing Installed Packages
| Command | Description | Notes |
|---|---|---|
| dpkg -l | List all installed packages | Shows package name, version, architecture, and status |
| dpkg -l | grep ^ii | List only installed packages (filter) | Filters output to show only "ii" status (installed, not removed) |
| apt list --installed | List all installed packages with details | Shows package name, version, and repository origin |
| apt list --installed | grep -v "^\s*$" | Remove empty lines from installed list | Useful for parsing or scripting |
Note: The
dpkg -l command lists all packages managed by dpkg, including those installed via apt. The ii status indicates that the package is installed and configured.
8. Cleaning Package Cache
Use apt clean to remove all downloaded package files (`.deb` files) from the local cache directory, freeing up disk space.
| Command | Description | Notes |
|---|---|---|
| apt clean | Removes all downloaded .deb files from the cache | Does not remove installed packages |
| apt autoclean | Removes only old .deb files (no longer needed) | More selective than apt clean |
Note: The cache is located at
/var/cache/apt/archives/.Warning: Running
apt clean will remove all downloaded packages, which may be needed if you need to reinstall or downgrade packages later.9. Managing Repositories
Repositories are sources from which packages are downloaded and installed. On Debian/Ubuntu systems, repositories are managed through the /etc/apt/sources.list file and files in the /etc/apt/sources.list.d/ directory.
| Repository Type | Description | Example |
|---|---|---|
| deb | Standard binary package repository | deb http://archive.ubuntu.com/ubuntu jammy main restricted |
| deb-src | Source code repository for packages | deb-src http://archive.ubuntu.com/ubuntu jammy main |
Example: Adding a Repository
sudo add-apt-repository "deb http://ppa.launchpad.net/username/ppa/ubuntu jammy main"
Example: Removing a Repository
sudo add-apt-repository --remove "deb http://ppa.launchpad.net/username/ppa/ubuntu jammy main"
Note: After modifying repositories, run
sudo apt update to refresh the package cache.
Workflow: Adding a Third-Party Repository
- Obtain the repository URL and GPG key (if required)
- Import the GPG key using
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys [KEY] - Add the repository using
sudo add-apt-repository "deb [URL] [RELEASE] [COMPONENT]" - Update package list with
sudo apt update
| Command | Description | Usage |
|---|---|---|
| apt-add-repository | Adds a new repository to the system | apt-add-repository "deb [URL] [RELEASE] [COMPONENT]" |
| apt-key | Manages GPG keys for repositories | apt-key adv --keyserver keyserver.ubuntu.com --recv-keys [KEY] |
| apt update | Refreshes package lists from repositories | apt update |
10. Installing DEB Packages
| Command | Description | Example |
|---|---|---|
| apt install | Install a DEB package from the repository | sudo apt install nginx |
| apt-get install | Legacy command to install packages (still functional) | sudo apt-get install curl |
| dpkg -i | Install a local DEB file directly | sudo dpkg -i package.deb |
Example: Installing a local DEB package
sudo dpkg -i example-package_1.0_amd64.deb
sudo apt-get install -f
Note: Using
dpkg -i does not resolve dependencies automatically. Use apt-get install -f to fix missing dependencies after installing a local package.
Warning: Installing packages directly from .deb files can lead to dependency issues if not handled carefully.
11. Resolving Dependencies
| Dependency Type | Description | Command Example | Notes |
|---|---|---|---|
| Runtime Dependencies | Libraries or packages required at runtime | ldd /path/to/executable |
Shows shared library dependencies |
| Build Dependencies | Libraries or packages required to compile software | dpkg-buildpackage -B |
Used during package creation |
| Conflicts | Dependencies that cannot coexist | apt-cache policy package-name |
Lists conflicts and version constraints |
Note: Use
apt-cache depends package-name to see all dependencies and apt-cache rdepends package-name for reverse dependencies.
Warning: Manually installing packages without resolving dependencies can lead to broken system state.
12. Holding Package Versions
| Command | Description | Example |
|---|---|---|
| apt-mark hold <package-name> | Prevents a package from being automatically upgraded | sudo apt-mark hold nginx |
| apt-mark unhold <package-name> | Removes the hold on a package, allowing upgrades again | sudo apt-mark unhold nginx |
| dpkg --get-selections | grep hold | Lists all packages currently on hold | dpkg --get-selections | grep hold |