03. Installing, removing and updating with Advanced Package Tool apt-get, apt-cache

The Advanced Package Tool (APT) is a suite of commands that allow us to interface with our package management system. With it, we can install new software packages (straight from the web via terminal) and upgrade existing ones. Additionally, the apt-get command pulls in any dependencies necessary, which covers dpkg's drawback. When dealing with installing and updating software, you'll mostly be working with these commands.

Again, make sure you're signed in as root permissions with the sudo -s command.

Let's start with a moo:

# apt-get moo
          / |    ||   
         *  /\---/\ 
            ~~   ~~   
..."Have you mooed today?"...

Actions with apt-get

Here is a list of actions you can use with the apt-get command.

Installs a package.
Removes a package.
Retrieves newest available source package file by name.
Checks for consistency and if there are any broken packages.
Clears out data regarding files from the Debian package database. Run this from time to time to save disk space.
Resynchronizes the package index files from sources. Allows for new and updated information about packages.
Upgrade all packages to the newest version from sources in /etc/apt/sources.list.
Intelligently handles changing dependencies with new version of packages. Avoids dependency breaking.

Examples with apt

Okay, now let's get down to business! Here are 14 ways you can use the apt tool to manage your packages.

1) Refreshing your cache

You may have wondered during the previous lesson how apt-get knows where to look for Vim when we typed apt-get install vim. The answer to this question lies in the system's cache. This is where all the software package names are linked to their location on the Debian packaging server. A list of where important packages can be obtained lies in /etc/apt/sources.list. Since the Debian package management system performs automatic updates via the links in this file, it's advisable to not add any repositories unless you're sure that it may be trusted.

To update the cache on your local system, use the apt-get update command. This will make sure your current listing is up-to-date so you can download the latest packages.

# apt-get update
Ign http://security.ubuntu.com trusty-security InRelease
Ign http://extras.ubuntu.com trusty InRelease
Ign http://us.archive.ubuntu.com trusty/restricted Translation-en_US           
Ign http://us.archive.ubuntu.com trusty/universe Translation-en_US             
Reading package lists... Done

2) Upgrading current softwares

Overtime, there will be new releases of your software, fixing bugs and adding new features. On Windows, you may have had to install and update them individually (darn you annoying Java updates!). On Linux however, you can upgrade all your software in one go:

# apt-get upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
The following packages have been kept back:
  linux-generic-lts-vivid linux-headers-generic-lts-vivid
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.

In the above case, there were no upgrades performed, but from time to time you may see changes. Remember that you must update your package index before running this command!

3) Installing packages

To install a package, use the apt-get install command, followed by the package names. This will install any packages, along with its dependencies.

# apt-get install vim
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  ctags vim-doc vim-scripts
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 3 not upgraded.
Need to get 0 B/956 kB of archives.
After this operation, 2,237 kB of additional disk space will be used.
Selecting previously unselected package vim.
(Reading database ... 166549 files and directories currently installed.)
Preparing to unpack .../vim_2%3a7.4.052-1ubuntu3_amd64.deb ...
Unpacking vim (2:7.4.052-1ubuntu3) ...
Setting up vim (2:7.4.052-1ubuntu3) ...
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode

4) Removing packages

To remove any packages, use apt-get remove. This will remove any particular package, but not its dependencies. It will also keep the configuration files, so you can revert to your normal settings in case you install the particular software again.

# apt-get remove vim
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
0 upgraded, 0 newly installed, 1 to remove and 3 not upgraded.
After this operation, 2,237 kB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 166554 files and directories currently installed.)
Removing vim (2:7.4.052-1ubuntu3) ...
update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/vi (vi) in auto mode
update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/rview (rview) in auto mode

To remove a package along with all its dependencies and config files, use the apt-get purge command. This is a combination of typing apt-get remove and apt-get clean.

5) Multiple packages

For multiple package installation and removals, separate each package name by spaces.

# apt-get install vim-nox emacs24 nanoweb

6) Removing unnecessary dependencies

To remove any packages that were downloaded and are in your cache, you may remove them with the apt-get clean command. This will free up a good amount of previously unused space.

7) Simulating installation

Sometimes you may want to simulate a run just to see what a apt-get install command would do. In this case, you can pass in the simulate option -s.

