apt-get Package Manager

apt-get command examples

Managing packages with apt-get


"apt-get" is a command line driven utility for working with the APT (Advanced Package Tool). apt-get works only with package names unlike "dpkg" which works with ".deb" files. apt-get is a very flexible easy to use tool that maintains its own database of packages. This makes it very easy to upgrade packages and handle dependencies. apt-get is mainly used on Debian based systems such as Ubuntu and Mint.

In the following examples, we will cover some of the most frequently used options. The use of the "sudo" command is commonly used in conjunction with apt-get commands as it escalates your privileges on the system allowing you to make changes/update your system. (This of course is only true if you have been granted sudo privileges!) :

Update all package information

Basic Syntax: sudo apt-get update



landoflinux@ubuntu1510:~$ sudo apt-get update
[sudo] password for landoflinux: 
Hit http://security.ubuntu.com wily-security InRelease
Hit http://gb.archive.ubuntu.com wily InRelease  
Hit http://gb.archive.ubuntu.com wily-updates InRelease               
Hit http://security.ubuntu.com wily-security/main Sources              
Hit http://gb.archive.ubuntu.com wily-backports InRelease              
Hit http://security.ubuntu.com wily-security/restricted Sources                
Hit http://gb.archive.ubuntu.com wily/main Sources
Hit http://gb.archive.ubuntu.com wily/restricted Sources      
Hit http://security.ubuntu.com wily-security/universe Sources 
Hit http://gb.archive.ubuntu.com wily/universe Sources             
Hit http://security.ubuntu.com wily-security/multiverse Sources    
Hit http://gb.archive.ubuntu.com wily/multiverse Sources           
Hit http://security.ubuntu.com wily-security/main amd64 Packages   
Hit http://gb.archive.ubuntu.com wily/main amd64 Packages
.............
Reading package lists... Done



The update parameter is used to resynchronize the package index files from their sources. The indexes of available packages are fetched from the locations specified in the file /etc/apt/sources.list. An update should always be performed before any upgrade or dist-upgrade is carried out. The above output is an extract from the "sudo apt-get update" command.


Upgrade All Packages


Basic Syntax: sudo apt-get upgrade



landoflinux@ubuntu1510:~$ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
The following packages have been kept back:
  liboxideqt-qmlplugin liboxideqtcore0 liboxideqtquick0 linux-generic
  linux-headers-generic linux-image-generic oxideqt-codecs-extra
The following packages will be upgraded:
  apport apport-gtk bind9-host binutils chromium-codecs-ffmpeg-extra compiz
  compiz-core compiz-gnome compiz-plugins-default cups-browsed cups-filters
  cups-filters-core-drivers curl dnsutils dpkg dpkg-dev eog firefox
  firefox-locale-en flashplugin-installer fonts-opensymbol gir1.2-rb-3.0
  grub-common grub-pc grub-pc-bin grub2-common ifupdown im-config initscripts
  isc-dhcp-client isc-dhcp-common krb5-locales libavcodec-ffmpeg56
  libavformat-ffmpeg56 libavutil-ffmpeg54 libbind9-90 libc-bin libc-dev-bin
  libc6 libc6-dbg libc6-dev libcompizconfig0 libcupsfilters1 libcurl3
  libcurl3-gnutls libdecoration0 libdns-export100 libdns100 libdpkg-perl
  libfontembed1 libgail-common libgail18 libgcrypt20 libgraphite2-3
  libgssapi-krb5-2 libgtk2.0-0 libgtk2.0-bin libgtk2.0-common libhogweed4
  libirs-export91 libisc-export95 libisc95 libisccc90 libisccfg-export90
  libisccfg90 libk5crypto3 libkrb5-3 libkrb5support0 libldb1
  liblightdm-gobject-1-0 liblwres90 libminiupnpc10 libnautilus-extension1a
  libnettle6 libnm-glib-vpn1 libnm-glib4 libnm-util2 libnm0 libnspr4 libnss3
175 to upgrade, 0 to newly install, 0 to remove and 7 not to upgrade.
Need to get 250 MB of archives.
After this operation, 11.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] 

