Debian-based systems (including Ubuntu) use the apt-* command to manage packages from the command line.

In this article, using an Apache 2 installation as an example, let’s see how to use the apt-* command to view, install, remove, or upgrade packages.

 

1. Apt-cache search: use the package name to search the repository

If you have Apache 2 installed, you might have guessed that the package name is Apache2. To verify that it is a valid package name, you may need to search the repository for that particular package name, as shown below.

The following example shows how to search for a specific package name in the repository.

$ apt-cache search ^apache2$
apache2 - Apache HTTP Server metapackage
Copy the code

2. Apt-cache search: use package descriptions to search repositories

If you don’t know the exact name of the package, you can still search using the package description, as shown below.

$ apt-cache search "Apache HTTP Server" apache2 - Apache HTTP Server metapackage apache2-doc - Apache HTTP Server documentation apache2-mpm-event - Apache HTTP Server - event driven model apache2-mpm-prefork - Apache HTTP Server - Traditional non-threaded Model apache2- MPM - worker-apache HTTP Server - high speed threaded Model apache2.2-common - Apache HTTP Server common filesCopy the code

3. Apt-file search: Searches repositories using file names in packages

Sometimes, you might know the configuration file name (or executable file name) in the package you want to install.

 

The following example shows that the apache2.conf file is part of the Apache2.2-common package. Use the apt-file command to search for the repository using the configuration file name, as shown below.

$apt - file search apache2. Conf apache2.2 - common: / etc/apache2 / apache2. Conf apache2.2 - common: The/usr/share/doc/apache2.2 - common/examples/apache2 / apache2. Conf. GzCopy the code

4. Apt-cache show: basic information about a packet

The following example shows basic information about the apache2 package.

$ apt-cache show apache2 Package: apache2 Priority: optional Maintainer: Ubuntu Core Developers Original-Maintainer: Debian Apache Maintainers Version: 2.2.11-2Ubuntu2.3 Depends: Apache2 MPM - worker (> = 2.2.11-2 ubuntu2. 3) | - prefork apache2 - MPM (> = 2.2.11-2 ubuntu2. 3) | apache2 MPM - event (> = 2.2.11-2 ubuntu2. 3) Filename: pool/main/a/apache2 / apache2_2. 2.11 2 ubuntu2. 3 _all. Deb Size: 46350 Description: Apache HTTP Server metapackage The Apache Software Foundation's goal is to build a secure, efficient and extensible HTTP server as standards-compliant open source software. Homepage: http://httpd.apache.org/Copy the code

Apt-cache showpkg: Details about a package

“Apt-cache show” displays basic package information. Use apt-cache showpkg to display package details, as shown in the following figure.

$ apt-cache showpkg apache2 Package: apache2 Versions: 2.2.11-2 ubuntu2. 3 (/ var/lib/apt/lists/us.archive.ubuntu.com _ubuntu_dists_jaunty - updates_main_binary - i386_Packages) (/var/lib/apt/lists/security.ubuntu.com_ubuntu_dists_jaunty-security_main_binary-i386_Packages) Description Language: File: /var/lib/apt/lists/us.archive.ubuntu.com_ubuntu_dists_jaunty-updates_main_binary-i386_Packages MD5: d24f049cd70ccfc178dd8974e4b1ed01 Reverse Depends: squirrelmail,apache2 squid3-cgi,apache2 phpmyadmin,apache2 mahara-apache2,apache2 ipplan,apache2 Dependencies: 2.2.11-2ubuntu2.3 - Apache2 -mpm-prefork (2.2.11-2ubuntu2.3) Apache2-mpm-event (2 2.2.11-2ubuntu2-3) 2.2.11-2ubuntu2-apache2-mpm-worker (18 2.2.11-2ubuntu2) apache2-mpm-prefork (18 2.2.11-2Ubuntu2) apache 2-Mmp-Event (2 2.2.11-2Ubuntu2) Provides: 2.2.11-2Ubuntu 2. 3-2.2.11-2Ubuntu 2-reverse Provides: Apache2-mpm-itk 2.2.6-02-1build4.3 apache2-mpm-worker 2.2.11-2ubuntu2.3 Apache2-mpm-prefork 2.2.11-2ubuntu2 Apache2 MPM - prefork 2.2.11-2 ubuntu2 apache2 MPM - event 2.2.11-2 ubuntu2Copy the code

