This tutorial provides a concise explanation of the Apt package manager and presents a curated list of 15 useful Apt Linux command examples for efficient package management in Debian and its derivative distributions such as Ubuntu, Linux Mint, PoP!_OS, Elementary OS, Kubuntu, and more.
At the end of this guide, we will present you a handy APT Linux command cheat sheet, so you can keep it nearby and use it as quick reference to learn how to use the APT package manager efficiently.
Table of Contents
What is APT in Linux?
APT, or Advanced Package Tool, is a free and open-source software package management system used to install, update, remove, and otherwise manage software packages on Debian and its derivative operating systems, such as Ubuntu and Linux Mint.
APT provides the same functionality as the specialized APT tools, like apt-get and apt-cache, but enables options more suitable for interactive use by default. Meaning - Apt can do everything that apt-get and apt-cache can do and Apt has some default options that are better suited for interactive use, such as the ability to show progress bars and the ability to ask for confirmation before making changes.
APT is the default package manager for Debian-based systems. APT can be used to manage a wide variety of software packages with ease.
Some of the key features of APT include:
- Ease of use: APT is a relatively easy-to-use tool, even for beginners. The basic commands are straightforward and the syntax is clear.
- Powerful: APT is a powerful tool that can be used to manage a wide range of software packages. It can be used to install, update, remove, and search for packages without having to worry about dependencies or conflicts.
- Large number of packages: Apt connects to a centralized software repository that offers a vast array of packages. All the packages in the repository are regularly updated and maintained by the community, ensuring that you always have access to the latest versions of your favorite software.
APT Linux Command Examples
Heads Up: Some of the APT and its sub-commands requires root user or sudo privilege. All the commands that require elevated privilege are prefixed with sudo in this article.
1. Getting Help
To get help for using APT on a Debian-based Linux system, you can consult its help and manual page.
If you are not sure which command to use, you can always use the apt help or apt --help command to get a list of all available commands.
$ apt --help
This will display a brief summary of APT commands and their usage.
The "man" command displays the manual pages for various commands, including APT commands. To view the manual for "apt," use:
$ man apt
Similarly, you can use "man" with other APT commands like "apt-get," "apt-cache," etc.
2. Update APT Package Index
The 'apt update' is the first command you will run after installing a new operating system or whenever you want to install any new packages.
$ sudo apt update
The update command downloads the package metadata by connecting to the configured repositories. The repository information is configured either in /etc/apt/source.list file or under /etc/apt/sources.list.d directory.
Each line in the file points to a repository and it may vary with different distributions. For example, Linux Mint maintains its own repository and you can see it in the first line of the file. Other distributions like PoP!_OS also maintains their own repository. Since all these distributions are derived from Ubuntu you will see Ubuntu repositories present in the list.
When you run the 'apt update' command, it will connect to all these repositories and will update the metadata in the local cache and will show the list of packages that can be upgraded, packages that are no more essential, etc.
3. Search for Packages using APT Linux Command
Before installing any packages, you should make sure that the package is available in the configured repositories. You can use the 'apt search' command which accepts the package name as the input and does a regex search.
$ apt search <package-name></package-name>
Let’s search for a package named Kitty terminal emulator.
$ apt search kitty
If you look at the above output, it does a regex search for the word 'kitty' and returns a similar result.
Further, you can pipe the search output to the grep command to filter the exact match.
$ apt search kitty | grep -i -w kitty
Sample Output:
kitty/stable 0.26.5-5 amd64 kitty-doc/stable 0.26.5-5 all kitty-shell-integration/stable 0.26.5-5 all kitty-terminfo/stable 0.26.5-5 all
Heads Up: The 'apt search' command can be submitted without sudo or root privilege.
4. Find Detailed Information about a Package
To display the metadata of a package (package details) such as who is the author of the package, installation size, dependencies, source repository, etc by running any of the following commands. The following commands can be run without sudo or as the root user.
$ apt show kitty$ apt show -a kitty$ apt-cache show kitty
5. Install Packages using APT
You can install one or more packages by running the 'apt install' command and passing the package names as the arguments. Before running the install command run the 'apt update' command to refresh the package index i.e. refresh software source lists.
$ sudo apt install kitty
Let’s see what happens when you run this command.
- First, it will check the repository and see if the package is available or not.
- Second, it will build the dependency tree.
- It will show you the list of additional packages that will be installed along with the kitty.
- It will also show you the required disk size.
Finally, it will prompt you for confirmation. Type 'Y' and press ENTER to proceed with the installation. You can also directly pass the -y flag to skip the confirmation step.
$ sudo apt install kitty -y
You can also install multiple packages at once. Just specify the name of the packages with space-separated like below.
$ sudo apt install kitty tmux kitty gparted -y
If you try to install the package which is already installed, then it will just show you that the package is already installed.
Every package has versions tagged. If you wish to install a package with a specific version, simply provide the version number along with the package name like below.
$ sudo apt install kitty=0.21.2
You can get the available package versions using the following command:
$ apt list --all-versions kitty Listing... Done kitty/oldstable 0.19.3-1 amd64
6. Check the List of Installed Packages using APT
There are a couple of ways you can check the list of packages that are installed.
1. To list all installed packages, run:
$ apt list --installed
This command will display a list of all the packages that are currently installed on your system. It will show the package names along with their versions and architecture information.
2. You can also refine the results by filtering packages with names that contain a specific string. For instance, you can run the 'apt list' command along with 'grep' to filter packages containing 'kitty' in their name like below. Please note that this command does not require elevated privilege.
$ apt list --installed | grep -i kitty
This approach will help you pinpoint packages related to 'kitty' and streamline your search process effectively.
3. The another way to find the installed package is by using the apt-cache command.
$ apt-cache policy kitty
4. Every apt operation is recorded in the log file. You can check the log file to see what operations are performed for the given package.
$ tail -n 5 /var/log/apt/history.log
Sample Output:
Start-Date: 2023-08-03 17:46:14 Commandline: apt install kitty Requested-By: ostechnix (1000) Install: libb2-1:amd64 (0.98.1-1.1, automatic), kitty-doc:amd64 (0.26.5-5, automatic), librsync2:amd64 (2.3.2-1+b1, automatic), kitty:amd64 (0.26.5-5), libjs-jquery:amd64 (3.6.1+dfsg+~3.5.14-1, automatic), libjs-sphinxdoc:amd64 (5.3.0-4, automatic), libjs-underscore:amd64 (1.13.4~dfsg+~1.11.4-3, automatic), kitty-terminfo:amd64 (0.26.5-5, automatic), kitty-shell-integration:amd64 (0.26.5-5, automatic) End-Date: 2023-08-03 17:46:17
Related Read: How To List Installed Packages In Linux
7. Reinstall a Package using APT
There may be cases where your package is broken or not functioning as expected. In this case, you can try reinstalling the package using the --reinstall flag.
$ sudo apt install kitty --reinstall
This command will just reinstall the given package but not the dependent packages. If you wish to go with a clean install run the following command.
$ sudo apt purge kitty && sudo apt autoremove && sudo apt install kitty -y
8. Get a List of Packages that can be Updated using APT
To keep the system safe and error-free, you always need to update your software and apply security patches. Unlike the Windows operating system, Linux distributions will not force you to install packages but doing it is essential.
When you run the 'apt update' command, you should see the number of packages that can be upgraded.
You can run the 'apt list --upgradable' command to get the list of packages that are candidates for update.
$ apt list --upgradable
Sample Output:
Listing... Done base-files/stable 12.4+deb12u1 amd64 [upgradable from: 12.4] cups-client/stable 2.4.2-3+deb12u1 amd64 [upgradable from: 2.4.2-3] cups-common/stable 2.4.2-3+deb12u1 all [upgradable from: 2.4.2-3] cups-core-drivers/stable 2.4.2-3+deb12u1 amd64 [upgradable from: 2.4.2-3] cups-daemon/stable 2.4.2-3+deb12u1 amd64 [upgradable from: 2.4.2-3] cups-ipp-utils/stable 2.4.2-3+deb12u1 amd64 [upgradable from: 2.4.2-3] cups-ppdc/stable 2.4.2-3+deb12u1 amd64 [upgradable from: 2.4.2-3] cups-server-common/stable 2.4.2-3+deb12u1 all [upgradable from: 2.4.2-3] cups/stable 2.4.2-3+deb12u1 amd64 [upgradable from: 2.4.2-3] dbus-bin/stable 1.14.8-2~deb12u1 amd64 [upgradable from: 1.14.6-1] dbus-daemon/stable 1.14.8-2~deb12u1 amd64 [upgradable from: 1.14.6-1] [..]
You can also check if updates are available for a single package by either piping the above output to the grep command or running the 'apt-cache policy' command.
Here I am looking for a timeshift utility update. The current version is 21.09.1-1 and the available version is 22.11.2+vera.
$ apt-cache policy timeshift
Sample Output:
timeshift: Installed: (none) Candidate: 22.11.2-1 Version table: 22.11.2-1 500 500 https://deb.debian.org/debian bookworm/main amd64 Packages
9. Update a Package using APT
To update a single package to the latest version, run the 'apt install
$ sudo apt install timeshift
10. Update all System Packages using APT
To update all the packages in the system, you can run the 'apt upgrade' or 'apt full-upgrade' or 'apt dist-upgrade' command.
$ sudo apt upgrade
$ sudo apt full-upgrade
$ sudo apt dist-upgrade
You will see the number of packages to be upgraded when you run any of the above commands and the size required in disk for the upgrade.
10.1. apt upgrade vs apt full-upgrade vs apt dist-upgrade
Although they all deal with package upgrades, there are some key differences in their behavior:
1. apt upgrade:
- Purpose: The "apt upgrade" command is used to upgrade installed packages to their latest versions. However, it only upgrades packages if there are no changes in their dependencies that require new packages to be installed or existing packages to be removed.
- Caution: This command is cautious and avoids making any major changes to the system that could cause disruptions or require additional package handling.
2. apt full-upgrade:
- Purpose: The "apt full-upgrade" command is essentially an alias for "apt-get dist-upgrade." It performs the same function as "apt upgrade," but it is more aggressive in handling package changes. If necessary to upgrade the system as a whole, "apt full-upgrade" will remove currently installed packages and install new ones, effectively resolving more complex dependency issues.
- Note: Despite the term "full," it does not imply a complete system upgrade to a new release, but rather a comprehensive package upgrade.
3. apt dist-upgrade:
- Purpose: The "apt dist-upgrade" command is designed to handle major changes in package dependencies and upgrade to a new distribution release if available. It goes beyond the functionality of "apt upgrade" by intelligently handling changes in package dependencies and resolving conflicts, even if it means removing some packages or installing new ones.
- Use Case: This command is particularly useful when you want to upgrade to a new version of the distribution, such as moving from Ubuntu 20.04 to 22.04.
In Summary, the key difference between these three commands is the upgrade command will only update the existing packages in the system and will not install or remove any packages as part of the update. But the full-upgrade and dist-upgrade command will install or remove any packages that are necessary as part of the upgrade process.
11. Hold Package Updates
To prevent automatic updates on packages with specific versions, use the "apt-mark" command to mark them as manually installed. For example, to hold the kitty package:
To hold the package:
$ sudo apt-mark hold kitty
To unhold the package:
$ sudo apt-mark unhold kitty
By doing this, you retain control over package updates, ensuring your desired versions remain intact.
You can view the list of held packages using command:
$ apt-mark showhold
12. Remove a Package using APT
Removing a package involves removing the package, dependent packages, and package configurations.
You can use 'apt remove' to remove packages. Here an important point to note is only the given package will be removed but the dependencies and other config files that are part of the package will not be removed.
$ sudo apt remove kitty
Take a look at the below output which shows kitty as the only package to be removed and not the dependent packages.
13. Purge Packages
The apt remove command removes the package but leaves its configuration files intact. If you want to remove a package including any configuration files associated with that package, you can use 'apt purge' command as shown below.
$ sudo apt purge kitty
14. Remove Unnecessary Packages
When you install a package using APT, it may bring in additional packages (dependencies) that are necessary for its proper functioning. However, as you use your system and install or remove other packages, some of these dependencies may become unnecessary, as they were only required by packages you no longer have installed. These unused dependencies can take up valuable disk space on your system.
The "apt autoremove" command helps clean up your system by identifying and removing these automatically installed packages that are no longer needed.
$ sudo apt autoremove
Heads Up: The "apt autoremove" command will only remove packages that were installed as dependencies and are no longer required by other packages. It will not remove packages that were explicitly installed by the user.
15. Clean APT Cache
APT caches package files (commonly with the .deb extension) that are downloaded during package installations, updates, or upgrades. The cached files in APT are stored in "/var/cache/apt/archives/" directory. They are used to speed up package installations, provide offline installation capability, handle dependencies efficiently, enable rollbacks, maintain package integrity, and save disk space and bandwidth.
$ ls -l /var/cache/apt/archives/ total 9976 -rw-r--r-- 1 root root 399336 Jun 22 01:18 bind9-dnsutils_1%3a9.18.16-1~deb12u1_amd64.deb -rw-r--r-- 1 root root 301232 Jun 22 01:18 bind9-host_1%3a9.18.16-1~deb12u1_amd64.deb -rw-r--r-- 1 root root 1410348 Jun 22 01:18 bind9-libs_1%3a9.18.16-1~deb12u1_amd64.deb -rw-r--r-- 1 root root 886284 Jan 21 2023 fd-find_8.6.0-3_amd64.deb -rw-r--r-- 1 root root 160188 Jul 14 22:55 libldb2_2%3a2.6.2+samba4.17.9+dfsg-0+deb12u3_amd64.deb -rw-r--r-- 1 root root 81512 Jul 14 22:55 libsmbclient_2%3a4.17.9+dfsg-0+deb12u3_amd64.deb -rw-r--r-- 1 root root 52264 Jul 14 22:55 libwbclient0_2%3a4.17.9+dfsg-0+deb12u3_amd64.deb -rw-r--r-- 1 root root 759428 Jun 16 02:12 libx11-6_2%3a1.8.4-2+deb12u1_amd64.deb -rw-r--r-- 1 root root 291808 Jun 16 02:07 libx11-data_2%3a1.8.4-2+deb12u1_all.deb -rw-r--r-- 1 root root 192148 Jun 16 02:12 libx11-xcb1_2%3a1.8.4-2+deb12u1_amd64.deb -rw-r----- 1 root root 0 Jun 10 10:43 lock drwx------ 2 _apt root 4096 Aug 4 12:58 partial -rw-r--r-- 1 root root 5655676 Jul 14 22:55 samba-libs_2%3a4.17.9+dfsg-0+deb12u3_amd64.deb
You can use "apt clean" or "apt autoclean" command to remove outdated or all cached files.
$ sudo apt clean
$ sudo apt autoclean
The main difference between "apt clean" and "apt autoclean" is that "apt clean" removes all cached package files, while "apt autoclean" only removes outdated cached package files, retaining the latest versions.
Running "apt autoremove" and "apt autoclean" periodically is a good practice to keep your system clean and free from unnecessary packages, thereby optimizing disk space usage.
Related Read: Nala – A Feature-rich Commandline Frontend For APT Package Manager
Linux Apt Command Cheat Sheet
Below is a tabular column listing all the essential APT commands. Print it out and keep it as a reference for easy access:
APT Command | Description |
---|---|
apt update | Update the local package index |
apt upgrade | Upgrade all installed packages |
apt full-upgrade | upgrade the system by removing/installing/upgrading packages |
apt install [package] | Install a new package |
apt remove [package] | Remove a package |
apt autoremove | Remove unused dependencies |
apt purge [package] | Remove a package along with its configuration |
apt dist-upgrade | Upgrade to a new distribution release |
apt clean | Remove cached package files |
apt autoclean | Remove partially downloaded package files |
apt-mark hold [package] | Prevent a package from updating |
apt-mark unhold [package] | Unhold a package |
apt-mark showhold | Show the list of packages on hold |
apt-cache search [keyword] | Search for packages matching a keyword |
apt-cache show [package] | Display detailed information about a package |
apt-cache depends [package] | Show the dependencies of a package |
apt-cache rdepends [package] | Show reverse dependencies of a package |
apt-cache policy [package] | Display the installation policy for a package |
apt-key add [keyfile] | Add a GPG key from a key file |
apt-key del [keyID] | Remove a GPG key by its ID |
apt-key list | List all GPG keys |
apt --help | Display help information for apt-get command |
apt-cache --help | Display help information for apt-cache command |
apt-key --help | Display help information for apt-key command |
man apt | Open the manual page for apt-get command |
man apt-cache | Open the manual page for apt-cache command |
man apt-key | Open the manual page for apt-key command |
Heads Up: In the "Description" column, I've provided a brief description of what each command does. Make sure to replace "[package]" and "[keyword]" with actual package names and keywords, respectively, when using the commands.
Keep this handy reference nearby to quickly access and use these essential APT commands for efficient package management on your Debian-based Linux system.
Frequently Asked Questions (FAQ)
Q: What is APT in Linux?A: APT (stands for Advanced Package Tool) is a package management system used to install, update, upgrade, search and remove packages in Debian-based Linux distributions.
Q: How does apt differ from apt-get and apt-cache?A: APT provides a more user-friendly interface and combines functionalities of apt-get and apt-cache. While apt-get and apt-cache are still available for compatibility, APT offers more intuitive and interactive commands for managing software packages.
Q: Which Linux distributions use APT as the default package managerA: APT is primarily used in Debian and its derivatives, including popular Linux distributions like Ubuntu, Linux Mint, Kubuntu, Xubuntu, Lubuntu, elementary OS, Pop!_OS and many others.
Q: How can I install a new package using APT?A: To install a package, use the following command: sudo apt install package_name. Replace "package_name" with the name of the package you want to install.
Q: Can I search for packages with specific keywords using APT?A: Yes, you can search for packages using: apt-cache search keyword
Q: How do I update the package list and upgrade installed packages using APT?A: To update the package list, run: sudo apt updateTo upgrade installed packages, use: sudo apt upgrade
Q: What's the difference between "apt upgrade," "apt full-upgrade," and "apt dist-upgrade"?A: "apt upgrade" upgrades packages without handling complex dependency changes. "apt full-upgrade" (or "apt-get dist-upgrade") handles package changes more aggressively, and "apt dist-upgrade" can also perform distribution upgrades.
Q: How do I hold a package from automatic updates using apt-mark?A: To hold a package, use the following command: sudo apt-mark hold package_name
Q: How to unhold a package?A: If you decide to update a held package, you can unhold it using the following command:sudo apt-mark unhold package_nameThis will allow the package to be updated in future system-wide updates.
Q: What command can you use to check if a package is held?A: You can check if a package is held using: apt-mark showhold
Q: Can you hold and unhold packages managed by other package managers like Snap or Flatpak using "apt-mark"?A: No, the "apt-mark" command is specific to APT and applies only to packages managed by APT. Other package managers have their own mechanisms for version locking.
Q: How can I remove a package using APT?A: To remove a package, use: sudo apt remove package_nameThis will remove the package but keep its configuration files. Use apt purge to remove the package along with its configurations.
Q: What is "apt autoremove," and when should I use it?A: The "apt autoremove" command removes unused packages that were automatically installed as dependencies but are no longer needed. Use it to free up disk space and keep your system clean.
Q: Is it safe to clean the APT cache?A: Yes, cleaning the APT cache with apt clean or apt autoclean is safe. It removes cached package files that can be re-downloaded if needed.
Conclusion
In this article, we discussed some of the most common APT Linux commands with examples. We also covered the differences between apt upgrade, apt full-upgrade, and apt dist-upgrade. At the end of the guide, we have included a handy APT Linux command cheat sheet and a list of frequently asked questions (FAQs).
We hope this article has given you a better understanding of how to use APT to manage your software packages in Debian-based systems.
以上是掌握APT Linux命令:一份具有15個實際示例的綜合指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Linux的主要用途包括:1.服務器操作系統,2.嵌入式系統,3.桌面操作系統,4.開發和測試環境。 Linux在這些領域表現出色,提供了穩定性、安全性和高效的開發工具。

