| by Arround The Web | No comments

APT-CACHE and APT-GET commands for package management in Ubuntu

Introduction

In an earlier article, we demonstrated how you could use the dpkg package manager to install, remove and query information about software packages in the Ubuntu OS.
In this article, we will show you how to use apt-cache to search for and query information about packages available in online and local repositories and we will also show you how to use apt-get to install and uninstall packages.

Essentially apt-cache is the tool we use to query the apt software cache to obtain information about packages and apt-get is the tool we use for installing packages and modifying the state of packages installed on the system.

All the examples demonstrated in this article were performed on an Ubuntu 16.04 system.

 

APT-CACHE examples

 

Example 1: List all available packages
To list all packages available to be installed, we use the apt-cache pkgnames command as shown below

root@linuxnix:~# apt-cache pkgnames
libdatrie-doc
libfstrcmp0-dbg
librime-data-sampheng
python-pyao-dbg
fonts-georgewilliams
python3-aptdaemon.test
libcollada2gltfconvert-dev
python3-doc8
r-bioc-hypergraph
angrydd
fonts-linuxlibertine
---------output truncated for brevity

Note that this command only shows the package names and no other information about the package.

Example 2: Search for a package
To search for a package use the apt-cache search command followed by the package name.
Let’s search for the nano text editor.

root@linuxnix:~# apt-cache search nano
nano - small, friendly text editor inspired by Pico
alpine-pico - Simple text editor from Alpine, a text-based email client
gwave - waveform viewer eg for spice simulators
kiki-the-nano-bot - 3D puzzle game, mixing Sokoban and Kula-World
kiki-the-nano-bot-data - Kiki the nano bot - game data
libaudio-moosic-perl - Moosic client library for Perl
--------------------------------------output truncated for brevity

The apt-cache search command prints the package name along with a short one-line description of the package.
This command performs a fuzzy match as it matches for the string being searched in the package names as well as package description.
Therefore, we get a lot of results and most of them are not entirely accurate.

Example 3: Query information about a package
To obtain information about a package we use the apt-cache show command followed by the package name.
Let’s view available information for the nano package.

root@linuxnix:~# apt-cache show nano
Package: nano
Architecture: amd64
Version: 2.5.3-2ubuntu2
Priority: standard
Section: editors
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Jordi Mallach <jordi@debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 684
Provides: editor
Depends: libc6 (>= 2.14), libncursesw5 (>= 6), libtinfo5 (>= 6)
Suggests: spell
Conflicts: pico
Replaces: pico
Filename: pool/main/n/nano/nano_2.5.3-2ubuntu2_amd64.deb
Size: 190566
MD5sum: e31024f60c11f615be8c3abf86af8cd9
SHA1: b2044c27b55e81306128822027d0277ae03a5487
SHA256: 1c0ce9033e272743d4037a063c46b011f73efbbd38932bc3a351d5bc471d1a5e
Homepage: http://www.nano-editor.org/
Description-en: small, friendly text editor inspired by Pico
GNU nano is an easy-to-use text editor originally designed as a replacement
for Pico, the ncurses-based editor from the non-free mailer package Pine
(itself now available under the Apache License as Alpine).
.
However, nano also implements many features missing in pico, including:
- feature toggles;
- interactive search and replace (with regular expression support);
- go to line (and column) command;
- auto-indentation and color syntax-highlighting;
- filename tab-completion and support for multiple buffers;
- full internationalization support.
Description-md5: b7e1d8c3d831118724cfe8ea3996b595
Task: standard, ubuntu-touch-core, ubuntu-touch
Supported: 5y

Package: nano
Priority: standard
Section: editors
Installed-Size: 684
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Jordi Mallach <jordi@debian.org>
Architecture: amd64
Version: 2.5.3-2
Replaces: pico
Provides: editor
Depends: libc6 (>= 2.14), libncursesw5 (>= 6), libtinfo5 (>= 6)
Suggests: spell
Conflicts: pico
Filename: pool/main/n/nano/nano_2.5.3-2_amd64.deb
Size: 190920
MD5sum: bd757bcdb6ffced902490ca2803c8e15
SHA1: 5baa89fa02ef15f26ce09fdbfd1d0be61349b7d7
SHA256: 2a61014111de157e6ce05f9e970563719872d5908f8bef43b3576228b2e0af0a
Description-en: small, friendly text editor inspired by Pico
GNU nano is an easy-to-use text editor originally designed as a replacement
for Pico, the ncurses-based editor from the non-free mailer package Pine
(itself now available under the Apache License as Alpine).
.
However, nano also implements many features missing in pico, including:
- feature toggles;
- interactive search and replace (with regular expression support);
- go to line (and column) command;
- auto-indentation and color syntax-highlighting;
- filename tab-completion and support for multiple buffers;
- full internationalization support.
Description-md5: b7e1d8c3d831118724cfe8ea3996b595
Homepage: http://www.nano-editor.org/
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Origin: Ubuntu
Supported: 5y
Task: standard, ubuntu-touch-core, ubuntu-touch