The command sudo apt-get upgrade is used to install the newest versions of all packages currently installed on your system from the sources listed within /etc/apt/sources.list. Any packages that are currently installed and new packages are available will be retrieved and upgraded. It is important to run the "sudo apt-get update" command first. The above output is an extract of the command being run on an Ubuntu System.


Distribution Upgrade


Basic Syntax: sudo apt-get dist-upgrade



landoflinux@ubuntu1510:~$ sudo apt-get dist-upgrade
[sudo] password for landoflinux: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
The following NEW packages will be installed
  libandroid-properties1 linux-headers-4.2.0-27 linux-headers-4.2.0-27-generic
  linux-image-4.2.0-27-generic linux-image-extra-4.2.0-27-generic
The following packages will be upgraded:
  liboxideqt-qmlplugin liboxideqtcore0 liboxideqtquick0 linux-generic
  linux-headers-generic linux-image-generic oxideqt-codecs-extra
7 to upgrade, 5 to newly install, 0 to remove and 0 not to upgrade.
Need to get 95.2 MB of archives.
After this operation, 293 MB of additional disk space will be used.
Do you want to continue? [Y/n]

The command sudo apt-get dist-upgrade is used to upgrade all packages and manages dependencies arriving from newer versions of packages. This command will attempt to upgrade the most important packages at the expense of less important packages. During this process, some packages may be removed. The above output is an extract of the "sudo apt-get dist-upgrade" command being run.
!! NOTE: only issue the above if you wish to upgrade to a newer version of your distribution.


Install a package


Basic Syntax: sudo apt-get install packagename



landoflinux@ubuntu1510:~$ sudo apt-get install htop
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed
  htop
0 to upgrade, 1 to newly install, 0 to remove and 7 not to upgrade.
Need to get 65.3 kB of archives.
After this operation, 203 kB of additional disk space will be used.
Get:1 http://gb.archive.ubuntu.com/ubuntu/ wily/universe htop amd64 1.0.3-1 [65.3 kB]
Fetched 65.3 kB in 0s (471 kB/s)
Selecting previously unselected package htop.
(Reading database ... 179679 files and directories currently installed.)
Preparing to unpack .../htop_1.0.3-1_amd64.deb ...
Unpacking htop (1.0.3-1) ...
Processing triggers for man-db (2.7.4-1) ...
Processing triggers for gnome-menus (3.13.3-6ubuntu1) ...
Processing triggers for desktop-file-utils (0.22-1ubuntu3) ...
Processing triggers for bamfdaemon (0.5.2~bzr0+15.10.20150627.1-0ubuntu1) ...
Rebuilding /usr/share/applications/bamf-2.index...
Processing triggers for mime-support (3.58ubuntu1) ...
Setting up htop (1.0.3-1) ...

In this example we issued the command "sudo apt-get install htop". Here "htop" is the name of the package to be installed. Just in case you are wondering, "htop" is a great utility for monitoring the health of your system.


Remove a Package


Basic Syntax: sudo apt-get remove packagename



landoflinux@ubuntu1510:~$ sudo apt-get remove htop
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED
  htop
0 to upgrade, 0 to newly install, 1 to remove and 7 not to upgrade.
After this operation, 203 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 179688 files and directories currently installed.)
Removing htop (1.0.3-1) ...
Processing triggers for gnome-menus (3.13.3-6ubuntu1) ...
Processing triggers for desktop-file-utils (0.22-1ubuntu3) ...
Processing triggers for bamfdaemon (0.5.2~bzr0+15.10.20150627.1-0ubuntu1) ...
Rebuilding /usr/share/applications/bamf-2.index...
Processing triggers for mime-support (3.58ubuntu1) ...
Processing triggers for man-db (2.7.4-1) ...

In this example, we issued the "sudo apt-get remove htop" command. This instructed the system to remove the package "htop". Notice you are prompted to confirm your removal of the specified package. It is possible to override this safety check with the option "-y or --yes".


Remove a package and its configuration files


Basic Syntax: sudo apt-get --purge remove packagename



landoflinux@ubuntu1510:~$ sudo apt-get --purge remove htop
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED
  htop*
