25 Useful Basic Commands of APT-GET and APT-CACHE for Package Management

[‘

n

This article explains how quickly you can learn to install, remove, update and search software packages using apt-get and apt-cache commands from the command line. This article provides some useful commands that will help you to handle package management in Debian/Ubuntu based systems.

n

APT-GET and APT-CACHE Commands
APT-GET and APT-CACHE Commands

n

What is apt-get?

n

The apt-get utility is a powerful and free package management command line program, that is used to work with Ubuntu’s APT (Advanced Packaging Tool) library to perform installation of new software packages, removing existing software packages, upgrading of existing software packages and even used to upgrading the entire operating system.

n

What is apt-cache?

n

The apt-cache command line tool is used for searching apt software package cache. In simple words, this tool is used to search software packages, collects information of packages and also used to search for what available packages are ready for installation on Debian or Ubuntu based systems.

n

APT-CACHE – 5 Useful Basic Commands

n

1. How Do I List All Available Packages?

n

To list all the available packages, type the following command.

n

$ apt-cache pkgnames

n

esseract-ocr-epornpipenightdreamsrnmumudvbrntbb-examplesrnlibsvm-javarnlibmrpt-hmtslam0.9rnlibboost-timer1.50-devrnkcm-touchpadrng++-4.5-multilibrn...

n

2. How Do I Find Out Package Name and Description of Software?

n

To find out the package name and with it description before installing, use the ‘search‘ flag. Using “search” with apt-cache will display a list of matched packages with short description. Let’s say you would like to find out description of package ‘vsftpd‘, then command would be.

n

$ apt-cache search vsftpd

n

vsftpd - lightweight, efficient FTP server written for securityrnccze - A robust, modular log coloriserrnftpd - File Transfer Protocol (FTP) serverrnyasat - simple stupid audit tool

n

To find and list down all the packages starting with ‘vsftpd‘, you could use the following command.

n

$ apt-cache pkgnames vsftpd

n

vsttpd

n

3. How Do I Check Package Information?

n

For example, if you would like to check information of package along with it short description say (version number, check sums, size, installed size, category etc). Use ‘show‘ sub command as shown below.

n

$ apt-cache show netcat

n

Package: netcatrnPriority: optionalrnSection: universe/netrnInstalled-Size: 30rnMaintainer: Ubuntu Developers <[emailxa0protected]>rnOriginal-Maintainer: Ruben Molina <[emailxa0protected]>rnArchitecture: allrnVersion: 1.10-40rnDepends: netcat-traditional (>= 1.10-39)rnFilename: pool/universe/n/netcat/netcat_1.10-40_all.debrnSize: 3340rnMD5sum: 37c303f02b260481fa4fc9fb8b2c1004rnSHA1: 0371a3950d6967480985aa014fbb6fb898bcea3arnSHA256: eeecb4c93f03f455d2c3f57b0a1e83b54dbeced0918ae563784e86a37bcc16c9rnDescription-en: TCP/IP swiss army knife -- transitional packagern This is a "dummy" package that depends on lenny's default version ofrn netcat, to ease upgrades. It may be safely removed.rnDescription-md5: 1353f8c1d079348417c2180319bdde09rnBugs: https://bugs.launchpad.net/ubuntu/+filebugrnOrigin: Ubuntu

n

4. How Do I Check Dependencies for Specific Packages?

n

Use the ‘showpkg‘ sub command to check the dependencies for particular software packages. whether those dependencies packages are installed or not. For example, use the ‘showpkg‘ command along with package-name.

n

$ apt-cache showpkg vsftpd

n

Package: vsftpdrnVersions: rn2.3.5-3ubuntu1 (/var/lib/apt/lists/in.archive.ubuntu.com_ubuntu_dists_quantal_main_binary-i386_Packages)rn Description Language: rn                 File: /var/lib/apt/lists/in.archive.ubuntu.com_ubuntu_dists_quantal_main_binary-i386_Packagesrn                  MD5: 81386f72ac91a5ea48f8db0b023f3f9brn Description Language: enrn                 File: /var/lib/apt/lists/in.archive.ubuntu.com_ubuntu_dists_quantal_main_i18n_Translation-enrn                  MD5: 81386f72ac91a5ea48f8db0b023f3f9brnrnReverse Depends: rn  ubumirror,vsftpdrn  harden-servers,vsftpdrnDependencies: rn2.3.5-3ubuntu1 - debconf (18 0.5) debconf-2.0 (0 (null)) upstart-job (0 (null)) libc6 (2 2.15) libcap2 (2 2.10) libpam0g (2 0.99.7.1) libssl1.0.0 (2 1.0.0) libwrap0 (2 7.6-4~) adduser (0 (null)) libpam-modules (0 (null)) netbase (0 (null)) logrotate (0 (null)) ftp-server (0 (null)) ftp-server (0 (null)) rnProvides: rn2.3.5-3ubuntu1 - ftp-server rnReverse Provides:

n

5. How Do I Check statistics of Cache

n

The ‘stats‘ sub command will display overall statistics about the cache. For example, the following command will display Total package names is the number of packages have found in the cache.

n

$ apt-cache stats

n

Total package names: 51868 (1,037 k)rnTotal package structures: 51868 (2,490 k)rn  Normal packages: 39505rn  Pure virtual packages: 602rn  Single virtual packages: 3819rn  Mixed virtual packages: 1052rn  Missing: 6890rnTotal distinct versions: 43015 (2,753 k)rnTotal distinct descriptions: 81048 (1,945 k)rnTotal dependencies: 252299 (7,064 k)rnTotal ver/file relations: 45567 (729 k)rnTotal Desc/File relations: 81048 (1,297 k)rnTotal Provides mappings: 8228 (165 k)rnTotal globbed strings: 286 (3,518 )rnTotal dependency version space: 1,145 krnTotal slack space: 62.6 krnTotal space accounted for: 13.3 M

n

APT-GET – 20 Useful Basic Commands for Package Management

n

6. How to Update System Packages

n

The ‘update‘ command is used to resynchronize the package index files from the their sources specified in /etc/apt/sources.list file. The update command fetched the packages from their locations and update the packages to newer version.

n

$ sudo apt-get update

n

[sudo] password for tecmint: rnIgn http://security.ubuntu.com quantal-security InRelease                      rnGet:1 http://security.ubuntu.com quantal-security Release.gpg [933 B]          rnGet:2 http://security.ubuntu.com quantal-security Release [49.6 kB]            rnIgn http://in.archive.ubuntu.com quantal InRelease                             rnIgn http://in.archive.ubuntu.com quantal-updates InRelease                     rnGet:3 http://repo.varnish-cache.org precise InRelease [13.7 kB]                rnIgn http://in.archive.ubuntu.com quantal-backports InRelease                   rnHit http://in.archive.ubuntu.com quantal Release.gpg                           rnGet:4 http://security.ubuntu.com quantal-security/main Sources [34.8 kB]       rnGet:5 http://in.archive.ubuntu.com quantal-updates Release.gpg [933 B]         rn...

n

7. How to Upgrade Software Packages

n

The ‘upgrade‘ command is used to upgrade all the currently installed software packages on the system. Under any circumstances currently installed packages are not removed or packages which are not already installed neither retrieved and installed to satisfy upgrade dependencies.

n

$ sudo apt-get upgrade

n

Reading package lists... DonernBuilding dependency tree       rnReading state information... DonernThe following packages have been kept back:rn  linux-headers-generic linux-image-generic wine1.5 wine1.5-i386rnThe following packages will be upgraded:rn  activity-log-manager-common activity-log-manager-control-center adium-theme-ubuntu alacartern  alsa-base app-install-data-partner appmenu-gtk appmenu-gtk3 apport apport-gtk aptrn  apt-transport-https apt-utils aptdaemon aptdaemon-data at-spi2-core bamfdaemon base-files bind9-hostrn   ...

n

However, if you want to upgrade, unconcerned of whether software packages will be added or removed to fulfill dependencies, use the ‘dist-upgrade‘ sub command.

n

$ sudo apt-get dist-upgrade

n

8. How Do I Install or Upgrade Specific Packages?

n

The ‘install‘ sub command is tracked by one or more packages wish for installation or upgrading.

n

$ sudo apt-get install netcat

n

Reading package lists... DonernBuilding dependency tree       rnReading state information... DonernThe following extra packages will be installed:rn  netcat-traditionalrnThe following NEW packages will be installed:rn  netcat netcat-traditionalrn0 upgraded, 2 newly installed, 0 to remove and 328 not upgraded.rnNeed to get 67.1 kB of archives.rnAfter this operation, 186 kB of additional disk space will be used.rnDo you want to continue [Y/n]? yrnGet:1 http://in.archive.ubuntu.com/ubuntu/ quantal/universe netcat-traditional i386 1.10-40 [63.8 kB]rnGet:2 http://in.archive.ubuntu.com/ubuntu/ quantal/universe netcat all 1.10-40 [3,340 B]rnFetched 67.1 kB in 1s (37.5 kB/s)rnSelecting previously unselected package netcat-traditional.rn(Reading database ... 216118 files and directories currently installed.)rnUnpacking netcat-traditional (from .../netcat-traditional_1.10-40_i386.deb) ...rnSelecting previously unselected package netcat.rnUnpacking netcat (from .../netcat_1.10-40_all.deb) ...rnProcessing triggers for man-db ...rnSetting up netcat-traditional (1.10-40) ...rnSetting up netcat (1.10-40) ...

n

9. How I can Install Multiple Packages?

n

You can add more than one package name along with the command in order to install multiple packages at the same time. For example, the following command will install packages ‘nethogs‘ and ‘goaccess‘.

n

$ sudo apt-get install nethogs goaccess

n

Reading package lists... DonernBuilding dependency tree       rnReading state information... Donerngoaccess is already the newest version.rnnethogs is already the newest version.rn0 upgraded, 0 newly installed, 0 to remove and 328 not upgraded.

n

10. How to Install Several Packages using Wildcard

n

With the help of regular expression you can add several packages with one string. For example, we use * wildcard to install several packages that contains the ‘*name*‘ string, name would be ‘package-name’.

n

$ sudo apt-get install '*name*'

n

11. How to install Packages without Upgrading

n

Using sub ‘–no-upgrade‘ command will prevent already installed packages from upgrading.

n

$ sudo apt-get install packageName --no-upgrade

n

Reading package lists... DonernBuilding dependency tree       rnReading state information... DonernSkipping vsftpd, it is already installed and upgrade is not set.rn0 upgraded, 0 newly installed, 0 to remove and 328 not upgraded.

n

12. How to Upgrade Only Specific Packages

n

The ‘–only-upgrade‘ command do not install new packages but it only upgrade the already installed packages and disables new installation of packages.

n

$ sudo apt-get install packageName --only-upgrade

n

Reading package lists... DonernBuilding dependency tree       rnReading state information... Donernvsftpd is already the newest version.rn0 upgraded, 0 newly installed, 0 to remove and 328 not upgraded.

n

13. How Do I Install Specific Package Version?

n

Let’s say you wish to install only specific version of packages, simply use the ‘=‘ with the package-name and append desired version.

n

$ sudo apt-get install vsftpd=2.3.5-3ubuntu1

n

Reading package lists... DonernBuilding dependency tree       rnReading state information... Donernvsftpd is already the newest version.rn0 upgraded, 0 newly installed, 0 to remove and 328 not upgraded.

n

14. How Do I Remove Packages Without Configuration

n

To un-install software packages without removing their configuration files (for later re-use the same configuration). Use the ‘remove‘ command as shown.

n

$ sudo apt-get remove vsftpd

n

[sudo] password for tecmint: rnReading package lists... DonernBuilding dependency tree       rnReading state information... DonernThe following packages will be REMOVED:rn  vsftpdrn0 upgraded, 0 newly installed, 1 to remove and 328 not upgraded.rnAfter this operation, 364 kB disk space will be freed.rnDo you want to continue [Y/n]? yrn(Reading database ... 216156 files and directories currently installed.)rnRemoving vsftpd ...rnvsftpd stop/waitingrnProcessing triggers for ureadahead ...rnProcessing triggers for man-db ...

n

15. How Do I Completely Remove Packages

n

To remove software packages including their configuration files, use the ‘purge‘ sub command as shown below.

n

$ sudo apt-get purge vsftpd

n

Reading package lists... DonernBuilding dependency tree       rnReading state information... DonernThe following packages will be REMOVED:rn  vsftpd*rn0 upgraded, 0 newly installed, 1 to remove and 328 not upgraded.rnAfter this operation, 0 B of additional disk space will be used.rnDo you want to continue [Y/n]? yrn(Reading database ... 216107 files and directories currently installed.)rnRemoving vsftpd ...rnPurging configuration files for vsftpd ...rnProcessing triggers for ureadahead ...

n

Alternatively, you can combine both the commands together as shown below.

n

$ sudo apt-get remove --purge vsftpd

n

Reading package lists... DonernBuilding dependency tree       rnReading state information... DonernThe following packages will be REMOVED:rn  vsftpd*rn0 upgraded, 0 newly installed, 1 to remove and 328 not upgraded.rnAfter this operation, 364 kB disk space will be freed.rnDo you want to continue [Y/n]? yrn(Reading database ... 216156 files and directories currently installed.)rnRemoving vsftpd ...rnvsftpd stop/waitingrnPurging configuration files for vsftpd ...rnProcessing triggers for ureadahead ...rnProcessing triggers for man-db ...

n

16. How I Can Clean Up Disk Space

n

The ‘clean‘ command is used to free up the disk space by cleaning retrieved (downloaded) .deb files (packages) from the local repository.

n

$ sudo apt-get clean

n

17. How Do I Download Only Source Code of Package

n

To download only source code of particular package, use the option ‘–download-only source‘ with ‘package-name’ as shown.

n

$ sudo apt-get --download-only source vsftpd

n

Reading package lists... DonernBuilding dependency tree       rnReading state information... DonernNeed to get 220 kB of source archives.rnGet:1 http://in.archive.ubuntu.com/ubuntu/ quantal/main vsftpd 2.3.5-3ubuntu1 (dsc) [1,883 B]rnGet:2 http://in.archive.ubuntu.com/ubuntu/ quantal/main vsftpd 2.3.5-3ubuntu1 (tar) [188 kB]rnGet:3 http://in.archive.ubuntu.com/ubuntu/ quantal/main vsftpd 2.3.5-3ubuntu1 (diff) [30.5 kB]rnFetched 220 kB in 4s (49.1 kB/s)rnDownload complete and in download only mode

n

18. How Can I Download and Unpack a Package

n

To download and unpack source code of a package to a specific directory, type the following command.

n

$ sudo apt-get source vsftpd

n

Reading package lists... DonernBuilding dependency tree       rnReading state information... DonernNeed to get 220 kB of source archives.rnGet:1 http://in.archive.ubuntu.com/ubuntu/ quantal/main vsftpd 2.3.5-3ubuntu1 (dsc) [1,883 B]rnGet:2 http://in.archive.ubuntu.com/ubuntu/ quantal/main vsftpd 2.3.5-3ubuntu1 (tar) [188 kB]rnGet:3 http://in.archive.ubuntu.com/ubuntu/ quantal/main vsftpd 2.3.5-3ubuntu1 (diff) [30.5 kB]rnFetched 220 kB in 1s (112 kB/s)  rngpgv: Signature made Thursday 24 May 2012 02:35:09 AM IST using RSA key ID 2C48EE4Erngpgv: Can't check signature: public key not foundrndpkg-source: warning: failed to verify signature on ./vsftpd_2.3.5-3ubuntu1.dscrndpkg-source: info: extracting vsftpd in vsftpd-2.3.5rndpkg-source: info: unpacking vsftpd_2.3.5.orig.tar.gzrndpkg-source: info: unpacking vsftpd_2.3.5-3ubuntu1.debian.tar.gzrndpkg-source: info: applying 01-builddefs.patchrndpkg-source: info: applying 02-config.patchrndpkg-source: info: applying 03-db-doc.patchrndpkg-source: info: applying 04-link-local.patchrndpkg-source: info: applying 05-whitespaces.patchrndpkg-source: info: applying 06-greedy.patchrndpkg-source: info: applying 07-utf8.patchrndpkg-source: info: applying 08-manpage.patchrndpkg-source: info: applying 09-s390.patchrndpkg-source: info: applying 10-remote-dos.patchrndpkg-source: info: applying 11-alpha.patchrndpkg-source: info: applying 09-disable-anonymous.patchrndpkg-source: info: applying 12-ubuntu-use-snakeoil-ssl.patch

n

19. How Can I Download, Unpack and Compile a Package

n

You can also download, unpack and compile the source code at the same time, using option ‘–compile‘ as shown below.

n

$ sudo apt-get --compile source goaccess

n

[sudo] password for tecmint: rnReading package lists... DonernBuilding dependency tree       rnReading state information... DonernNeed to get 130 kB of source archives.rnGet:1 http://in.archive.ubuntu.com/ubuntu/ quantal/universe goaccess 1:0.5-1 (dsc) [1,120 B]rnGet:2 http://in.archive.ubuntu.com/ubuntu/ quantal/universe goaccess 1:0.5-1 (tar) [127 kB]rnGet:3 http://in.archive.ubuntu.com/ubuntu/ quantal/universe goaccess 1:0.5-1 (diff) [2,075 B]rnFetched 130 kB in 1s (68.0 kB/s)rngpgv: Signature made Tuesday 26 June 2012 09:38:24 AM IST using DSA key ID A9FD4821rngpgv: Can't check signature: public key not foundrndpkg-source: warning: failed to verify signature on ./goaccess_0.5-1.dscrndpkg-source: info: extracting goaccess in goaccess-0.5rndpkg-source: info: unpacking goaccess_0.5.orig.tar.gzrndpkg-source: info: unpacking goaccess_0.5-1.debian.tar.gzrndpkg-buildpackage: source package goaccessrndpkg-buildpackage: source version 1:0.5-1rndpkg-buildpackage: source changed by Chris Taylor <[emailxa0protected]>rndpkg-buildpackage: host architecture i386rn dpkg-source --before-build goaccess-0.5rndpkg-checkbuilddeps: Unmet build dependencies: debhelper (>= 9) autotools-dev libncurses5-dev libglib2.0-dev libgeoip-dev autoconfrndpkg-buildpackage: warning: build dependencies/conflicts unsatisfied; abortingrndpkg-buildpackage: warning: (Use -d flag to override.)rn...

n

20. How Do I Download a Package Without Installing

n

Using ‘download‘ option, you can download any given package without installing it. For example, the following command will only download ‘nethogs‘ package to current working directory.

n

$ sudo apt-get download nethogs

n

Get:1 Downloading nethogs 0.8.0-1 [27.1 kB]rnFetched 27.1 kB in 3s (7,506 B/s)

n

21. How Do I Check Change Log of Package?

n

The ‘changelog‘ flag downloads a package change-log and shows the package version that is installed.

n

$ sudo apt-get changelog vsftpd

n

vsftpd (2.3.5-3ubuntu1) quantal; urgency=lowrnrn  * Merge from Debian testing (LP: #1003644).  Remaining changes:rn    + debian/vsftpd.upstart: migrate vsftpd to upstart.rn    + Add apport hook (LP: #513978):rn      - debian/vsftpd.apport: Added.rn      - debian/control: Build-depends on dh-apport.rn      - debian/rules: Add --with apport.rn    + Add debian/watch file.rn    + debian/patches/09-disable-anonymous.patch: Disable anonymous loginrn      by default. (LP: #528860)rn  * debian/patches/12-ubuntu-us-snakeoil-ssl.patch: Use snakeoil SSLrn    certificates and key.rnrn -- Andres Rodriguez <[emailxa0protected]>  Wed, 23 May 2012 16:59:36 -0400rn...

n

22. How Do I Check Broken Dependencies?

n

The ‘check‘ command is a diagnostic tool. It used to update package cache and checks for broken dependencies.

n

$ sudo apt-get check

n

[sudo] password for tecmint: rnReading package lists... DonernBuilding dependency tree       rnReading state information... Done

n

23. How Do I Search and Build Dependencies?

n

This ‘build-dep‘ command searches the local repositories in the system and install the build dependencies for package. If the package does not exists in the local repository it will return an error code.

n

$ sudo apt-get build-dep netcat

n

The following NEW packages will be installed:rn  debhelper dh-apparmor html2text po-debconf quiltrn0 upgraded, 5 newly installed, 0 to remove and 328 not upgraded.rnNeed to get 1,219 kB of archives.rnAfter this operation, 2,592 kB of additional disk space will be used.rnDo you want to continue [Y/n]? yrnGet:1 http://in.archive.ubuntu.com/ubuntu/ quantal/main html2text i386 1.3.2a-15build1 [91.4 kB]rnGet:2 http://in.archive.ubuntu.com/ubuntu/ quantal/main po-debconf all 1.0.16+nmu2ubuntu1 [210 kB]rnGet:3 http://in.archive.ubuntu.com/ubuntu/ quantal/main dh-apparmor all 2.8.0-0ubuntu5 [9,846 B]rnGet:4 http://in.archive.ubuntu.com/ubuntu/ quantal/main debhelper all 9.20120608ubuntu1 [623 kB]rnGet:5 http://in.archive.ubuntu.com/ubuntu/ quantal/main quilt all 0.60-2 [285 kB]rnFetched 1,219 kB in 4s (285 kB/s)rn...

n

24. How I Can Auto clean Apt-Get Cache?

n

The ‘autoclean‘ command deletes all .deb files from /var/cache/apt/archives to free-up significant volume of disk space.

n

$ sudo apt-get autoclean

n

Reading package lists... DonernBuilding dependency tree       rnReading state information... Done

n

25. How I Can Auto remove Installed Packages?

n

The ‘autoremove‘ sub command is used to auto remove packages that were certainly installed to satisfy dependencies for other packages and but they were now no longer required. For example, the following command will remove an installed package with its dependencies.

n

$ sudo apt-get autoremove vsftpd

n

Reading package lists... DonernBuilding dependency tree       rnReading state information... DonernPackage 'vsftpd' is not installed, so not removedrn0 upgraded, 0 newly installed, 0 to remove and 328 not upgraded.

n

I’ve covered most of the available options with apt-get and apt-cache commands, but still there are more options available, you can check them out using ‘man apt-get‘ or ‘man apt-cache‘ from the terminal. I hope you enjoyed reading this article, If I’ve missed anything and you would like me to add to the list. Please feel free to mention in the comment below.

n

Read Also : 20 Useful Linux YUM Commands for Package Management

n

‘]