# apt-get -s install xxxterm
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
   libjavascriptcoregtk-1.0-0 libwebkitgtk-1.0-0 libwebkitgtk-1    | .0-common
The following NEW packages will be installed:
  libjavascriptcoregtk-1.0-0 libwebkitgtk-1.0-0 libwebkitgtk-1    | .0-common
0 upgraded, 4 newly installed, 0 to remove and 3 not upgraded.
Inst libjavascriptcoregtk-1.0-0 (2.4.8-1ubuntu1~ubuntu14.04.1 Ubuntu:14.04/trusty-updates [amd64]) Inst libwebkitgtk-1.0-common (2.4.8-1ubuntu1~ubuntu14.04.1    | Ubuntu:14.04/trusty-updates [all]) Inst libwebkitgtk-1.0-0 (2.4.8-1ubuntu1~ubuntu14.04.1 Ubuntu:14.04/trusty-updates [amd64])
Inst xxxterm (1:1.11.3-1.2 Ubuntu:14.04/trusty [amd64]) 
Conf libjavascriptcoregtk-1.0-0 (2.4.8-1ubuntu1~ubuntu14.04.1  Ubuntu:14.04/trusty-updates [amd64])
Conf libwebkitgtk-1.0-common (2.4.8-1ubuntu1~ubuntu14.04.1 Ubuntu:14.04/trusty-updates [all]) 
Conf libwebkitgtk-1.0-0 (2.4.8-1ubuntu1~ubuntu14.04.1  Ubuntu:14.04/trusty-updates [amd64])
Conf xxxterm (1:1.11.3-1.2 Ubuntu:14.04/trusty [amd64])

Instead of actually performing any actions, you'll see parameters such as Inst and Conf, indicating where the package will be installed and configured.

8) Setting all responses to yes

To set all responses to Yes, so that you don't have to type them per each confirmation prompt, use the -y flag.

# apt-get -y remove vim

9) Download but not install

To download but not install, use the -d flag.

# apt-get -d install vim

10) Suppressing output

To suppress output, use the -qq flag. This will quietly install Vim.

# apt-get install -qq vim

11) Finding a package

The apt-cache command is used to query the package database for information. For example, if you know the name of the package, but aren't sure of the keyword, you can search the package as a query string.

To find a package, first update your cache, the run apt-cache search with your query.

# apt-get update
# apt-cache search convert wma
dir2ogg - audio file converter into ogg-vorbis format kid3 - KDE MP3 ID3 tag editor kid3-cli - Command line audio tag editor kid3-core - Audio tag editor core libraries and data kid3-qt - Audio tag editor nautilus-script-audio-convert - A nautilus audio converter script pacpl - multi-purpose audio converter/ripper/tagger script sorune - tool to manage the database on the Neuros Audio player

12) Viewing package details

You can easily see package details with apt-cache show.

# apt-cache show vimPackage: vim
Priority: optional
Section: editors
Installed-Size: 2185
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian Vim Maintainers <pkg-vim-maintainers@lists.alioth.debian.org>
Architecture: amd64
Version: 2:7.4.052-1ubuntu3
Provides: editor
Depends: vim-common (= 2:7.4.052-1ubuntu3), 
  vim-runtime (= 2:7.4.052-1ubuntu3), libacl1 (>= 2.2.51-8), 
  libc6 (>= 2.15), libgpm2 (>= 1.20.4), libpython2.7 (>= 2.7), 
  libselinux1 (>= 1.32), libtinfo5
Suggests: ctags, vim-doc, vim-scripts
Filename: pool/main/v/vim/vim_7.4.052-1ubuntu3_amd64.deb
Size: 955616
MD5sum: f870bba8885a240acb21977e22503c73
SHA1: 038639fda5e3a73d7f26a8e1bd20faa0282c74ff
SHA256: 1c59553660fb37a9a0317ce7a906b55d580be53e4a478c55a88da4de9f9a86b9
Description-en: Vi IMproved - enhanced vi editor
 Vim is an almost compatible version of the UNIX editor Vi.
 Many new features have been added: multi level undo, syntax
 highlighting, command line history, on-line help, filename
 completion, block operations, folding, Unicode support, etc.
 This package contains a version of vim compiled with a rather
 standard set of features.  This package does not provide a GUI
 version of Vim.  See the other vim-* packages if you need more
 (or less).
