Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Use the Apt-Get Command in Linux?
The Advanced Packaging Tool (APT) is a powerful command-line tool used in Debian-based systems like Ubuntu, Linux Mint, and others. The apt-get command is one of the most common ways to interact with APT. It's used to handle packages, allowing you to install, upgrade, and remove software on your Linux system.
In this guide, we'll walk you through the essential apt-get commands with practical examples and their outputs.
Updating Package Lists
The first command you should know is apt-get update. This command retrieves information about the newest versions of packages and their dependencies. It doesn't install or upgrade any packages, but updates the local package database for upgrades and new installations.
sudo apt-get update
Hit:1 http://archive.ubuntu.com/ubuntu bionic InRelease Get:2 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB] Get:3 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB] Reading package lists... Done
Upgrading Packages
Once you've updated your package lists, you can upgrade your installed packages with apt-get upgrade. This command installs the newest versions of all packages currently installed on the system.
sudo apt-get upgrade
Reading package lists... Done Building dependency tree Reading state information... Done Calculating upgrade... Done The following packages will be upgraded: libssl1.1 openssl 2 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. Need to get 1,374 kB of archives. After this operation, 1,024 B of additional disk space will be used. Do you want to continue? [Y/n]
Installing Packages
To install a new package, use the apt-get install command followed by the package name. APT automatically resolves and installs any required dependencies.
sudo apt-get install firefox
Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: firefox-locale-en Suggested packages: fonts-lyx The following NEW packages will be installed: firefox firefox-locale-en 0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded. Need to get 49.4 MB of archives. After this operation, 182 MB of additional disk space will be used. Do you want to continue? [Y/n]
Removing and Purging Packages
There are two ways to remove packages
Remove (Keep Configuration Files)
sudo apt-get remove firefox
Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be removed: firefox firefox-locale-en 0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded. After this operation, 182 MB disk space will be freed. Do you want to continue? [Y/n]
Purge (Remove Everything Including Configuration)
sudo apt-get purge firefox
Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be removed: firefox* firefox-locale-en* 0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded. After this operation, 182 MB disk space will be freed. Do you want to continue? [Y/n]
System Cleanup Commands
Autoremove Unused Dependencies
When packages are removed, their dependencies might become orphaned. The autoremove command removes these unnecessary packages.
sudo apt-get autoremove
Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: libfreetype6 libjpeg-turbo8 libjpeg8 libjbig0 libtiff5 0 upgraded, 0 newly installed, 5 to remove and 0 not upgraded. After this operation, 1,525 kB disk space will be freed. Do you want to continue? [Y/n]
Clean Package Cache
The clean command removes downloaded .deb files from /var/cache/apt/archives/ to free up disk space.
sudo apt-get clean
Advanced Package Operations
Install Specific Package Version
sudo apt-get install apache2=2.4.29-1ubuntu4.14
Download Package Without Installing
apt-get download apache2
Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 apache2 amd64 2.4.29-1ubuntu4.14 [95.1 kB] Fetched 95.1 kB in 1s (67.8 kB/s)
Package Information Commands
Check Installed Packages
dpkg -l | grep apache2
ii apache2 2.4.29-1ubuntu4.14 amd64 Apache HTTP Server ii apache2-bin 2.4.29-1ubuntu4.14 amd64 Apache HTTP Server (modules and other binary files) ii apache2-data 2.4.29-1ubuntu4.14 all Apache HTTP Server (common files)
List All Installed Packages
dpkg --get-selections
adduser install apache2 install apache2-bin install apache2-data install apt install ...
Common Usage Patterns
| Task | Command | Purpose |
|---|---|---|
| System Update | sudo apt-get update && sudo apt-get upgrade |
Update package lists and upgrade all packages |
| Install with Dependencies | sudo apt-get install -f package |
Fix broken dependencies during installation |
| Simulate Installation | sudo apt-get install -s package |
Show what would be installed without actually installing |
| Complete Cleanup | sudo apt-get autoremove && sudo apt-get clean |
Remove orphaned packages and clean cache |
Conclusion
The apt-get command is an essential tool for managing packages on Debian-based Linux systems. It provides comprehensive package management capabilities from installation and updates to cleanup and maintenance. Always use sudo for system-level operations and be cautious when removing packages to avoid breaking system dependencies.