This shows a lot of useful information about the package like the available versions, the .deb package file name, dependencies and conflicts and also a package description.

Example 4: Get cache statistics
To check the total number of packages available, we the apt-cache stats command.

root@linuxnix:~# apt-cache stats
Total package names: 70538 (1,411 k)
Total package structures: 70546 (3,104 k)
Normal packages: 55326
Pure virtual packages: 1146
Single virtual packages: 4764
Mixed virtual packages: 474
Missing: 8836
Total distinct versions: 62726 (5,018 k)
Total distinct descriptions: 118584 (2,846 k)
Total dependencies: 369801/98006 (8,964 k)
Total ver/file relations: 1482 (35.6 k)
Total Desc/File relations: 53048 (1,273 k)
Total Provides mappings: 12818 (308 k)
Total globbed strings: 157526 (3,489 k)
Total slack space: 16.4 k
Total space accounted for: 26.9 M
Total buckets in PkgHashTable: 50503
Unused: 12488
Used: 38015
Utilization: 75.2728%
Average entries: 1.85574
Longest: 8
Shortest: 1
Total buckets in GrpHashTable: 50503
Unused: 12488
Used: 38015
Utilization: 75.2728%
Average entries: 1.85553
Longest: 8
Shortest: 1
root@linuxnix:~#

 

APT-GET examples

 

Example 5: Update ubuntu package lists
The apt-get update command downloads the package lists from the repositories and “updates” them to get information on the newest versions of packages and their dependencies.
This command is used to re-synchronize the package index files from their sources.
The indexes of available packages are fetched from the location(s) specified in /etc/apt/sources.list
Let’s execute it.

root@linuxnix:~# apt-get update
Hit:1 http://ap-southeast-1.ec2.archive.ubuntu.com/ubuntu xenial InRelease
Hit:2 http://ap-southeast-1.ec2.archive.ubuntu.com/ubuntu xenial-updates InRelease
Hit:3 http://ap-southeast-1.ec2.archive.ubuntu.com/ubuntu xenial-backports InRelease
Get:4 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Fetched 102 kB in 1s (77.1 kB/s)
Reading package lists... Done
root@linuxnix:~#

Example 6: Update the operating system
The apt-get upgrade command is used to upgrade all the currently installed software packages on the system.
the apt-get upgrade command does not remove any currently installed package.

root@linuxnix:~# apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
linux-aws-headers-4.4.0-1032 linux-headers-4.4.0-1032-aws linux-image-4.4.0-1032-aws
Use 'apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@linuxnix:~#

If you want to upgrade, unconcerned about whether software packages will be added or removed to fulfill dependencies, use the apt-get dist-upgrade command.

root@linuxnix:~# apt-get dist-upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
linux-aws-headers-4.4.0-1032 linux-headers-4.4.0-1032-aws linux-image-4.4.0-1032-aws
Use 'apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@linuxnix:~#

The system that I’m working on has already been updated with the latest available package updates and that’s why the upgrade commands completed with zero package updates.

Example 7: Install a package
To install a package we use the apt-get install command followed by the package name.
For demonstration purpose, let’s install the nmap package.

root@linuxnix:~# apt-get install nmap
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-aws-headers-4.4.0-1032 linux-headers-4.4.0-1032-aws linux-image-4.4.0-1032-aws
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
libblas-common libblas3 liblinear3 liblua5.2-0 libxslt1.1 lua-lpeg ndiff python-bs4 python-chardet python-html5lib python-lxml
python-pkg-resources python-six
Suggested packages:
liblinear-tools liblinear-dev python-genshi python-lxml-dbg python-lxml-doc python-setuptools
The following NEW packages will be installed:
libblas-common libblas3 liblinear3 liblua5.2-0 libxslt1.1 lua-lpeg ndiff nmap python-bs4 python-chardet python-html5lib
python-lxml python-pkg-resources python-six
0 upgraded, 14 newly installed, 0 to remove and 0 not upgraded.
Need to get 6,312 kB of archives.
After this operation, 28.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
------------------------------output truncated for brevity

When we type in apt-get install, the apt-get tool first goes through the package lists to find out which repository has the package we are trying to install.
It then gathers information about the package dependencies in the steps step.
Once the required package information is available, the package and any dependencies are downloaded from the appropriate repository and installed on the system.

Example 8: Install packages using wildcards
We can use * wildcard to install packages that match a given package name.
In the below example we attempt to install all packages that have the name git.

