Terminal is your friend - Basic Linux Commands - Part II : Using apt-get and apt-cache


Nitin Venkatesh's Gravatar

Nitin Venkatesh
published Dec. 19, 2011, 4:06 p.m.


In this section we’ll be discussing the different ways in which we can use the apt-get package management tool to install/remove and do some other cool things and the apt-cache tool as well. The apt package management tools are used in Debian and other Debian-based distros like Ubuntu. The examples were executed on a machine running Ubuntu 11.10.

  1. sudo apt-get update

This command updates the sources list from where your packages get downloaded to see if there are any updates, new software offered by the PPA of a certain user that you’ve added to the sources and such. This is equivalent to clicking the check button in the Update Manager.

  1. sudo apt-get upgrade

This command installs any new updates to the already installed software if they are available. This is equivalent to clicking the install updates button in the Update Manager.

  1. sudo apt-get dist-upgrade

This command moves you up to the next release if there is a newer distribution release. For example if you are on Ubuntu 11.10 now and use this command it will upgrade you to Ubuntu 12.04 Alpha 1.

  1. sudo apt-get install <package-name>

This command installs a package along with its dependencies and recommended packages. It usually asks for a confirmation.

    nits@nits-excalibur:~$ sudo apt-get install emacs
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following package was automatically installed and is no longer required:
    libntfs10
    Use 'apt-get autoremove' to remove them.
    The following extra packages will be installed:
    anthy anthy-common emacs23 emacs23-bin-common emacs23-common emacsen-common
    iamerican ienglish-common ispell libanthy0 libm17n-0 libotf0 m17n-contrib
    m17n-db
    Suggested packages:
    emacs23-el spell m17n-docs gawk
    The following NEW packages will be installed:
    anthy anthy-common emacs emacs23 emacs23-bin-common emacs23-common
    emacsen-common iamerican ienglish-common ispell libanthy0 libm17n-0 libotf0
    m17n-contrib m17n-db
    0 upgraded, 15 newly installed, 0 to remove and 0 not upgraded.
    Need to get 32.3 MB of archives.
    After this operation, 104 MB of additional disk space will be used.
    Do you want to continue [Y/n]?

Combining this with -y parameter like sudo apt-get -y install emacs will download and install the package without giving you the prompt, Do you want to continue [Y/n]? . Similarly –assume-no parameter like sudo apt-get –assume-no install emacs will assume no wherever a prompt is present.

To add a repository to your software sources and install packages from there, you can do a sudo add-apt-repository <ppa> && sudo apt-get update && sudo apt-get install <package-name>. For example,

    nits@nits-excalibur:~$ sudo add-apt-repository ppa:clicompanion-devs/clicompanion-nightlies && sudo apt-get update && sudo apt-get install clicompanion
    [sudo] password for nits:
    You are about to add the following PPA to your system:
    clicompanion-nightlies
    This is the nightly ppa for clicompanion.
    Currently this ppa is ok for daily use. Once we get a stable release we will have a clicompanion-stable ppa. Until then we will do our best not to introduce any showstoppers.
    More info: https://launchpad.net/~clicompanion-devs/+archive/clicompanion-nightlies
    Press [ENTER] to continue or ctrl-c to cancel adding it
    Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /tmp/tmp.k3pYH7CoNP --trustdb-name /etc/apt/trustdb.gpg --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyserver hkp://keyserver.ubuntu.com:80/ --recv 1970E5148200CA418348886341FDF8A2B455BEF0
    gpg: requesting key B455BEF0 from hkp server keyserver.ubuntu.com
    gpg: key B455BEF0: public key "Launchpad clicompanion-nightlies" imported
    gpg: Total number processed: 1
    gpg: imported: 1 (RSA: 1)

Different variations with apt-get install:

###### sudo apt-get –no-install-recommends install <package-name>

This command allows you to install the packages without installing the suggested packages.

    nits@nits-excalibur:~$ sudo apt-get --no-install-recommends install javacc
    [sudo] password for nits:
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    Suggested packages:
    javacc-doc
    The following NEW packages will be installed:
    javacc
    0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
    Need to get 284 kB of archives.
    After this operation, 418 kB of additional disk space will be used.

Here you can see that although the javacc-doc package is a suggested package, it doesn’t get downloaded.


###### sudo apt-get –install-suggests install <package-name>

