Apt-Get Commands - Package Management Guide

Learn essential apt-get commands for package management on Debian-based systems. Install, upgrade, and manage packages efficiently with this comprehensive guide.

Apt-Get Commands

The apt-get command is a powerful tool for managing software packages on Debian-based Linux distributions like Ubuntu. It allows users to install, update, upgrade, and remove packages from the system's repositories. Mastering these commands is crucial for system administration and development.

Package List Management

Before installing or upgrading any software, it's essential to ensure your local package index is up-to-date. This command fetches the latest information about available packages and their versions from the configured repositories.

# To fetch package list:
apt-get update

System Upgrades

Once the package list is updated, you can upgrade your installed packages. This command downloads and installs newer versions of packages that are already on your system, without removing or installing new ones.

# To download and install package updates:
apt-get upgrade

Distribution Upgrade

For a more comprehensive upgrade that includes handling dependency changes, installing new packages, and removing obsolete ones, use dist-upgrade. This is often used when upgrading to a new major version of the distribution.

# To download and install the updates AND install new necessary packages
# AND remove any packages that stand in the way of the upgrade:
apt-get dist-upgrade

A common workflow for a full system update involves combining the update and distribution upgrade commands:

# Full command for system update:
apt-get update && apt-get dist-upgrade

Installing New Packages

To install a new software package, use the install command followed by the package name(s).

# To install a new package(s):
apt-get install <package>...

Downloading Packages

Sometimes, you might want to download a package's .deb file without installing it. This can be useful for offline installations or for inspecting the package contents.

# To download a package without installing it. (The package will be downloaded in your current working dir)
apt-get download <package>

Customizing Cache Directories

You can specify custom directories for apt's cache and archives. This is an advanced option, typically used in specific deployment scenarios.

# To change cache dir and archive dir (where .deb are stored):
apt-get -o Dir::Cache="/path/to/destination/dir/" -o Dir::Cache::archives="./" install ...

Viewing Installed Packages

To see a log of installed packages, you can grep the dpkg.log file.

# To show apt-get installed packages:
grep 'install ' /var/log/dpkg.log

Handling Configuration Files During Updates

In automated scripts or batch updates, you might want to prevent apt-get from prompting for configuration file changes. The --force-confold option tells apt-get to keep the old configuration file if a new one is available.

# To silently keep old configuration during batch updates:
apt-get update -o DPkg::Options::='--force-confold' ...

For more detailed information on package management, refer to the apt-get man page and the Debian Reference.