0 to upgrade, 0 to newly install, 1 to remove and 7 not to upgrade.
After this operation, 203 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 179688 files and directories currently installed.)
Removing htop (1.0.3-1) ...
Purging configuration files for htop (1.0.3-1) ...
Processing triggers for gnome-menus (3.13.3-6ubuntu1) ...
Processing triggers for desktop-file-utils (0.22-1ubuntu3) ...
Processing triggers for bamfdaemon (0.5.2~bzr0+15.10.20150627.1-0ubuntu1) ...
Rebuilding /usr/share/applications/bamf-2.index...
Processing triggers for mime-support (3.58ubuntu1) ...
Processing triggers for man-db (2.7.4-1) ...

In the above we added the "--purge" option which purges any configuration files. We can see this has occurred from the output "Purging configuration files for htop ..."



Free Disk Space - clean


You can use the "clean" parameter to clears out the local repository of retrieved package files. "clean" removes everything but the lock file from /var/cache/apt/archives/ and /var/cache/apt/archives/partial/.



landoflinux@ubuntu1510:~$ sudo apt-get clean

Free Disk Space - autoclean


You can use the "autoclean" parameter to clear out the local repository of retrieved package files. "autoclean" only removes package files that can no longer be downloaded, and are largely useless. This allows a cache to be maintained over a long period without it growing out of control.



landoflinux@ubuntu1510:~$ sudo apt-get autoclean
Reading package lists... Done
Building dependency tree       
Reading state information... Done

Check for Broken Dependencies - check


You can use the "check" parameter to check for any broken packages on your system.



landoflinux@ubuntu1510:~$ sudo apt-get check
Reading package lists... Done
Building dependency tree       
Reading state information... Done

Help with apt-get


If you need further information on the apt-get command, you can issue the "apt-get --help" option. This will show you some of the numerous other options that are available. You can also issue the "man apt-get" for a more detailed overview of commands.



landoflinux@ubuntu1510:~$ apt-get --help
apt 1.0.10.2ubuntu1 for amd64 compiled on Oct  5 2015 15:55:05
Usage: apt-get [options] command
       apt-get [options] install|remove pkg1 [pkg2 ...]
       apt-get [options] source pkg1 [pkg2 ...]

apt-get is a simple command line interface for downloading and
installing packages. The most frequently used commands are update
and install.

Commands:
   update - Retrieve new lists of packages
   upgrade - Perform an upgrade
   install - Install new packages (pkg is libc6 not libc6.deb)
   remove - Remove packages
   autoremove - Remove automatically all unused packages
   purge - Remove packages and config files
   source - Download source archives
   build-dep - Configure build-dependencies for source packages
   dist-upgrade - Distribution upgrade, see apt-get(8)
   dselect-upgrade - Follow dselect selections
   clean - Erase downloaded archive files
   autoclean - Erase old downloaded archive files
   check - Verify that there are no broken dependencies
   changelog - Download and display the changelog for the given package
   download - Download the binary package into the current directory

Options:
  -h  This help text.
  -q  Loggable output - no progress indicator
  -qq No output except for errors
  -d  Download only - do NOT install or unpack archives
  -s  No-act. Perform ordering simulation
  -y  Assume Yes to all queries and do not prompt
  -f  Attempt to correct a system with broken dependencies in place
  -m  Attempt to continue if archives are unlocatable
  -u  Show a list of upgraded packages as well
  -b  Build the source package after fetching it
  -V  Show verbose version numbers
  -c=? Read this configuration file
  -o=? Set an arbitrary configuration option, e.g. -o dir::cache=/tmp
See the apt-get(8), sources.list(5) and apt.conf(5) manual
pages for more information and options.
                       This APT has Super Cow Powers.

Super Cow Powers


If you looked carefully at the above output, you would have noticed a reference to "This APT has Super Cow Powers". Just for fun, try issuing the following command: "sudo apt-get moo"



landoflinux@ubuntu1510:~$ sudo apt-get moo
                 (__) 
                 (oo) 
           /------\/ 
          / |    ||   
         *  /\---/\ 
            ~~   ~~   
..."Have you mooed today?"...