互聯網運行不依賴單一操作系統,但Linux在其中扮演重要角色。 Linux廣泛應用於服務器和網絡設備,因其穩定性、安全性和可擴展性受歡迎。

Linux操作系統的核心是其命令行界面,通過命令行可以執行各種操作。 1.文件和目錄操作使用ls、cd、mkdir、rm等命令管理文件和目錄。 2.用戶和權限管理通過useradd、passwd、chmod等命令確保系統安全和資源分配。 3.進程管理使用ps、kill等命令監控和控制系統進程。 4.網絡操作包括ping、ifconfig、ssh等命令配置和管理網絡連接。 5.系統監控和維護通過top、df、du等命令了解系統運行狀態和資源使用情況。

介紹 Linux是一個強大的操作系統,由於其靈活性和效率,開發人員,系統管理員和電源用戶都喜歡。但是,經常使用長而復雜的命令可能是乏味的

Linux適用於服務器、開發環境和嵌入式系統。 1.作為服務器操作系統,Linux穩定高效,常用於部署高並發應用。 2.作為開發環境,Linux提供高效的命令行工具和包管理系統,提升開發效率。 3.在嵌入式系統中,Linux輕量且可定制,適合資源有限的環境。

簡介:通過基於Linux的道德黑客攻擊數字邊界 在我們越來越相互聯繫的世界中,網絡安全至關重要。 道德黑客入侵和滲透測試對於主動識別和減輕脆弱性至關重要

Linux基礎學習從零開始的方法包括:1.了解文件系統和命令行界面,2.掌握基本命令如ls、cd、mkdir,3.學習文件操作,如創建和編輯文件,4.探索高級用法如管道和grep命令,5.掌握調試技巧和性能優化,6.通過實踐和探索不斷提陞技能。

Linux在服務器、嵌入式系統和桌面環境中的應用廣泛。 1)在服務器領域,Linux因其穩定性和安全性成為託管網站、數據庫和應用的理想選擇。 2)在嵌入式系統中,Linux因其高度定制性和高效性而受歡迎。 3)在桌面環境中,Linux提供了多種桌面環境,滿足不同用戶需求。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Dreamweaver Mac版
視覺化網頁開發工具

記事本++7.3.1
好用且免費的程式碼編輯器