Chapter 10.1 - apt (Debian/Ubuntu)
Learning Objectives
By the end of this chapter, you will be able to:
- Use apt to install packages
- Update the system
- Manage repositories
- Resolve dependencies
- Search for packages
- Understand apt vs apt-get
What is apt?
apt (Advanced Package Tool) is the package manager for Debian and Ubuntu.
Functions:
- Package Installation/Removal
- System Update
- Dependency Management
- Package Search
Basic Commands
Update
# Update package list
sudo apt update
# Upgrade installed packages
sudo apt upgrade
# Full upgrade (includes dependency changes)
sudo apt full-upgrade
# Update and upgrade in one command
sudo apt update && sudo apt upgrade
Installation
# Install a package
sudo apt install package-name
# Install multiple packages
sudo apt install package1 package2 package3
# Install without confirmation
sudo apt install -y package-name
# Install specific version
sudo apt install package-name=version
Removal
# Remove package (keeps config)
sudo apt remove package-name
# Remove package and config
sudo apt purge package-name
# Remove unused dependencies
sudo apt autoremove
# Clean cache
sudo apt clean
sudo apt autoclean
Search and Information
Search
# Search for package
apt search keyword
# Search by exact name
apt search --names-only package-name
# View package information
apt show package-name
# List installed packages
apt list --installed
# List available packages
apt list --upgradable
Information
# Detailed information
apt show package-name
# Dependencies
apt depends package-name
# Dependent packages
apt rdepends package-name
# Package contents
dpkg -L package-name
Repositories
Configuration Files
# List repositories
cat /etc/apt/sources.list
ls /etc/apt/sources.list.d/
# Format
deb http://archive.ubuntu.com/ubuntu/ jammy main restricted
deb-src http://archive.ubuntu.com/ubuntu/ jammy main restricted
Add Repository
# Method 1: File in sources.list.d/
echo "deb http://repo.example.com/ubuntu jammy main" | \
sudo tee /etc/apt/sources.list.d/my-repo.list
# Add GPG key
wget -qO - http://repo.example.com/key.gpg | sudo apt-key add -
# Update
sudo apt update
PPA Repository (Ubuntu)
# Add PPA
sudo add-apt-repository ppa:name/ppa
# Remove PPA
sudo add-apt-repository --remove ppa:name/ppa
# Update
sudo apt update
apt vs apt-get
apt (Modern)
# Modern interface, recommended
sudo apt update
sudo apt install
sudo apt upgrade
sudo apt search
apt-get (Classic)
# Still functional
sudo apt-get update
sudo apt-get install
sudo apt-get upgrade
Difference:
aptis more user-friendly (progress bar, colors)apt-getis more low-level- Use
aptfor daily use
Practical Examples
Complete Installation
# Update
sudo apt update
# Install package
sudo apt install nginx
# Verify installation
systemctl status nginx
# Check version
nginx -v
System Update
#!/bin/bash
# Update script
echo "Updating package list..."
sudo apt update
echo "Upgrading packages..."
sudo apt upgrade -y
echo "Cleaning up..."
sudo apt autoremove -y
sudo apt autoclean
echo "Done!"
Search and Install
# Search
apt search nginx
# View information
apt show nginx
# Install
sudo apt install nginx
# Verify
dpkg -l | grep nginx
Troubleshooting
Broken Dependencies
# Fix dependencies
sudo apt --fix-broken install
# Fully repair
sudo apt --fix-missing install
sudo dpkg --configure -a
Corrupted Cache
# Clean cache
sudo apt clean
sudo apt autoclean
# Rebuild cache
sudo apt update
Locked Package
# See processes using package
sudo lsof | grep package-name
# Force installation
sudo dpkg --force-depends -i package.deb
Summary
In this chapter, you learned:
apt: Debian/Ubuntu package manager
Commands: update, upgrade, install, remove, search
Repositories: sources.list, sources.list.d/, PPA
Search: search, show, list
Problems: --fix-broken, clean, dpkg
Practice: Installation, update, maintenance
Next Steps
Chapter 10.2: yum/dnf (Red Hat/CentOS)
Chapter 10.3: Compilation from Source
Chapter created on: December 2024