首页 > 系统教程 > 操作系统 > 掌握APT Linux命令:一份具有15个实际示例的综合指南

掌握APT Linux命令:一份具有15个实际示例的综合指南

Lisa Kudrow
发布: 2025-03-22 09:50:10
原创
221 人浏览过

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.

掌握APT Linux命令:一份具有15个实际示例的综合指南

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.

掌握APT Linux命令:一份具有15个实际示例的综合指南

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>
登录后复制

Let’s search for a package named Kitty terminal emulator.

$ apt search kitty
登录后复制

掌握APT Linux命令:一份具有15个实际示例的综合指南

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
登录后复制

掌握APT Linux命令:一份具有15个实际示例的综合指南

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.

掌握APT Linux命令:一份具有15个实际示例的综合指南

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.

掌握APT Linux命令:一份具有15个实际示例的综合指南

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.

掌握APT Linux命令:一份具有15个实际示例的综合指南

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
登录后复制

掌握APT Linux命令:一份具有15个实际示例的综合指南

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
登录后复制

掌握APT Linux命令:一份具有15个实际示例的综合指南

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
登录后复制

掌握APT Linux命令:一份具有15个实际示例的综合指南

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.

掌握APT Linux命令:一份具有15个实际示例的综合指南

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 ' command. The 'apt install' command will install the given package if not already installed and will update the package if the latest version is found.

$ 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.

掌握APT Linux命令:一份具有15个实际示例的综合指南

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
Apt Command Cheat sheet

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 manager

A: 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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板