Managing packages in Ubuntu and Debian with apt-get and apt-cache
30 Mar 2025, 20:20:06
Debian-based operating systems (Ubuntu, Linux Mint, etc.) use a package management tool called APT (Advanced Package Tool). The main commands used to work with packages are apt-get (install, update and uninstall packages) and apt-cache (search and retrieve package information).Package management allows you to quickly and safely install, upgrade or uninstall software, automatically resolving dependency issues and keeping your system stable. In this tutorial, we'll take a closer look at working with packages using the command line.
All commands are run as root or using sudo.
Update package and system information
Update the list of available packages:apt-get updateThis command updates information about available packages from the repositories.Upgrade installed packages to the latest available versions:
apt-get upgradeThe command updates already installed packages, but does not remove obsolete ones.Complete package update (with dependency conflicts resolved):
apt-get dist-upgradeThis command can install or remove packages to resolve dependencies and provide a complete system update.Search and retrieve package information
Search for a package by keyword:apt-cache search keywordThis command outputs a list of packages matching the keyword.Getting detailed information about the package:
apt-cache show package_nameThe command displays the package description, version, dependencies, and other information.Checks the status of the package installation:
dpkg -l | grep package_nameThis command checks to see if the package is installed on the system.Or use:
apt list --installed | grep package_nameA more modern way to check if a package is available.Installing and uninstalling packages
Package Installation:apt-get install package_nameThis command installs the package and automatically loads its dependencies.Installing the local package (.deb):
dpkg -i имя_файла.deb
apt-get install -fThe first command installs the local package, the second fixes dependency issues.Deleting a package (with settings preserved):
apt-get remove package_nameUninstalls the package, but retains the configuration files in case of reinstallation.Complete package removal (with deletion of configurations):
apt-get purge package_nameThis command removes the package and its configuration files.Uninstall automatically installed unnecessary packages:
apt-get autoremoveRemoves unused dependencies, freeing up space.Cleaning and managing the package cache
Clearing all downloaded package files:apt-get cleanRemoves all downloaded package files from the cache.Remove obsolete packages from the cache:
apt-get autocleanClears the cache of outdated package versions.Working with repositories
Package repositories are defined in the file /etc/apt/sources.list. Changes to this file should be made with administrator privileges and caution.Adding third-party repositories (PPA):
add-apt-repository ppa:name/ppa
apt-get updateThe first command adds a repository, the second updates the list of available packages.Package version management
You can install a specific version of a package by adding =version after the package name:apt-get install package_name=versionFix package version (prevents automatic updates):
apt-mark hold package_nameRemoves the version fixation:
apt-mark unhold package_nameUseful tips and advice
- Always check the changes that will be made when installing or uninstalling packages;
- Use the apt command (the more modern equivalent of apt-get) for a more compact syntax;
- Clean the system regularly:
apt-get autoremove - Clear the package cache after major updates:
apt-get clean - Avoid confirmations when installing:
apt-get install -y package_name - Review the history of installed packages:
less /var/log/dpkg.log