apt-get command in Linux with Examples
The command-line tool `apt-get` is the most popular package management tool used in our Debian-based Linux operating system. This article provides an overview of `apt-get` and its basic syntax. It will include the most commonly used commands, their syntax, description, and examples. It also gives an explanation of the contrast to other Linux distributions such as CentOS and Fedora, which use different package managers, and Debian-based Linux distributions use APT (Advanced Packaging Tool).
What is apt-get?
apt-get is a command-line tool that helps in handling packages in Linux. Its main task is to retrieve the information and packages from the authenticated sources for installation, upgrade, and removal of packages along with their dependencies. Here APT stands for Advanced Packaging Tool. It is a Debian-based Linux operating system, for example, Ubuntu and Debian itself. It is an APT (Advance Packaging Tool) system. Which manages packages for Debian-based Linux distributions.
What is Debian-based Linux distributions?
A family of operating systems that are built on the Debian operating system. They use the same APT (Advance Packaging Tool) for package management system. Some of the most popular Debian-based Linux distributions have different features but come under the same underlying Debian system and package management tools are Linux Mint, Ubuntu and kali Linux.
One must know when we talk about Red Hat-based distributions like CentOS and Fedora, they are Linux distributions but not Debian-based Linux distributions. Red Hat-based Linux distributions are often preferred for enterprise environments and servers, focusing on stability and security. Whereas Debian-based Linux distributions moreover focus on long-term support and stability.
Note: apt-get (package manager) is used in Debian-based Linux distributions but in Linux distributions like CentOS and Fedora uses different package manager.
APT-GET Command in Linux
sudo apt-get [options] [command] [package(s)]
Breakdown of each component:
- ` sudo `: We will be using ‘sudo’ before every ‘apt-get’ command because ‘sudo’ in Linux system is used to execute command with the administrative privileges (root privileges). In the case of ‘apt-get’ which is used to manage software packages on Debian-based Linux systems, we need administrative privileges to install, remove, update and make other changes.
- `apt-get`: The command itself.
- `[options]`: This is an optional parameter in this we can use `-y` or `-s` for modifying the behavior of `apt-get` command. `-y` is for automatically answering “yes” and `-s` is for simulating a command without making any changes.
- `[packages]`: In this we specify the package that we want to remove, upgrade or install. If we want to mention multiple packages at same time, specify them by separated spaces between them.
Most Used Commands[APT-GET]
You need to provide one of the commands below, if the -h option is not used.
|update||Used in Linux-based operating systems to update the package lists for available software packages from the configured repositories.||sudo apt-get update||sudo apt-get update|
|upgrade||This command is used to install the latest versions of the packages currently installed on the user’s system from the sources enumerated in /etc/apt/sources.list. The installed packages which have new packages available are retrieved and installed. You need to perform an update before the upgrade so that apt-get knows that new versions of packages are available.||sudo apt-get upgrade||sudo apt-get upgrade|
|dselect-upgrade||The command used in Linux-based operating systems to upgrade the system by installing and removing packages according to the selections made in the `dselect`.||sudo apt-get dselect-upgrade||sudo apt-get dselect-upgrade|
|dist-upgrade||This command performs the function of upgrade, and also handles changing dependencies with new versions of packages. If necessary, the apt-get command will try to upgrade important packages at the expense of less important ones. It may also remove some packages in this process.||sudo apt-get dist-upgrade
||sudo apt-get dist-upgrade
|install||This command is used to install or upgrade packages. It is followed by one or more package names the user wishes to install. All the dependencies of the desired packages will also be retrieved and installed. The user can also select the desired version by following the package name with an ‘equal’ and the desired version number. Also, the user can select a specific distribution by following the package name with a forward slash and the version or the archive name (e.g., ‘stable’, ‘testing’, or ‘unstable’). Both of these version selection methods have the potential to downgrade the packages, so they must be used with care.||sudo apt-get install [package_name]
||sudo apt-get install vim
|reinstall||If we already have a package but we want to reset it to its default state, we can re-install that package.||sudo apt-get install –reinstall [package_name]
||sudo apt-get install –reinstall firefox
|remove||This is similar to install, with the difference being that it removes the packages instead of installing. It does not remove any configuration files created by the package.||sudo apt-get remove [package_name]
||sudo apt-get remove vim
|purge||This command removes the packages, and also removes any configuration files related to the packages.||sudo apt-get purge [package_name]
||sudo apt-get purge vim
|check||This command is used to update the package cache and check for broken dependencies.||sudo apt-get check
||sudo apt-get check
|download||This command is used to download the given binary package in the current directory.||sudo apt-get download [package_name]
||sudo apt-get download firefox
|clean||This command is used to keep our system clean and tidy. It removes all the cached package files that were downloaded due to the downloading of recent packages using `apt-get`.||sudo apt-get clean
||sudo apt-get clean
|autoremove||Sometimes the packages which are automatically installed to satisfy the dependencies of other packages, are no longer needed then the autoremove command is used to remove this kind of packages.||sudo apt-get autoremove
||sudo apt-get autoremove
|source||If we want to recompile or modify the source code of a package. Source code will be downloaded in current directory in tarball format.||sudo apt-get source [package_name]
||sudo apt-get source firefox
|show||If we want to know more about a package, verify its installation status, version, description, other metadata and dependencies.||sudo apt-get show [package_name]
||sudo apt-get show firefox
|list||It also gives details (version, architecture and repository source) about package but only if package is available or installed in our system.||
sudo apt-get list
sudo apt-get list [package_name]
|sudo apt-get list firefox
Options Available in `apt-get`:
|–no-install-recommends||It will avoid downloading any kind of recommended packages that are not strictly required by the package (installed/upgraded/reinstalled/installed). One must be caution while using this because there may be some package needed for your requirement.||sudo apt-get –no-install-recommends [Command] [package_name]
||sudo apt-get –no-install-recommends install firefox
|–install-suggests||It works opposite of `–no-install-recommends`. It downloads all the suggested packages that are not required much.||sudo apt-get –install-suggests [Command] [package_name]
||sudo apt-get –install-suggests install firefox
|-d or –download-only||In this package is only downloaded in the location (`/var/cache/apt/archives/`) by default, it is not installed, we can further in future install it.||sudo apt-get -d [command] [package_name]
||sudo apt-get -d install vlc
|-f or –fix-broken||By passing this option, the user specifies that apt-get should attempt to correct the system with broken dependencies in place.||sudo apt-get -f install
||sudo apt-get -f install
|-m or –ignore-missing or –fix-missing||By passing this option, the user specifies that apt-get should ignore the missing packages (packages that cannot be retrieved or fail the integrity check) and handle the result.||sudo apt-get -m install [package_name]
||sudo apt-get -m install apache2
|–no-download||In this `apt-get` is instructed to download packages already available in cache, not to download any package from configured package sources.||sudo apt-get –no-download install [package_name]
||sudo apt-get –no-download install vim
|-q or –quiet||This is used to silence the output errors or displaying warning messages. It is useful while writing scripts.||sudo apt-get -q install [package_name]
||sudo apt-get -q install apache2
|-s or –simulate or –just-print or –dry-run or –recon or –no-act||In this package in not actually installed, it just shows what operations it will perform while installing that package.||sudo apt-get -s install [package_name]
||sudo apt-get -s install firefox
|-y or –yes or –assume-yes||It automatically promotes yes while installing a package if there is any request or conformation that has to be taken from user, but one should be careful while using this.||sudo apt-get -y [command] [package_name]
||sudo apt-get -y install vim
|–assume-no||It automatically passes no to any question that will arise during the installation of any package.||sudo apt-get –assume-no install [package_name]
||sudo apt-get –assume-no install apache2
|–no-show-upgraded||Used to suppress the list of upgraded packages that is normally displayed at the end of the upgrade process. (Only works on upgrade command don’t work on install, remove or purge etc).||sudo apt-get –no-show-upgraded upgrade
||sudo apt-get –no-show-upgraded upgrade
|-V or –verbose-versions||Used to display the version number of all the available packages including those that are not even installed. (Only work on update command, not on any other like upgrade, install, remove or purge etc)||sudo apt-get -V update
||sudo apt-get -V update
|–show-progress||Used to display a bar for the progress of the command. (Only used for update command and has no effect on commands like upgrade, install, remove or purge etc)||sudo apt-get –show-progress update
||sudo apt-get –show-progress update
|-b or –compile or –build||build-dep is used to install the build dependencies of the specified packages and `-b` is used to build a package from source.||
sudo apt-get build-dep [package_name]
sudo apt-get build-dep firefox
|–no-upgrade||It is to install a package without upgrading to a new version. (Only works with install command)||sudo apt-get install [package_name] –no-upgrade
||sudo apt-get install vim –no-upgrade
|–only-upgrade||It upgrades the package to the newer version while stopping the installation of other packages from being upgraded during the update process.||sudo apt-get install –only-upgrade [package_name]
||sudo apt-get install –only-upgrade firefox
|–reinstall||It reinstalls the package, even if the package is installed.||sudo apt-get install –reinstall [package_name]
||sudo apt-get install –reinstall firefox
|-h or –help||It is used to display a help manual.||apt-get –help
Note: apt-get command will return 0 for successful executions, and decimal 100 in case of errors.
Basic Queries Asked About `apt-get`:
1) Difference between apt-get remove and apt-get purge?
Both of `apt-get remove` and `apt-get purge` are used for similar purposes, that is to remove or uninstall the packages from our system. But still, they have a different use case.
`apt-get remove` this command only removes packages from system but leave behind their configuration files. (To see syntax, refer above context)
`apt-get purge` this command is also used to remove packages but in this case configuration files are also removed. (To see syntax, refer above context)
2)Difference between apt-get update and apt-get upgrade?
`apt-get update` This command is used to update packages rather than installing them, basically it is used to update local package index with the latest information from the configured source. Remember it does not install packages.
`apt-get upgrade` This command is used to upgrade the packages to the newest version available. Basically, it is used to upgrade and download the packages that are installed in our system.
Syntax for `apt-get update`:
sudo apt-get update
Syntax for `apt-get upgrade`:
In this we going to upgrade firefox, if it is already upgrade it will show firefox is already upgraded.
sudo apt-get upgrade
3)Not able to run `apt-get` in Red Hat Linux?
`apt-get` it is command used for package management but only in Debian-based Linux distributions like mint, Debian and ubuntu, whereas Red Hat Linux is a Linux distribution but does not come under Debian-based Linux distributions. Red Hat Enterprise Linux (RHEL), Fedora and CentOS all will not be able to use `apt-get` instead we can use `dnf` or `yum` package management tools. Just for basic example: we can use `sudo yum update` or `sudo dnf update`.
4)How to install packages using `apt-get` command?
In this we are going to install firefox package in our Debian-base Linux operating system (In this case we are executing this command in Ubuntu)
Syntax for `apt-get install`:
sudo apt-get install firefox
Please Login to comment...