This command downloads the package along with other packages that are suggested for it (recommended packages).

    nits@nits-excalibur:~$ sudo apt-get --install-suggests install javacc
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following extra packages will be installed:
    gcj-4.6-base javacc-doc libgcj-doc
    The following NEW packages will be installed:
    gcj-4.6-base javacc javacc-doc libgcj-doc
    0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
    Need to get 44.9 MB of archives.
    After this operation, 481 MB of additional disk space will be used.
    Do you want to continue [Y/n]?

###### sudo apt-get -d install <package-name>

This command performs a download-only of the packages but doesn’t install them.

    nits@nits-excalibur:~$ sudo apt-get -d install javacc
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following packages were automatically installed and are no longer required:
    fortunes-min fortune-mod librecode0
    Use 'apt-get autoremove' to remove them.
    Suggested packages:
    javacc-doc
    The following NEW packages will be installed:
    javacc
    0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
    Need to get 284 kB of archives.
    After this operation, 418 kB of additional disk space will be used.
    Get:1 http://ubuntu.oss.eznetsols.org/ubuntu/ oneiric/main javacc all 5.0-4 [284 kB]
    Fetched 208 kB in 28s (7,377 B/s)
    Download complete and in download only mode

The downloaded packages can be found at /var/cache/apt/archives with the rest of the apt-get cache.


##### sudo apt-get –fix-broken install

This command downloads and installs any broken dependencies on your system.


##### sudo apt-get –fix-missing install

This command downloads and installs any missing packages on your system.


##### sudo apt-get –ignore-missing install

This command ignores any missing packages and carries on with the downloading of the rest of the packages.


  1. sudo apt-get source <package-name>

This command allows you to download the source package so that you can look into the code.

    nits@nits-excalibur:~$ sudo apt-get source cloudsn
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    Need to get 1,642 kB of source archives.
    Get:1 http://ppa.launchpad.net/chuchiperriman/cloudsn/ubuntu/ oneiric/main cloudsn 0.8.12 (tar) [1,641 kB]
    Get:2 http://ppa.launchpad.net/chuchiperriman/cloudsn/ubuntu/ oneiric/main cloudsn 0.8.12 (dsc) [866 B]
    Fetched 1,642 kB in 1min 33s (17.6 kB/s)
    gpgv: Signature made Thursday 20 October 2011 03:10:16 AM IST using DSA key ID A0BD4CF9
    gpgv: Can't check signature: public key not found
    dpkg-source: warning: failed to verify signature on ./cloudsn_0.8.12.dsc
    dpkg-source: info: extracting cloudsn in cloudsn-0.8.12
    dpkg-source: info: unpacking cloudsn_0.8.12.tar.gz

All the source files required to study the code or rebuild the program gets retrieved and is stored in the same working directory from where the package was downloaded.

    nits@nits-excalibur:~$ ls -la | grep cloud
    drwxr-xr-x 9 root root 4096 2011-10-20 03:10 cloudsn-0.8.12
    -rw-r--r-- 1 root root 866 2011-10-20 03:17 cloudsn_0.8.12.dsc
    -rw-r--r-- 1 root root 1640870 2011-10-20 03:17 cloudsn_0.8.12.tar.gz
  1. sudo apt-get remove <package-name>

This command removes a package from the system. This effectively uninstalls the package although the configuration files remain on the system.

    nits@nits-excalibur:~$ sudo apt-get remove gedit
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following package was automatically installed and is no longer required:
    libntfs10
    Use 'apt-get autoremove' to remove them.
    The following packages will be REMOVED:
    gedit ubuntu-desktop
    0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded.
    After this operation, 2,081 kB disk space will be freed.
    Do you want to continue [Y/n]?
  1. sudo apt-get purge <package-name>

This command is same as the sudo apt-get remove command but also additionally removes the package along with any configuration files of the program.

  1. sudo apt-get autoremove

This command removes the unused dependencies on the system.

    nits@nits-excalibur:~$ sudo apt-get autoremove
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following packages will be REMOVED:
    fortune-mod fortunes-min librecode0
    0 upgraded, 0 newly installed, 3 to remove and 0 not upgraded.
    After this operation, 1,696 kB disk space will be freed.
    Do you want to continue [Y/n]?
  1. sudo apt-get clean

