Apt Linux Command with Examples

If you are using Linux as your operating system, then you might be familiar with the apt command. APT stands for "Advanced Package Tool" and it is a package manager used in Linux distributions like Ubuntu, Debian, and others. APT is a command-line tool that allows users to search, install, remove, and manage software packages on their Linux system.

Basic Package Management Operations

Updating Package Lists

Before installing any package on your Linux system, it's important to update package lists. APT uses package lists to know which packages are available for installation.

sudo apt update

This command downloads the latest package lists from repositories. After executing this command, you will be able to install the latest packages available for your Linux distribution.

Installing Packages

To install a package, use the apt install command followed by the name of the package you want to install.

sudo apt install apache2

This command will download and install the Apache web server on your Linux system.

Removing Packages

To remove a package from your Linux system, use the apt remove command.

sudo apt remove apache2

This command removes the Apache web server but keeps its configuration files. To completely remove a package including configuration files, use purge:

sudo apt purge apache2

Upgrading Packages

To upgrade installed packages on your Linux system, use the apt upgrade command.

sudo apt upgrade

This command downloads and installs the latest versions of packages that are already installed on your Linux system.

Package Information and Search

Listing Installed Packages

To list all packages that are installed on your Linux system:

apt list --installed

This displays a list of all installed packages along with their version numbers.

Searching for Packages

To search for a package, use the apt search command:

apt search apache2

This searches the repository for the Apache web server and displays matching results.

Checking Package Information

To get detailed information about a package, use the apt show command:

apt show apache2

This displays detailed information about the package, including its version, size, dependencies, and description.

System Maintenance

Cleaning Up

When you install or remove packages, APT keeps downloaded package files in cache. To clean up the cache:

sudo apt clean

This removes all downloaded package files from the cache, freeing up disk space.

Autoremove Packages

Sometimes, when you remove a package, it may leave behind dependencies that are no longer needed:

sudo apt autoremove

This removes unnecessary dependencies that are no longer required by any other package.

Advanced Package Operations

Installing Specific Package Versions

To install a specific version of a package:

sudo apt install apache2=2.4.41-4ubuntu3

Reinstalling Packages

If a package is corrupted, you can reinstall it:

sudo apt --reinstall install apache2

Installing from .deb Files

To install a package from a downloaded .deb file:

sudo dpkg -i package-name.deb
sudo apt install -f

The second command fixes any dependency issues that may arise.

Repository Management

Adding Repositories

To add a new repository (PPA), use:

sudo add-apt-repository ppa:ondrej/php
sudo apt update

Always update package lists after adding a new repository.

Common APT Commands Reference

Command Description
sudo apt update Update package lists
sudo apt upgrade Upgrade installed packages
sudo apt install <package> Install a package
sudo apt remove <package> Remove a package
sudo apt purge <package> Remove package and config files
apt search <keyword> Search for packages
apt show <package> Show package information
apt list --installed List installed packages
sudo apt autoremove Remove unused dependencies
sudo apt clean Clear package cache

Conclusion

APT is a powerful package manager that simplifies software management on Debian-based Linux distributions. With these essential commands, you can efficiently install, update, remove, and maintain software packages on your Linux system. Regular use of apt update and apt upgrade ensures your system stays current and secure.

Updated on: 2026-03-17T09:01:38+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements