apt-get

package managementlinux
The apt-get command is one of the most frequently used commands in Linux/Unix-like operating systems. apt-get The apt-get command is a powerful command-line tool for handling packages in Debian and Ubuntu-based Linux distributions, providing functionalities for installing, upgrading, configuring, and removing software packages.

Quick Reference

Command Name:

apt-get

Category:

package management

Platform:

linux

Basic Usage:

apt-get [options] [arguments]

Common Use Cases

  • 1

    Software installation

    Install software packages from repositories with dependency handling

  • 2

    System updates

    Keep the system up-to-date with security patches and bug fixes

  • 3

    Package removal

    Safely remove unwanted software while maintaining system integrity

  • 4

    Source code retrieval

    Download source code of packages for inspection or modification

Syntax

apt-get [options] command

Options

Option Description
-h, --help Show help message and exit
-v, --version Show the program version
-y, --yes, --assume-yes Automatically answer yes to all prompts
--assume-no Automatically answer no to all prompts
-q, --quiet Quiet mode; produce output suitable for logging
-d, --download-only Download packages only, don't install or unpack
-s, --simulate Simulate actions but don't actually change the system
--no-install-recommends Don't install recommended packages
--install-suggests Consider suggested packages as dependencies
-f, --fix-broken Attempt to fix a system with broken dependencies
-m, --ignore-missing Ignore missing packages, proceed with installation
--purge Use purge instead of remove for packages
--reinstall Reinstall packages even if already installed
--list-cleanup Clean up obsolete files from /var/lib/apt/lists
-t, --target-release Control the default release to install packages from
--auto-remove, --autoremove Remove automatically installed packages no longer needed
--allow-unauthenticated Ignore missing or invalid authentication keys
--no-upgrade Don't upgrade packages
--only-upgrade Only upgrade already installed packages

Examples

How to Use These Examples

The examples below show common ways to use the apt-get command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

# Update package lists
sudo apt-get update
# Upgrade installed packages sudo apt-get upgrade
# Install a package sudo apt-get install package-name
# Remove a package sudo apt-get remove package-name
# Remove a package and its configuration files sudo apt-get purge package-name

Advanced Examples:

# Upgrade the entire system including package removals if needed
sudo apt-get dist-upgrade
# Download a package without installing sudo apt-get download package-name # Download only the source code of a package sudo apt-get source package-name # Clean up the local repository of retrieved package files sudo apt-get clean # Download package information only sudo apt-get update -o APT::Get::List-Cleanup=0 # Install packages and automatically answer yes to prompts sudo apt-get install -y package-name # Fix broken dependencies sudo apt-get -f install # Install a specific version of a package sudo apt-get install package-name=1.2.3-4 # Download only, don't install sudo apt-get -d install package-name # Simulate an installation to see what would happen sudo apt-get -s install package-name

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Common Commands:

The apt-get command provides several subcommands for package management:

  • update: Update the package list from repositories
  • upgrade: Upgrade installed packages to newer versions
  • dist-upgrade: Upgrade packages and handle dependencies intelligently, potentially removing packages if necessary
  • install: Install new packages
  • remove: Remove packages while keeping configuration files
  • purge: Remove packages and delete their configuration files
  • autoremove: Remove automatically installed packages that are no longer needed
  • source: Download source packages
  • build-dep: Install build dependencies for a source package
  • download: Download binary packages to the current directory
  • clean: Clear out downloaded package files from the local cache
  • autoclean: Clear out older package files from the local cache
  • check: Verify that there are no broken dependencies

Apt-get vs Apt:

While apt is newer and more user-friendly, apt-get has some advantages:

  • More stable command-line interface, better for scripts
  • More backward compatibility
  • More detailed control options
  • Has been part of Debian/Ubuntu for longer
  • Supports additional low-level operations

Update vs Upgrade vs Dist-upgrade:

  • update: Refreshes the package lists from repositories, but doesn't install anything
  • upgrade: Installs newer versions of packages you already have, but won't remove packages or install new ones
  • dist-upgrade: More aggressive upgrading - will install new packages or remove existing ones if necessary to resolve dependencies

Working with Package Versions:

  • Install a specific version: apt-get install package=version
  • Hold a package at its current version: apt-mark hold package
  • List available versions: apt-cache policy package
  • Track a specific release: apt-get -t codename install package

Managing Dependencies:

  • Fix broken dependencies: apt-get -f install
  • Install without recommended packages: apt-get --no-install-recommends install package
  • Install build dependencies: apt-get build-dep package
  • View package dependencies: apt-cache depends package
  • Check for broken dependencies: apt-get check

Configuration Files:

  • /etc/apt/sources.list: Main repository configuration
  • /etc/apt/sources.list.d/: Additional repository configuration files
  • /etc/apt/apt.conf and /etc/apt/apt.conf.d/: Configuration settings for APT
  • /etc/apt/preferences and /etc/apt/preferences.d/: Package pinning configuration
  • /var/lib/apt/lists/: Package list cache files
  • /var/cache/apt/archives/: Downloaded package files

Useful Techniques:

  • Simulate installations: apt-get -s install package
  • Show what packages would be upgraded: apt-get -s upgrade
  • Download only: apt-get -d install package
  • Reinstall a package: apt-get --reinstall install package
  • Automatically answer yes: apt-get -y install package
  • Install security updates only: apt-get -o "Dpkg::Options::=--force-confdef" -o "Dpkg::Options::=--force-confold" upgrade

Important Notes:

  • Always run apt-get update before installing or upgrading to get the latest package information
  • Using sudo is required for most operations that modify the system
  • Consider using apt-get dist-upgrade regularly to ensure all dependencies are properly resolved
  • The --fix-broken or -f option is useful when dependencies are broken
  • For scripting, -y is essential to avoid interactive prompts
  • Be careful with --force-yes as it can lead to an unstable system
  • Clean the package cache occasionally with apt-get clean to free disk space
  • Use apt-get autoremove periodically to remove unused packages

Tips & Tricks

1

Use the -y option to automatically answer yes to prompts

2

Use the -s option to simulate the installation process

3

Use the -u option to perform a system upgrade

4

Use the -d option to download packages without installing them

5

Use the -f option to fix broken dependencies

Common Use Cases

Software installation

Install software packages from repositories with dependency handling

System updates

Keep the system up-to-date with security patches and bug fixes

Package removal

Safely remove unwanted software while maintaining system integrity

Source code retrieval

Download source code of packages for inspection or modification

System maintenance

Clean package caches and perform dependency maintenance tasks

Related Commands

These commands are frequently used alongside apt-get or serve similar purposes:

Use Cases

1

Software installation

Install software packages from repositories with dependency handling

2

System updates

Keep the system up-to-date with security patches and bug fixes

3

Package removal

Safely remove unwanted software while maintaining system integrity

4

Source code retrieval

Download source code of packages for inspection or modification

5

System maintenance

Clean package caches and perform dependency maintenance tasks

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the apt-get command works in different scenarios.

$ apt-get
View All Commands