This command cleans out all the cached packages that get downloaded for a package to get installed. Everything in /var/cache/apt/archives/ and /var/cache/apt/archives/partial/ except for the lock file gets emptied

    nits@nits-excalibur:~$ sudo apt-get clean
    [sudo] password for nits:
    nits@nits-excalibur:~$ ls -la /var/cache/apt/archives/partial/
    total 76
    drwxr-xr-x 2 root root 4096 2011-12-19 22:58 .
    drwxr-xr-x 3 root root 69632 2011-12-19 23:38 ..
    nits@nits-excalibur:~$ ls -la /var/cache/apt/archives/
    total 80
    drwxr-xr-x 3 root root 69632 2011-12-19 23:38 .
    drwxr-xr-x 3 root root 4096 2011-12-19 23:28 ..
    -rw-r----- 1 root root 0 2011-10-15 09:26 lock
    drwxr-xr-x 2 root root 4096 2011-12-19 22:58 partial
  1. apt-cache stats

This command provides a short summary/statistics of the cache.

    nits@nits-excalibur:~$ apt-cache stats
    Total package names: 46293 (926 k)
    Total package structures: 46293 (2,222 k)
    Normal packages: 35419
    Pure virtual packages: 493
    Single virtual packages: 3628
    Mixed virtual packages: 541
    Missing: 6212
    Total distinct versions: 37790 (2,419 k)
    Total distinct descriptions: 75548 (1,813 k)
    Total dependencies: 222872 (6,240 k)
    Total ver/file relations: 40377 (646 k)
    Total Desc/File relations: 75548 (1,209 k)
    Total Provides mappings: 6985 (140 k)
    Total globbed strings: 253 (3,019 )
    Total dependency version space: 1,035 k
    Total slack space: 65.3 k
    Total space accounted for: 11.7 M
  1. apt-cache showpkg <package-name>

This command shows information about a package thats present in apt’s cache. This can be used to be view information about a package like its file version number, MD5, its dependencies, reverse dependencies and other such information. This also applies for packages that have not been installed and are present in the repositories (the sources present in sources.list file)

    nits@nits-excalibur:~$ apt-cache showpkg clementine
    Package: clementine
    Versions:
    0.7.1-0ubuntu3 (/var/lib/apt/lists/ubuntu.oss.eznetsols.org_ubuntu_dists_oneiric_universe_binary-i386_Packages) (/var/lib/dpkg/status)
    Description Language: en
    File: /var/lib/apt/lists/ubuntu.oss.eznetsols.org_ubuntu_dists_oneiric_universe_i18n_Translation-en
    MD5: 39f783fb4a919468072f5254101a7ead
    Description Language:
    File: /var/lib/apt/lists/ubuntu.oss.eznetsols.org_ubuntu_dists_oneiric_universe_binary-i386_Packages
    MD5: 39f783fb4a919468072f5254101a7ead
    Reverse Depends:
    Dependencies:
    0.7.1-0ubuntu3 - gstreamer0.10-alsa (16 (null)) gstreamer0.10-audiosink (0 (null)) gstreamer0.10-plugins-base (0 (null)) gstreamer0.10-plugins-good (0 (null)) gstreamer0.10-plugins-ugly (0 (null)) libqt4-sql-sqlite (0 (null)) projectm-data (0 (null)) libc6 (2 2.6) libgcc1 (2 1:4.1.1) libgl1-mesa-glx (16 (null)) libgl1 (0 (null)) libglew1.5 (2 1.5.7.is.1.5.2) libglib2.0-0 (2 2.22.0) libgpod4 (2 0.7.92) libgstreamer0.10-0 (2 0.10.16) libimobiledevice2 (2 0.9.7) libindicate-qt1 (2 0.2.5) liblastfm0 (2 0.4.0~really0.3.3) libmtp9 (2 1.1.0) libplist1 (2 0.13) libqt4-dbus (2 4:4.5.3) libqt4-network (2 4:4.6.1) libqt4-opengl (2 4:4.5.3) libqt4-sql (2 4:4.6.1) libqt4-xml (2 4:4.5.3) libqtcore4 (2 4:4.7.0~beta1) libqtgui4 (2 4:4.6.2) libstdc++6 (2 4.6) libtag1c2a (2 1.6-2~) libx11-6 (0 (null)) zlib1g (2 1:1.1.4)
    Provides:
    0.7.1-0ubuntu3 -
    Reverse Provides:
  1. apt-cache pkgnames –generate

This command generates a list of available packages from all the repositories. The output is way too big to be listed here, so here’s a partial output -

    nits@nits-excalibur:~$ apt-cache pkgnames --generate | head
    pipenightdreams
    mumudvb
    mpg123-alsa
    tbb-examples
    libsvm-java
    libmrpt-hmtslam0.9
    snort-rules-default
    freediams-doc-fr
    davical
    cutmp3
  1. apt-cache depends <package-name>

