The apt-get command in Linux: classic package installation

What is apt-get?

apt-get is a command-line tool that is part of the APT (Advanced Package Tool) package management system in Debian-based distributions, such as Ubuntu and Linux Mint. Its main function is to interact with software repositories to search, download, install, update, and remove packages automatically, resolving dependencies and keeping the system consistent.

How the package repository works

Before using apt-get, the system maintains an internal list of the packages available in the repositories configured in /etc/apt/sources.list and in the files of /etc/apt/sources.list.d/. This list is updated by running apt-get update, which downloads the latest information from the indicated servers.

Updating the package list

The recommended first step before any installation or update is to refresh the package index:

sudo apt-get update

This command queries each repository and downloads the Packages.gz files that contain metadata such as versions, dependencies, and descriptions. If there are changes on the servers, the local list is updated; otherwise, nothing new is downloaded.

Installing a package

To install a software, the syntax is used:

sudo apt-get install nombre_del_paquete

apt-get will search for the package in the updated list, download the corresponding .deb file and all its dependencies, then proceed to configuration. For example, to install the text editor vim:

sudo apt-get install vim

During the process, a summary of the packages that will be installed is shown and confirmation is requested before continuing.

Updating installed packages

To apply the latest versions of all already installed packages, execute:

sudo apt-get upgrade

This command only updates packages without removing any or changing dependency versions that would imply removal of packages. If a deeper update is needed that may include dependency changes, use:

sudo apt-get dist-upgrade

dist-upgrade intelligently handles dependency changes and may install or remove packages to complete the update.

Removing packages

When a software is no longer needed, it can be uninstalled in two ways:

  • sudo apt-get remove nombre_del_paquete removes the binary but leaves the configuration files on the system, allowing a quick reinstall without losing settings.
  • sudo apt-get purge nombre_del_paquete removes both the binary and the configuration files, leaving the system completely clean with respect to that package.

Searching for packages

If you do not know the exact name of the package, apt-get offers a simple search:

apt-get search palabra_clave

This command scans the list of available packages and shows those whose name or description match the keyword. To get more details about a specific package, you can use:

apt-get show nombre_del_paquete

which shows the version, dependencies, size, and a full description.

Cleaning the cache

apt-get stores the downloaded packages in /var/cache/apt/archives/. Over time, this cache can take up a lot of disk space. To free it, use:

sudo apt-get clean

which removes all stored .deb files, or:

sudo apt-get autoclean

which only deletes packages that can no longer be downloaded (obsolete versions).

Tips and best practices

  • Always run apt-get update before installing or updating.
  • Check the command output for warnings about held packages or conflicts.
  • Use apt-get -s install paquete for a simulation that shows what will be installed without making real changes.
  • On production systems, consider using apt-get upgrade --with-new-pkgs to allow installation of new dependencies when needed.
  • Keep a backup of the list of installed packages with dpkg --get-selections > paquetes.txt to facilitate replication on another machine.

With this knowledge, apt-get becomes a powerful and reliable tool for managing software on any Debian-based distribution, allowing you to keep the system up-to-date, secure, and tailored to the user’s needs.

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .