Complete Guide to the Linux apt-get Command
apt-get is the core command-line tool for managing software packages in Debian-based Linux distributions such as Debian and Ubuntu.
apt-get It is a front-end tool for Advanced Packaging Tool (APT), used to handle .deb format software packages. It can automatically resolve dependencies between packages, simplifying software management on Linux systems.
Basic Syntax
sudo apt-get [选项] 命令 [包名]
Common options
- -y or --yes: automatically answer "yes" to all prompts
- -q or --quiet: quiet mode, reduce output
- -s or --simulate: simulate execution without actually installing or removing
- --reinstall: reinstall an already installed package
Main Features
- Automated Dependency Handling: automatically download and install required dependency packages
- Repository Management: obtain software packages from configured repositories
- System Maintenance: supports system upgrades and cleanup
- Command-Line Interface: suitable for scripting and automation
Basic apt-get Usage
1. Update the package list
Before installing or upgrading software, it is recommended to update the local package list:
sudo apt-get update
Execution result:
- Download the latest package information from configured repositories
- Update the local database without installing or upgrading any software
2. Install packages
Install a single package:
sudo apt-get install package_name
Install multiple packages:
sudo apt-get install package1 package2 package3
Example: install the Firefox browser
sudo apt-get install firefox
3. Remove packages
Remove a package but keep its configuration files:
sudo apt-get remove package_name
Completely remove a package and its configuration files:
sudo apt-get purge package_name
4. Upgrade installed packages
Upgrade all upgradable packages:
sudo apt-get upgrade
Upgrade the system (including system upgrades that may require removing old packages):
sudo apt-get dist-upgrade
Advanced apt-get Usage
1. Search for packages
apt-cache search keyword
Example: search for packages related to Python
apt-cache search python
2. View package information
apt-cache show package_name
3. Clean up unused packages
Delete downloaded package files (.deb):
sudo apt-get clean
Delete packages that are no longer needed (automatic cleanup):
sudo apt-get autoclean
4. Fix broken dependencies
sudo apt-get -f install
Common Problem Solutions
1. Dependency problems
When you encounter dependency problems, you can try:
sudo apt-get -f install
sudo apt-get --fix-broken install
2. Repository problems
If the "Unable to locate package" error occurs:
- Check the repository configuration
/etc/apt/sources.list - Run
sudo apt-get update - Confirm the spelling is correct
3. Lock issues
When the "Could not get lock" error occurs:
- Another apt process may be running
- You can wait or manually delete the lock file
/var/lib/apt/lists/lock
Best Practices
Update regularly:
sudo apt-get update && sudo apt-get upgrade
Use dist-upgrade with caution: it may remove some packages to resolve complex dependencies
View changes: see what changes will occur before upgrading
sudo apt-get -s upgrade
Keep the system clean: regularly clean up unneeded packages
sudo apt-get autoremove
Summary of key points
| Command | Description |
|---|---|
apt-get update |
Update the package list |
apt-get install |
Install packages |
apt-get remove |
Remove packages |
apt-get purge |
Completely remove packages |
apt-get upgrade |
Upgrade all upgradable packages |
apt-get dist-upgrade |
Intelligently upgrade the system |
apt-get clean |
Clean up downloaded package files |
apt-get autoremove |
Remove automatically installed packages that are no longer needed |
Remember these core commands, and you can efficiently manage a Debian-based Linux system!
Linux Command Reference