This command shows a list of dependencies for a given package.

    nits@nits-excalibur:~$ apt-cache depends cloudsn
    cloudsn
    Depends: python
    Depends: python-support
    Depends: python-gtk2
    Depends: python-gconf
    Depends: python-notify
    Depends: python-xdg
    Depends: python-dbus
  1. apt-cache rdpends <package-name>

This command shows a list of reverse dependencies for a given package. That means it shows what other packages depend on the given package.

    nits@nits-excalibur:~$ apt-cache rdepends qt-at-spi
    qt-at-spi
    Reverse Depends:
    ubuntu-desktop
  1. apt-cache search <regex | some-term>

This command searches for the regex (or whatever you specify) in the description and names of packages in the cache and produces results accordingly. This produces the short description of the packages related to the regex.

    nits@nits-excalibur:~$ apt-cache search python | head
    ajaxterm - Web based terminal written in Python
    bittornado - bittorrent client (and tracker) with console and curses interfaces
    bzr - easy to use distributed version control system
    bzr-doc - easy to use distributed version control system (documentation)
    dbus - simple interprocess messaging system (daemon and utilities)
    desktopcouch - Desktop CouchDB instance
    devscripts - scripts to make the life of a Debian Package maintainer easier
    diveintopython - free Python book for experienced programmers
    diveintopython-zh - free Python book for experienced programmers (zh translation)
    doxygen - Documentation system for C, C++, Java, Python and other languages

######apt-cache search –full <regex | some-term>

This produces a full or long description of each of the packages that match the regex present in the cache.

    nits@nits-excalibur:~$ apt-cache search --full python | head
    Package: ajaxterm
    Description-md5: 9e098aee9d80e3cf59e245095e6a70ea
    Description-en: Web based terminal written in Python
    Ajaxterm is a web based terminal written in Python and some AJAX
    javascript for client side. It can use almost any web browser
    and even works through firewalls.

    Package: bittornado
    Description-md5: 369c0fcd3b015c34e46b68ad36098715
    Description-en: bittorrent client (and tracker) with console and curses interfaces
    BitTornado is a bittorrent client built on the original BitTorrent
    client from BitTorrent Inc. This client features a console and curses
    mode, lots of features, and is one of the original bittorrent clients
    created.
    .
    Features include:
    * upload/download speed limitation
    * prioritised downloading when downloading batches (several files)
    * detailed information about connections to other peers
    * encryption (PE/MSE) support (with the recommended python-crypto)
    * console mode for running from scripts
    * curses mode for running interactively
    * tracker for the distribution of files
    .
    This package contains the console and curses interfaces, and a
    bittorrent tracker, install the package bittornado-gui to get the GUI
    components. See the bittorrent package for a description of what
    bittorrent is.

##### apt-cache search –names-only <regex | some-term>

This command only searches for the regex in the packages’ name and not in their description.

    nits@nits-excalibur:~$ apt-cache search --names-only python | head
    bittornado - bittorrent client (and tracker) with console and curses interfaces
    diveintopython - free Python book for experienced programmers
    diveintopython-zh - free Python book for experienced programmers (zh translation)
    gimp - The GNU Image Manipulation Program
    idle-python2.7 - An IDE for Python (v2.7) using Tkinter
    krosspython - Python module for Kross
    libapache2-mod-python - Python-embedding module for Apache 2
    libapache2-mod-python-doc - Python-embedding module for Apache 2 - documentation
    libboost-python-dev - Boost.Python Library development files (default version)
    libboost-python1.46-dev - Boost.Python Library development files
  1. apt-cache unmet

This command shows a summary of all unmet dependencies in the package cache.

    nits@nits-excalibur:~$ apt-cache unmet | head
    Package libgtk-3-0-dbg version 3.2.0-0ubuntu3 has an unmet dep:
    Replaces: libgtk3.0-0-dbg
    Package libgtk-3-0-dbg version 3.2.0-0ubuntu2 has an unmet dep:
    Replaces: libgtk3.0-0-dbg
    Package pioneers-console version 0.12.3-4 has an unmet dep:
    Replaces: pioneers-ai (< 0.12)
    Replaces: pioneers-server-console (< 0.12)
    Package isdnutils-xtools version 1:3.12.20071127-0ubuntu9 has an unmet dep:
    Replaces: isdnutils (< 1:3.1pre1b-0)

Well, that’s that about apt-get and apt-cache. If you feel like adding something extra, please do so in the comments.