Linux: Package Management
On Linux, you don't need to visit websites to download exe files — a package manager handles installation with a single command. Different distributions use different package managers, but the core concepts are the same.
📋 Prerequisites: You should already know
- Lesson 17: Process Management
1. What You'll Learn
- apt: update/install/remove/search
- dpkg: local deb package installation
- Adding PPA third-party sources
- snap/flatpak: containerized packages
- pip/npm/cargo: language-level package managers
2. A Story of Installing Nginx
(1) The Pain Point: On Windows, Installing Software Means Searching the Web, Downloading, Double-Clicking, and Clicking Next
Xiaoming needed to install Nginx on Ubuntu. He instinctively opened a browser and searched for "nginx download."
(2) apt Does It in One Line
Bob said: "Installing Nginx on Ubuntu takes just one command."
sudo apt update
sudo apt install nginx -y
30 seconds later Nginx was installed — dependencies handled automatically, service files created, and startup on boot configured.
(3) The Benefit: Package Managers Changed How We Install
Xiaoming discovered that apt not only installs software but automatically handles all dependency relationships. He never manually downloaded exe or dmg files again.
3. Knowledge Points
(1) apt — Debian/Ubuntu Package Manager
# Update package source lists
sudo apt update
# Upgrade all installed packages
sudo apt upgrade -y
> 💡 Tip: `apt update && apt upgrade -y` is the golden combo for system maintenance — `update` refreshes the package source list, and `upgrade` upgrades installed packages. Always run `update` before installing new software, or you may not find the latest version.
# Install software
sudo apt install nginx -y
sudo apt install curl wget git vim -y # Install multiple at once
# Remove software
sudo apt remove nginx # Keep config files
sudo apt purge nginx # Delete config files too
# Search
apt search "web server"
apt show nginx # View package info
# Clean up
sudo apt autoremove # Remove unneeded dependencies
sudo apt autoclean # Clean downloaded cache
(2) dpkg — Local deb Package Installation
# Install a local .deb file
sudo dpkg -i package.deb
# If dependencies are missing, fix with apt
sudo apt install -f
# View installed packages
dpkg -l | grep nginx
dpkg -L nginx # View files installed by the package
# Remove
sudo dpkg -r package_name # Remove
sudo dpkg -P package_name # Purge (including config files)
(3) Software Sources and PPA
# Package source list
cat /etc/apt/sources.list
# Add a PPA (Personal Package Archive)
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.12
# Ubuntu software source priority order
# 1. /etc/apt/sources.list
# 2. /etc/apt/sources.list.d/*.list
(4) snap/flatpak — Containerized Packages
# snap (built into Ubuntu)
snap find "code" # Search
sudo snap install code --classic # Install VS Code
sudo snap install lxd # Install LXD
sudo snap refresh # Update all snaps
sudo snap remove code # Remove
# snap features: auto-update, sandbox isolation, cross-distribution
ℹ️ Note: apt, snap, and flatpak are three different packaging solutions — apt installs native system packages (best performance, most lightweight), while snap/flatpak are containerized packages (better isolation, auto-updates, but slightly slower startup and larger disk usage). Prefer apt for daily use; choose snap/flatpak when you need cross-distribution support or sandbox isolation.
(5) Language-Level Package Managers
# Python
pip install requests # Install Python package
pip install -r requirements.txt # Install from file
pip list # List installed packages
pip uninstall requests # Uninstall
pip freeze > requirements.txt # Export dependencies
# Node.js
npm install express # Install package
npm install -g typescript # Global install
npm list # List installed packages
npx create-react-app my-app # Run without installing
# Rust
cargo install ripgrep # Install tool
cargo add serde # Add dependency to project
(6) ▶ Example: apt Basic Operations
# 1. Update package sources
sudo apt update
# 2. Install common dev tools
sudo apt install -y build-essential curl wget git vim htop
# 3. Search for a package
apt search "text editor" | grep -i "vim\|nano"
# 4. View package info
apt show htop
# 5. Remove
sudo apt remove htop
sudo apt autoremove # Clean up unneeded dependencies
Output:
TEXTHit:1 http://archive.ubuntu.com/ubuntu jammy InRelease Get:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB] Fetched 119 kB in 1s Reading package lists... Done Reading package lists... Done Building dependency tree... Done The following NEW packages will be installed: build-essential curl wget git vim htop 0 upgraded, 6 newly installed, 0 to remove Need to get 12.3 MB of archives. Setting up build-essential (12.9ubuntu3) ... Setting up curl (7.81.0-1) ... vim - Vi IMproved - enhanced vi editor htop - interactive processes viewer Package: htop Version: 3.0.5-7build2 Priority: optional Section: utils Reading package lists... Done Building dependency tree... Done The following packages will be REMOVED: htop 0 upgraded, 0 newly installed, 1 to remove Reading package lists... Done Building dependency tree... Done The following packages will be REMOVED: libhwloc5 0 upgraded, 0 newly installed, 1 to remove
(7) ▶ Example: dpkg Install Local Package
# Download VS Code .deb package
wget https://code.visualstudio.com/sha/download?build=stable&os=linux-deb-x64 -O code.deb
# Install
sudo dpkg -i code.deb
# If dependency issues are reported, fix them
sudo apt install -f
# View files installed by VS Code
dpkg -L code | head -20
Output:
TEXT--2026-07-07 10:23:01-- https://code.visualstudio.com/sha/download?build=stable&os=linux-deb-x64 Resolving code.visualstudio.com... 20.190.159.4 HTTP request sent, awaiting response... 302 Found Length: 95000000 (90M) [application/octet-stream] Saving to: 'code.deb' code.deb 100%[==========>] 90.51M 5.2MB/s in 18s Selecting previously unselected package code. Setting up code (1.89.1-1716721786) ... /usr/share/code/code /usr/share/code/resources/app/package.json /usr/share/code/resources/app/product.json /usr/bin/code /usr/share/applications/code.desktop /usr/share/icons/hicolor/512x512/apps/code.png
(8) ▶ Example: Adding PPA to Install Newer Python
# Ubuntu 22.04 default is Python 3.10; to install Python 3.12, you need a PPA
sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt update
sudo apt install -y python3.12 python3.12-venv python3.12-distutils
# Verify
python3.12 --version
Output:
TEXTHit:1 http://archive.ubuntu.com/ubuntu jammy InRelease Hit:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease Get:3 http://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy InRelease [23.8 kB] Fetched 23.8 kB in 1s Reading package lists... Done The following NEW packages will be installed: python3.12 python3.12-venv python3.12-distutils Setting up python3.12 (3.12.4-1+jammy1) ... Python 3.12.4
(9) ▶ Example: pip Managing Python Packages
# Install Flask web framework
pip install flask
# Create a dependency list
pip freeze > requirements.txt
# Install the same dependencies on another machine
pip install -r requirements.txt
# Install to user directory (no sudo needed)
pip install --user black
Output:
TEXTCollecting flask Downloading flask-3.0.3-py3-none-any.whl (101 kB) Installing collected packages: flask Successfully installed flask-3.0.3 flask==3.0.3 werkzeug==3.0.3 jinja2==3.1.4 Collecting black Downloading black-24.4.2-py3-none-manylinux1_x86_64.whl (2.2 MB) Installing collected packages: black Successfully installed black-24.4.2
(10) ▶ Example: Finding Installed Package Paths
# View which directories nginx was installed to
dpkg -L nginx | head -10
# View nginx config files
dpkg -L nginx | grep -E "\.conf$"
# Find which package a command belongs to
dpkg -S /usr/bin/htop
# htop: /usr/bin/htop
# Reverse — find which package a file belongs to
dpkg -S /etc/nginx/nginx.conf
# nginx: /etc/nginx/nginx.conf
Output:
TEXT/etc/nginx/nginx.conf /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default /usr/sbin/nginx /var/www/html/index.html /etc/nginx/nginx.conf /etc/nginx/sites-available/default htop: /usr/bin/htop nginx: /etc/nginx/nginx.conf
(11) ▶ Comprehensive Example: Setting Up a Web Dev Environment from Scratch
#!/bin/bash
# setup-web-env.sh - Set up a Web development environment from scratch
set -e
echo "=== Setting Up Web Development Environment ==="
# 1. Update system
echo ">>> Updating package sources..."
sudo apt update
sudo apt upgrade -y
# 2. Install basic tools
echo ">>> Installing basic tools..."
sudo apt install -y build-essential curl wget git vim htop net-tools
# 3. Install Nginx
echo ">>> Installing Nginx..."
sudo apt install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx
# 4. Install Node.js
echo ">>> Installing Node.js 20..."
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
> ⚠️ Warning: The `curl | bash` pattern (downloading a script from the internet and executing it directly) carries supply chain attack risks — if the server is compromised or DNS is hijacked, malicious code could be executed. On production servers, download the script first, review its contents, then execute it, or prefer the distribution's official repositories.
# 5. Install Python 3 + pip
echo ">>> Installing Python 3..."
sudo apt install -y python3 python3-pip python3-venv
# 6. Install Docker
echo ">>> Installing Docker..."
curl -fsSL https://get.docker.com | sudo sh
sudo usermod -aG docker $USER
# 7. Verify
echo ""
echo "=== Environment Verification ==="
echo "Nginx: $(nginx -v 2>&1)"
echo "Node.js: $(node --version)"
echo "npm: $(npm --version)"
echo "Python: $(python3 --version)"
echo "Git: $(git --version)"
echo "Docker: $(docker --version)"
echo ""
echo "✅ Environment setup complete! Some changes (like Docker) require re-login to take effect."
❓ FAQ
Q: What's the difference between
apt updateandapt upgrade? A:apt updaterefreshes the local package index (list of available packages and versions).apt upgradeactually upgrades installed packages to newer versions. Always runupdatebeforeupgrade.
Q: What's the difference between
apt removeandapt purge? A:apt removeuninstalls the package but keeps its configuration files.apt purgeuninstalls the package AND deletes its configuration files. Usepurgeif you want a clean removal.
Q: How to see which files a package installed? A:
dpkg -L package_namelists all files installed by a package.dpkg -S /path/to/filedoes the reverse — finds which package a file belongs to.
Q: Are PPAs safe? A: PPAs are third-party maintained sources with a lower security level than official repositories. Only add official PPAs from well-known projects (like deadsnakes, nginx, git-core). Unknown PPAs carry security risks.
Q: What to do about dependency conflicts? A: First try
sudo apt install -fto fix. If that fails, check for PPA conflicts (disable conflicting PPAs). As a last resort, manually download.debpackages and install withdpkg -i.
📖 Summary
- Debian/Ubuntu:
apt install/remove/purge/update/upgrade - Local packages:
dpkg -ito install.deb,apt install -fto fix dependencies - Third-party sources:
add-apt-repository ppa:xxx/yyy - Language-level packages:
pip installfor Python,npm installfor Node.js apt autoremovecleans up unneeded dependencies
📝 Exercises
- Basic (⭐): Use apt to install htop and tree, verify the installation, then use
dpkg -Lto see which files htop installed - Intermediate (⭐⭐): Use
apt search "database"to search for and install a database (like PostgreSQL or SQLite), then use pip to install a Python package (likerequestsorflask) - Challenge (⭐⭐⭐): Write a one-click environment setup script that installs git/vim/htop/nginx/nodejs/python3, checks whether each tool is already installed to avoid duplicates, and outputs a version verification report at the end