root@linuxnix:~# apt-get install git*
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'git-sh' for glob 'git*'
Note, selecting 'gitolite' for glob 'git*'
Note, selecting 'git-ftp' for glob 'git*'
Note, selecting 'git-big-picture' for glob 'git*'
Note, selecting 'git-gui' for glob 'git*'
Note, selecting 'gitlab-shell' for glob 'git*'
Note, selecting 'git-hub' for glob 'git*'
------------------------------output truncated for brevity

Example 9: Install multiple packages in a single command
To install more than one package in a single apt-get install command just specify the additional package names separated by whitespace.
For example:

apt-get install <package1> <package2>

Example 10: Install a specific version of a package
To install a specific version of a package, use the syntax apt-get install <package name>=<required version>.
You may obtain the available versions of a package using the apt-cache show <package name> command.
In the below example, we try to install a specific version of the nano package.

root@linuxnix:~# apt-get install nano=2.5.3-2
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-aws-headers-4.4.0-1032 linux-headers-4.4.0-1032-aws linux-image-4.4.0-1032-aws
Use 'apt autoremove' to remove them.
Suggested packages:
spell
The following packages will be DOWNGRADED:
nano
0 upgraded, 0 newly installed, 1 downgraded, 0 to remove and 0 not upgraded.
Need to get 191 kB of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n]

Since a higher version of the package was already installed, apt-get would’ve performed a downgrade of the package if we had gone ahead with the install.

Example 11: Remove a package
To remove a package we use the apt-get remove command followed by the package name.
Let’s remove the nmap package that we had installed earlier.

root@linuxnix:~# apt-get remove nmap
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libblas-common libblas3 liblinear3 liblua5.2-0 libxslt1.1 linux-aws-headers-4.4.0-1032 linux-headers-4.4.0-1032-aws
linux-image-4.4.0-1032-aws lua-lpeg ndiff python-bs4 python-chardet python-html5lib python-lxml python-pkg-resources python-six
Use 'apt autoremove' to remove them.
The following packages will be REMOVED:
nmap
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 21.3 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 108793 files and directories currently installed.)
Removing nmap (7.01-2ubuntu2) ...
Processing triggers for man-db (2.7.5-1) ...

Example 12: Download a package without installation
To download a package without installing use the apt-get download command followed by the package name.
We’ll download the nmap package to demonstrate.

root@linuxnix:~# apt-get download nmap
Get:1 http://ap-southeast-1.ec2.archive.ubuntu.com/ubuntu xenial/main amd64 nmap amd64 7.01-2ubuntu2 [4,638 kB]
Fetched 4,638 kB in 0s (43.7 MB/s)

This downloads the package in the current working directory.

root@linuxnix:~# ls
nmap_7.01-2ubuntu2_amd64.deb

Note that this does not download the package dependencies.

Example 13: View the changelog for a package
The developers mention changes and updates made to a package throughout its history in the package changelog.
To view the changelog for a package we use the apt-get changelog command followed by the package name.

root@linuxnix:~# apt-get changelog nmap
Get:1 http://changelogs.ubuntu.com nmap 7.01-2ubuntu2 Changelog [27.8 kB]
Fetched 27.8 kB in 0s (35.4 kB/s)
nmap (7.01-2ubuntu2) xenial; urgency=medium
* Revert the last change; no changes left.
-- Matthias Klose <doko@ubuntu.com> Thu, 31 Mar 2016 13:36:38 +0200
nmap (7.01-2ubuntu1) xenial; urgency=medium
* Configure --without-liblua for a first build.
-- Matthias Klose <doko@ubuntu.com> Mon, 22 Feb 2016 16:48:47 +0100

Example 14: Check for broken dependencies
The apt-get check command checks for any dependency issues prevalent in the system.

root@linuxnix:~# apt-get check
Reading package lists... Done
Building dependency tree
Reading state information... Done
root@linuxnix:~#

Example 15: Clean the apt-get cache directory
When we install packages using apt-get, the utility keeps a local copy of the .deb file for the packages being installed in the /var/cache/apt/archives.
The size of this directory could become significant over time and therefore it makes sense to periodically clean this directory.
The apt-get autoclean command performs this task for us.

Given below is a practical demonstration

root@linuxnix:~# apt-get autoclean
Reading package lists... Done
Building dependency tree
Reading state information... Done
root@linuxnix:~#

Conclusion

In this article, we demonstrated the most frequently used features and options of the apt-cache and apt-get utilities.
In our next article, we’ll explain how to use the apt utility which is a combination of the features provided by apt-cache and apt-get.

The post APT-CACHE and APT-GET commands for package management in Ubuntu appeared first on The Linux Juggernaut.

Share Button

Source: The Linux Juggernaut

Leave a Reply