Description-md5: 59e8b8f7757db8b53566d5d119872de8
Homepage: http://www.vim.org/
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Origin: Ubuntu
Supported: 5y
Task: ubuntu-usb, cloud-image, server, edubuntu-desktop-gnome, edubuntu-usb

13) View package's policies

To view the current and most recent package versions, use the apt-cache policy command.

# apt-cache policy vim
vim: Installed: 2:7.4.052-1ubuntu3 Candidate: 2:7.4.052-1ubuntu3 Version table: *** 2:7.4.052-1ubuntu3 0 500 http://us.archive.ubuntu.com/ubuntu/ trusty/main amd64 Packages 100 /var/lib/dpkg/status h3 14) View package's dependencies p | To view a package's dependencies, use the code.lang-bash.single.command apt-cache showpkg | command. To view any unmet dependencies, use code.lang-bash.single.command apt-cache unmet | . pre # apt-cache showpkg vim Package: vim Versions: 2:7.4.052-1ubuntu3 (/var/lib/apt/lists/us.archive.ubuntu.com_ubuntu_dists_trusty_main_binary-amd64_Packages) (/var/lib/dpkg/status) Description Language: File: /var/lib/apt/lists/us.archive.ubuntu.com_ubuntu_dists_trusty_main_binary-amd64_Packages MD5: 59e8b8f7757db8b53566d5d119872de8 Description Language: en File: /var/lib/apt/lists/us.archive.ubuntu.com_ubuntu_dists_trusty_main_i18n_Translation-en MD5: 59e8b8f7757db8b53566d5d119872de8 Reverse Depends: vim-syntax-docker,vim mercurial,vim exuberant-ctags,vim byobu,vim vim:i386,vim vimhelp-de,vim 2:7.5 vimhelp-de,vim 2:7.4 vim-vimoutliner,vim vim-syntax-go,vim vim-syntax-docker,vim vim-scripts,vim vim-scripts,vim vim-rails,vim vim-migemo,vim vim-latexsuite,vim vim-latexsuite,vim vim-gocomplete,vim vim-addon-manager,vim supercollider-vim,vim ocaml-tools,vim mercurial,vim libvi-quickfix-perl,vim libtext-vimcolor-perl,vim kde-developer-sdk,vim halibut,vim gworkspace-apps-wrappers,vim edubuntu-desktop,vim debian-reference-common,vim vim-runtime,vim vim-dbg,vim 2:7.4.052-1ubuntu3 vim-common,vim exuberant-ctags,vim byobu,vim Dependencies: 2:7.4.052-1ubuntu3 - vim-common (5 2:7.4.052-1ubuntu3) vim-runtime (5 2:7.4.052-1ubuntu3) libacl1 (2 2.2.51-8) libc6 (2 2.15) libgpm2 (2 1.20.4) libpython2.7 (2 2.7) libselinux1 (2 1.32) libtinfo5 (0 (null)) ctags (0 (null)) vim-doc (0 (null)) vim-scripts (0 (null)) vim:i386 (0 (null)) Provides: 2:7.4.052-1ubuntu3 - editor Reverse Provides: vim-nox 2:7.4.052-1ubuntu3 vim-gtk 2:7.4.052-1ubuntu3 vim-athena 2:7.4.052-1ubuntu3 vim-gnome 2:7.4.052-1ubuntu3

Want to avoid becoming a code monkey?

The Clean Coder

Want to avoid becoming a code monkey? Try Good Practice

This book is packed with practical advice about everything from estimating and coding to refactoring and testing. It covers not only technique, but also attitude, as it shows how to approach software development with honor, self-respect, and pride; communicate and estimate faithfully; and understand that deep knowledge comes with a responsibility to act.

$ Check price
44.9944.99Amazon 4.5 logo(427+ reviews)

More Good Practice resources

Take your Linux skills to the next level!

System Admin Handbook

Take your Linux skills to the next level! Try Linux & UNIX

This book approaches system administration in a practical way and is an invaluable reference for both new administrators and experienced professionals. It details best practices for every facet of system administration, including storage management, network design and administration, email, web hosting, scripting, and much more.

$ Check price
74.9974.99Amazon 4.5 logo(142+ reviews)

More Linux & UNIX resources