6. Apt-file list: Lists all files in the package

Use “apt-file list” to display all files located in the Apache2 package, as shown below.

$ apt-file list apache2 | more
apache2: /usr/share/bug/apache2/control
apache2: /usr/share/bug/apache2/script
apache2: /usr/share/doc/apache2/NEWS.Debian.gz
apache2: /usr/share/doc/apache2/README.Debian.gz
apache2: /usr/share/doc/apache2/changelog.Debian.gz
...
Copy the code

7. Apt-cache depends: List all dependent packages

Before installation, if you want to view all dependent packages, please use “apt-cache Depends”, as shown below.

$ apt-cache depends apache2
apache2
 |Depends: apache2-mpm-worker
 |Depends: apache2-mpm-prefork
  Depends: apache2-mpm-event
Copy the code

8. Dkg-l: Is the package installed?

Before installing the package, you may want to make sure it is not installed using the DPKG -l command, as shown below.

$ dpkg -l | grep -i apache
Copy the code

9. Apt-get install

Finally, use “apt-get Install” to install the package, as shown below.

$ sudo apt-get install apache2 [sudo] password for ramesh: The following NEW packages will be installed: Apache2 apache2- MPM -worker apache2-utils apache2.2-common libapr1 libaprutil1 libpq5 0 Upgrade, 7 newly installed, 0 to remove and 26 not upgraded.Copy the code

10. DKG -l: verifies whether the package is successfully installed

After the installation package is installed, use DPKG -l to ensure successful installation.

$DPKG -l | grep apache ii apache2 2.2.11-2 ubuntu2. 3 the apache HTTP Server metapackage apache2 ii - MPM - worker 2.2.11-2Ubuntu2.3 Apache HTTP Server - High Speed threaded mod II Apache2-utils 2.2.11-2Ubuntu2.3 Utility Programs for Webservers II Apache2.2-common 2.2.11-2Ubuntu2.3 Apache HTTP Server Common FilesCopy the code

11. Apt-get remove: Delete a package

Use “apt-get Purge” or “apt-get remove” to delete a package, as shown below.

$ sudo apt-get purge apache2 (or) $ sudo apt-get remove apache2 The following packages were automatically installed and are no longer required: Apache2-utils linux-headers 2.6.28-11 libapr1 apache2.2-common linux-headers 2.6.28-11-generic apache2-mpm-worker libpq5  libaprutil1 Use 'apt-get autoremove' to remove them. The following packages will be REMOVED: apache2 0 upgraded, 0 newly installed, 1 to remove and 26 not upgraded. Removing apache2 ...Copy the code
  • Deletion of apt-get will not delete the configuration file of the package
  • Apt-get purge will delete the package configuration file

12. Apt-get -u install: Upgrade specific packages

The following example shows how to upgrade a specific software package.

$ sudo apt-get -u install apache2 Reading package lists... Done Building dependency tree Reading state information... Done apache2 is already the newest version. The following packages were automatically installed and are no longer required: Linux-headers 2.6.28-11 linux-headers 2.6.28-11-generic Use 'apt-get autoremove' toremove them. 0 upgrade, 0 newly installed, 0 to remove and 26 not upgraded.Copy the code

13. Apt-get -u upgrade: Upgrade all packages

To upgrade all packages to the latest version, use “apt-get -u upgrade”, as shown below.

$ sudo apt-get -u upgrade
The following packages will be upgraded:
  libglib2.0-0 libglib2.0-data libicu38 libsmbclient libwbclient0
  openoffice.org-base-core openoffice.org-calc openoffice.org-common
  openoffice.org-core openoffice.org-draw openoffice.org-emailmerge
  openoffice.org-gnome openoffice.org-gtk openoffice.org-impress
  openoffice.org-math openoffice.org-style-human openoffice.org-writer
  python-uno samba-common smbclient ttf-opensymbol tzdata
26 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Copy the code