Apt Get Command
Understanding Apt Get for Package Management
The apt-get command is a powerful command-line tool used for handling packages on Debian-based Linux distributions like Ubuntu. It allows users to install, remove, update, and upgrade software packages efficiently. Mastering apt-get is crucial for any Linux user or administrator managing systems.
Core Apt Get Operations
Here are some of the most common and essential apt-get commands:
Updating Package Lists
Before installing or upgrading any software, it's vital to update your local package index. This command fetches the latest information about available packages from your configured repositories.
# Update the local database of available packages, as discovered from package
# index file found in their sources. This does not actually update your
# installed software! For that, keeping reading.
apt-get update
Upgrading Installed Packages
Once your package lists are updated, you can upgrade your installed software. The upgrade command installs newer versions of all currently installed packages, but it will not remove any packages.
# Upgrade installed packages, but there may be exceptions, such as important
# kernel packages. Also, packages will not be removed, like if they're
# deprecated, with this method.
apt-get upgrade
Performing a Full Distribution Upgrade
For a more comprehensive upgrade that can handle dependency changes and potentially install new packages or remove obsolete ones, use dist-upgrade. This is often used when upgrading to a new minor release of your distribution.
# Unlike the above, this will upgrade all of the installed packages, and
# perform other actions required for a successful and thorough upgrade. This
# will also allow for upgrading to the next minor release of your
# distributions, such as from Ubuntu '16.04.1' to '16.04.2'.
apt-get dist-upgrade
Cleaning Package Cache
Over time, downloaded package files (.deb archives) can accumulate in the cache, consuming disk space. The clean command removes these cached files.
# Clean out (completely) the follow locations of saved DEB files:
#
# /var/cache/apt/archives/* /var/cache/apt/archives/partial/
# /var/lib/apt/lists/partial/
# /var/cache/apt/pkgcache.bin /var/cache/apt/srcpkgcache.bin
#
# This will also, thanks to the provided flag, be somewhat verbose.
apt-get clean -s
Viewing Package Changelogs
It's often useful to check the changelog of a package, especially before or after an upgrade, to understand what has changed.
# View the changelog for the firefox package. Useful prior to or after upgrade.
apt-get changelog firefox
Downloading Packages
You can download package files without installing them, which is useful for offline installations or for transferring packages to another system.
# Download PKG (one or more) without actually installing or extracting them. A
# good use for this might be to upgrade an offline system, by downloading the
# packages on a system using an Internet-able machine. Files are downloaded
# into the CWD.
apt-get download PKG
Installing Software Packages
The primary function of apt-get is to install new software. This command downloads and installs the specified package along with any necessary dependencies.
# Install PKG (one or more), bringing in dependencies and, provided settings
# allow it, install recommended and/or suggested packages.
apt-get install PKG
Fixing Broken Dependencies
Sometimes, package installations can leave the system in a broken state due to unmet dependencies. The -f install option attempts to fix these issues.
# At times, dependencies won't be installed, yet you still need them; the
# following command will often fix this, and is usually suggested to the user.
apt-get -f install
Removing Software Packages
To uninstall a package and remove its configuration files, use the purge command. If you only want to remove the package itself but keep configuration files, use remove.
# Remove PKG, while also purging system-wide configuration files for it.
apt-get purge PKG
# Alternative syntax to the above.
apt-get remove --purge PKG
Combining Apt Get Commands
You can chain commands together for more complex operations. A common pattern is to update the package list and then perform a full system upgrade.
# Often used to first update the local database of packages, then, only if
# successful, a full system upgrade is started.
apt-get update && apt-get dist-upgrade
Advanced Package Downloading
For more advanced scenarios, you can download a package along with all its important dependencies.
# Download specified package (firefox, in this example) and all packages marked
# thereby as important or dependencies. Files are downloaded into the CWD.
apt-get download firefox `apt-cache --important depends firefox |
awk '{if(NR>1){printf("%s ", $2)}}'`