Using apt-get || apt command.
This guide outlines the capabilities of apt-get or apt commands in Linux, explaining how to utilize them for tasks such as discovering new packages, installing and upgrading them, and maintaining system cleanliness.
If you've started using Ubuntu or any Ubuntu-based Linux distribution such as Linux Mint, elementary OS or Pop!_OS, etc..., you've probably come across the apt-get
or apt
command at some point.
What is apt
?
APT (Advanced Package Tool), or apt-get is a package management system used in Debian-based Linux distributions like Ubuntu. It is a collection of command-line tools that facilitate the installation, removal, and management of software packages on the system. APT automates the process of fetching software packages from repositories, resolving dependencies, and configuring them, simplifying the software management process for users and administrators.
Using apt-get
or apt
Commands
Maintaining up-to-date packages is crucial because using outdated ones can expose your system to security vulnerabilities. Since apt-get manages core system components, it necessitates administrative (Super-User) privileges. Therefore, in Ubuntu or Ubuntu-based distributions, you'll typically need to prepend most commands with sudo
command.
Update the package database with apt-get
apt-get
essentially operates based on a database of accessible packages. Without it, the system remains unaware of the availability of newer packages. It's essential to execute an update before proceeding with an upgrade
or dist-upgrade
.
To perform an update, simply run this command:
$ sudo apt-get update
OUTPUT
[sudo] password for linux:
Hit:1 http://ports.ubuntu.com/ubuntu-ports jammy InRelease
Get:2 http://ports.ubuntu.com/ubuntu-ports jammy-updates InRelease [119 kB]
Hit:3 https://packages.cisofy.com/community/lynis/deb stable InRelease
Hit:4 http://ports.ubuntu.com/ubuntu-ports jammy-backports InRelease
Get:5 http://ports.ubuntu.com/ubuntu-ports jammy-security InRelease [110 kB]
Get:6 https://deb.nodesource.com/node_16.x focal InRelease [4,583 B]
Hit:7 https://packagecloud.io/crowdsec/crowdsec/ubuntu focal InRelease
Fetched 234 kB in 4s (64.1 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
13 packages can be upgraded. Run 'apt list --upgradable' to see them.
Observing the terminal screen above, you can identify two distinct line types: Hit and Get with the potential for an Ign to appear as well.
Let us explain!
- Hit: This indicates that the package lists have been successfully retrieved from the repositories, and the package index files are up to date. Basically, it indicates that the repository was reached, and there have been no modifications since the previous update.
- Get: This means that the system is downloading package lists from the repositories. It indicates that the system is actively retrieving updated package information from the specified repositories.
- Ign: Short for "Ignore" this message indicates that the system has ignored a repository or package source because it is disabled or not trusted. This usually happens when a repository has been disabled or marked as "Ign" in the
sources.list
file, or if the repository's GPG key is not recognized as trusted.
Upgrading Package Database
After updating your package database, you can proceed to upgrade the packages with available updates on your machine. This process ensures that both applications and the Ubuntu core system are updated to the latest available versions.
To perform an upgrade, execute the following command:
sudo apt-get upgrade -y
To upgrade a specific program individually instead of upgrading the entire system, execute this command:
sudo apt-get upgrade [package_name]
You can also perform an upgrade with the following command:
sudo apt-get dist-upgrade
Similar to the apt-get upgrade
command, the apt-get dist-upgrade
also upgrades the packages. In addition to this, it also handles changing dependencies with the latest versions of the package. It intelligently resolves the conflict among package dependencies and tries to upgrade the most significant packages at the expense of less significant ones, if required.
Unlike apt-get upgrade
, the apt-get dist-upgrade
command is proactive. It automatically installs new packages or removes existing ones as needed to complete the upgrade process.
This could potentially result in the removal of packages that you may wish to keep. It's important to use this command cautiously, especially on production systems, as it can lead to significant changes in the system's configuration.
Combining Commands
You can actually combine commands into a single line, allowing one command to execute multiple functions sequentially.
To perform an update followed by an upgrade, run the following command:
$ sudo apt-get update && sudo apt-get upgrade -y
Essentially, this command instructs the machine to execute an update, followed by an upgrade using the &&
operator. The -y
(yes) flag indicates automatic confirmation to any prompts that may appear in the command line.
Search for Packages with apt-cache
Unlike apt-get
, which handles package upgrades, installations, and removals, apt-cache
is a tool specifically designed to query the package database for package information. Unlike apt-get
, apt-cache
does not necessitate administrative privileges, so you can execute commands without using sudo
.
apt-cache search
The apt-cache search
command in Linux is used to search the package cache for packages matching a specified keyword or pattern. It returns a list of packages whose names or descriptions match the provided search term. This command is helpful for discovering available packages related to specific functionalities or software.
For example, if you're looking for packages related to "python", you can search for packages whose names contain "python".
$ apt-cache search [search_term]
$ apt-cache search python
OUPUT
alembic - lightweight database migration tool for SQLAlchemy
autodep8 - DEP-8 test control file generator
brltty - Access software for a blind person using a braille display
dbus - simple interprocess messaging system (daemon and utilities)
debian-goodies - Small toolbox-style utilities for Debian systems
devscripts - scripts to make the life of a Debian Package maintainer easier
docutils-common - text processing system for reStructuredText - common data
docutils-doc - text processing system for reStructuredText - documentation
exuberant-ctags - build tag file indexes of source code definitions
frr-pythontools - FRRouting suite - Python tools
jeepney-doc - pure Python D-Bus interface — documentation
libapache2-mod-wsgi-py3 - Python 3 WSGI adapter module for Apache
libboost-python1.74.0 - Boost.Python Library
libcrack2-dev - pro-active password checker library - development files
libdbus-1-3 - simple interprocess messaging system (library)
libgtksourceview-4-common - common files for the GTK+ syntax highlighting widget
libhogweed6 - low level cryptographic library (public-key cryptos)
libhpmud-dev - HP Multi-Point Transport Driver (hpmud) development libraries
libhpmud0 - HP Multi-Point Transport Driver (hpmud) run-time libraries
libinih-dev - simple .INI file parser (development files)
libinih1 - simple .INI file parser
libinireader0 - simple .INI file parser for C++
libjs-sphinxdoc - JavaScript support for Sphinx documentation
liblttng-ust-python-agent1 - LTTng 2.0 Userspace Tracer (Python agent native library)
libnettle8 - low level cryptographic library (symmetric and one-way cryptos)
...
..
.
This feature enables you to search for terms rather than precise package names, eliminating the necessity to know the exact package name.
However, if you want to be more specific, you can use regular expressions.
For instance, to search for packages that start with "python3", you can use:
$ apt-cache search '^python3'
OUPUT
python3 - interactive high-level object-oriented language (default python3 version)
python3-alabaster - Configurable sidebar-enabled Sphinx theme (Python 3)
python3-alembic - lightweight database migration tool for SQLAlchemy - Python module
python3-all - package depending on all supported Python 3 runtime versions
python3-all-dbg - package depending on all supported Python 3 debugging packages
python3-all-dev - package depending on all supported Python 3 development packages
python3-amqp - Low-level AMQP client (Python3 version)
python3-aniso8601 - python3 library for parsing dates and time
python3-anyjson - Common interface for the best available JSON implementation (Python3 version)
python3-aodh - OpenStack Telemetry (Ceilometer) Alarming - Python 3 libraries
python3-aodhclient - OpenStack Alarming as a Service - Python 3.x client
python3-apparmor - AppArmor Python3 utility library
python3-appdirs - determining appropriate platform-specific directories (Python 3)
python3-apport - Python 3 library for Apport crash report handling
python3-apt - Python 3 interface to libapt-pkg
...
..
.
This command will display packages whose names start with "python3". Regular expressions offer powerful pattern matching capabilities, allowing you to refine your search criteria as needed.
apt-cache depends
The apt-cache depends
command displays the dependencies of a specific package. When executed with the name of a package as an argument, it lists the dependencies required by that package. This command helps users understand what other packages are needed for the selected package to function properly.
$ apt-cache depends [package_name]
$ apt-cache depends curl
OUTPUT
curl
Depends: libc6
Depends: libcurl4
Depends: zlib1g
This command displays a list of dependencies required by the curl
package.
apt-cache show
The apt-cache show
command displays detailed information about a specific package available in the package cache. This command provides various details about the package, including its version number, dependencies, description, maintainer, and other relevant information as well as MD5sum, SHA1, SHA256 checksum. To view information about a package, run the following command:
$ apt-cache show [package_name]
$ apt-cache show curl
OUTPUT
Package: curl
Architecture: amd64
Version: 7.81.0-1ubuntu1.15
Multi-Arch: foreign
Priority: optional
Section: web
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Alessandro Ghedini <ghedo@debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 443
Depends: libc6 (>= 2.34), libcurl4 (= 7.81.0-1ubuntu1.15), zlib1g (>= 1:1.1.4)
Filename: pool/main/c/curl/curl_7.81.0-1ubuntu1.15_amd64.deb
Size: 194102
MD5sum: 4cf3f3e70baae1f49a2538a779650ae9
SHA1: dda8b8aab6eff5abf963b84b4b64ecf8ee4f5fda
SHA256: 881c85170fcdf54d2d862c1a96e974226377021dfb8efc43f807630756fbe4bd
SHA512: fffecc0b7f9fd86f4eabcd3f0afbca3f69b20016044ac14415ab81787a59cc5233b18ce6e32ef43f2e032f7c91241c13ece5ceafb27e99bb2458dbcc4391bf83
Homepage: https://curl.haxx.se
Description-en: command line tool for transferring data with URL syntax
curl is a command line tool for transferring data with URL syntax, supporting
DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3,
POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP.
...
..
.
How to Install new Package(s) with apt-get
To install new packages using apt-get
, follow these steps:
$ sudo apt-get install mailutils
Of course, you are not limited to installing just one package at a time.
You can install several packages at a time just by providing their names:
$ sudo apt-get install <package_1> <package_2> <package_3>
$ sudo apt-get install curl get ufw
How to Remove installed Package(s) with apt-get
All you need to do is to use the command in this manner:
$ sudo apt-get remove <package_name>
$ sudo apt-get remove curl
OUTPUT
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
curl pollinate ubuntu-server ubuntu-server-minimal
0 upgraded, 0 newly installed, 4 to remove and 0 not upgraded.
After this operation, 656 kB disk space will be freed.
Do you want to continue? [Y/n]
Of course, you are not limited to remove just one package at a time.
You can remove several packages at a time just by providing their names:
$ sudo apt-get remove <package_1> <package_2> <package_3>
$ sudo apt-get remove curl get ufw
Another way of uninstalling packages is to use purge.
$ sudo apt-get purge <package_name>
$ sudo apt-get purge curl
This command will not only remove the package itself but also delete any configuration files associated with it. It's useful when you want to completely eliminate all traces of a package from your system.
What is the Difference Between apt-get remove
and apt-get purge
?
apt-get remove
just removes the binaries of a package. It does not touch any of the configuration files.apt-get purge
removes everything related to a package, including any trace of the configuration files, and starts afresh.
How to clean your system with apt-get
Oh yeeeeah!
The apt-get clean
command will free up disk space by removing cached package files that were previously downloaded and installed by APT (Advanced Package Tool). These cached files are stored in the "/var/cache/apt/archives" directory.
Use the command below to clear apt-cache (locally saved retrieved package files):
$ sudo apt-get clean
Over time, this cache can accumulate a significant amount of disk space. The "apt-get clean" command helps to reclaim this space by deleting all package files from the cache, except for the package files currently installed on the system.
autoclean
Another way is to use autoclean
.
The apt-get autoclean
command is similar to apt-get clean
, but with a slight difference. While apt-get clean
removes all cached package files from the "/var/cache/apt/archives" directory, apt-get autoclean
removes only package files that can no longer be downloaded, meaning they are obsolete or unnecessary.
sudo apt-get autoclean
The apt-get autoclean
command removes these obsolete package files from the cache, helping to keep the cache directory clean and free up disk space.
autoremove
The apt-get autoremove
command is used to remove packages that were automatically installed to satisfy dependencies for other packages but are no longer needed. These packages are often referred to as "orphaned" packages.
sudo apt-get autoremove
This will scan your system for orphaned packages and prompt you to confirm their removal. Once confirmed, it will proceed to remove the orphaned packages, helping to clean up your system.
For additional information, please refer to the manual pages by using the man
command. These manual pages contain detailed information about how to use a specific command, its options, syntax, and often include examples and explanations.
$ man apt-get
$ man apt-cache
We hope this was of great use!
Cheers guys!!!