Linux apt-get Command Complete Guide

Linux 命令大全Linux Command Encyclopedia

apt-get is a core command-line tool used to manage software packages in Debian and Debian-based Linux distributions such as Ubuntu.

apt-getIt is a front-end tool for the Advanced Packaging Tool (APT), used to handle.debpackages in the format. 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, reduces output
  • -s or --simulate: Simulate execution, do not actually install or delete
  • --reinstall: Reinstall already installed packages

Main Features

  1. Automated dependency handling: Automatically downloads and installs required dependency packages
  2. Repository management: Fetches software packages from configured software sources (repositories)
  3. System maintenance: Supports system upgrades and cleanup
  4. Command-line interface: Suitable for scripting and automated operations

apt-get Basic Usage

1. Update the package list

Before installing or upgrading software, it is recommended to update the local package list first:

sudo apt-get update

Execution result:

  • Downloads the latest package information from the configured repositories
  • Updates the local database, but does not install or upgrade 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. Uninstall packages

Remove packages but keep configuration files:

sudo apt-get remove package_name

Completely remove packages and their configuration files:

sudo apt-get purge package_name

4. Upgrade installed packages

Upgrade all upgradeable packages:

sudo apt-get upgrade

Upgrade the system (including system upgrades that may require removing old packages):

sudo apt-get dist-upgrade

apt-get Advanced 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 useless 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

Troubleshooting

1. Dependency issues

When encountering dependency issues, you can try:

sudo apt-get -f install
sudo apt-get --fix-broken install

2. Repository issues

If you encounter the "Unable to locate package" error:

  1. Check the repository configuration/etc/apt/sources.list
  2. Runsudo apt-get update
  3. Confirm that the spelling is correct

3. Lock issues

When encountering the "Could not get lock" error:

  • There may be another apt process 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 cautiously: It may remove some packages to resolve complex dependencies

Review changes: Check what will change 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 package list
apt-get install Install packages
apt-get remove Remove packages
apt-get purge Completely remove packages
apt-get upgrade Upgrade all upgradeable 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 will be able to efficiently manage Debian-based Linux systems!

Linux 命令大全Linux Command Encyclopedia

Other Extensions