A Short Apt-Get / Dpkg Tutorial For New Debian Users

December 13, 2005
This is a short tutorial (Primer) on using Apt-get and Dpkg tools to manage software packages in Debian and, Debian-based Linux distributions such as Ubuntu.

What is Package Management ?


A 'Package Management System' or 'Package Manager' is a collection of tools used to automate the process of installing, updating, configuring, and removing of software packages for a computer's operating system in a consistent manner. Different operating systems have their own package managers. For instance, this is a list of package management tools used by different operating systems. [Courtesy: Wikipedia]

Things you should know


Debian uses the Deb package format for packing together files belonging to an application. You can look at it as something like the Setup installer (Eg: Installshield) in Microsoft Windows.

Software meant to be installed in Debian Linux has the file extension '.deb'.

Debian, (and most other Linux distributions), use a publicly accessible place on the Internet - called a repository - to store all their software packages.

The file /etc/apt/sources.list contain a list of web addresses pointing to the Debian repository.

The web address has the following syntax :

deb  [web address] [distribution name] [options]

An example of a web address pointing to the Ubuntu repository is shown below.

deb  http://in.archive.ubuntu.com/ubuntu  breezy  main restricted

Managing software in Debian


Debian (and related) Linux (distributions) use two important command line tools to manage deb packages. They are -

  1. Dpkg, and
  2. Advanced Packaging Tools or APT

Dpkg - The low level package management tool


Here are a few ways of using dpkg in Debian Linux.

Install a package

dpkg -i <package-name>

Example : Let's say you want to install Gedit text editor in Debian and you have downloaded its deb package from the Gedit website. The package is named gedit-2.12.1.deb. To install it in Debian, open a terminal and move to the directory containing the deb file and execute the command :

# dpkg -i gedit-2.12.1.deb

You can also use --install instead of -i.

Remove a package

To remove an installed package, the syntax is :

dpkg [-r | -P] <program name>

-r - Will remove the program but leave its configuration files intact. You can also use --remove instead of -r.

-P - Will completely remove the package including the configuration files. You can also use --purge instead of -P.

Continuing with the previous example, to remove Gedit (but not its configuration files) from Debian -

# dpkg -r gedit

To remove Gedit completely -

# dpkg -P gedit

Get more information

To see the contents of a deb package (Eg: gedit-2.12.1.deb), use the -c option.

# dpkg -c gedit-2.12.1.deb

To get more information about a deb package such as - the authors name,the year of compilation, a short description of its use, and so on, use the -I option.

# dpkg -I gedit-2.12.1.deb

To check if a program is installed in Debian, use the -s option.

# dpkg -s gedit

The above command also provides other details about the program such as the package maintainer, dependencies, installed version, a small description of the package and so on.

To know where all the files associated with a program are installed, use the -L option.

# dpkg -L gedit

Advanced Packaging Tool (APT)


APT is the Advanced Package Tool and provides the apt-get program. apt-get provides a simple way to retrieve and install packages from multiple sources using the command line.

Unlike dpkg, apt-get does not understand .deb files, it works with the packages proper name and can only install .deb archives from a source specified in '/etc/apt/sources.list'.

apt-get will call dpkg directly after downloading the .deb archives from the configured sources.

Some common ways to using apt-get are shown below :

To update the list of packages known by your system, do the following :

# apt-get update

You should update your package lists regularly by running the above command.


To upgrade all the installed packages on your system -

# apt-get upgrade

To install the gedit package and all its dependencies -

# apt-get install gedit

To remove the gedit package from your system -

# apt-get remove gedit

To remove gedit and also its configuration files -

# apt-get --purge remove gedit

Upgrading Debian to a new version can be done with the command -

# apt-get dist-upgrade

Saving valuable hard disk space

Each time you install an application using apt-get, the corresponding deb package is downloaded and stored in a location on your hard disk - usually in the directory '/var/cache/apt/archives/'.

The following command will remove all the archives and free up hard disk space -

# apt-get clean

To delete only those packages in the cache which are found useless or partially complete, do -

# apt-get autoclean

Another tool in the APT tool suite is apt-cache.

apt-cache tool can be used to search for and find packages, and it supports use of regular expressions.

To find all packages that contain the word "editor", do the following -

$ apt-cache search editor

To print detailed information about a package, the syntax is -

apt-cache show <package name>

To print the packages that a given package depends on, the syntax is -

apt-cache depends <package name>

To print detailed information of the versions available for a package and the packages that reverse-depends on it -

apt-cache showpkg <package name>

To get statistics of the packages available in the repositories -

$ apt-cache stats

I hope you have enjoyed reading this concise tutorial on apt-get and dpkg which will help you manage the packages in Debian Linux.

42 comments:

  • It is my guess that Debian uses "aptitude" for installing packages by default. Especially during installation.

  • OK, this is a dpkg primer for new Debian users, but sooner or later everybody will have to use dpkg --force, especially dpkg --force-overwrite, because dpkg not being able to overwrite a file from one package with another is one of the most common error messages which need manual intervention.

    "dpkg --force-help" is your friend :-)

  • I think aptitude is the recommended option. It has more sophisticated problem-solving options. But personally I find the interface enough to induce psychosis and stick to apt and if I need it synaptic where possible. Out of the box, aptitude also treats recommended packages as dependencies, I think. This means that mixing apt and aptitude together can produce different results and probably isn't a good idea.

    There are a couple of other nice things about apt on Debian, like apt listbugs, so you can check first whether you want to install potentially borked packages on Testing or Unstable. All over, it's very thorough.

    The other half of the story, I think, is the very good packaging of Debian files that lies behind apt. I also run apt on SuSE but I wouldn't feel so confident about doing a big apt-get upgrade there. On Debian I can feel confident that the whole process will run smooth as butter.

  • Thanks for this very useful tutorial on apt-get.

    Sammy
    NY.

  • "the interface enough to induce psychosis"

    You must mean the poor aptitude (G)UI? But you can use aptitude simply from the commandline too:
    aptitude update
    aptitude install
    aptitutude upgrade
    etc.

    In problematic situations, like when running Debian Sid, I've found that sometimes apt-get solves some situation better than aptitude, sometimes it is vice versa, so you may try both if in trouble because they work slightly differently from each other.

  • Good Tutorial, truly helpful. I am using Simply Mepis and apt-get and synaptic are really important. In my experience if you are using Mepis
    # apt-get dist-upgrade
    is not very wise to use. It tends to mess with the install. You can do that if your reps all are "stable". Its not a big problem with Mepis though coz if ur install is gone u need like 15min to reinstall Mepis again.

  • It is always important to have a good understanding of how low level things works (in this case ).
    Some day, in the future, I wish I would see a dpkg/apt-get related article mentioning other fundamental utilities like dpkg-reconfigure, dpkg-divert, dpkg-statoverride and so on...
    Maybe that day people won't think "dpkg --force-*"'s mandatory?

  • thank you for usefull paper
    ubuntum@gmail.com

  • Is there a feature that will tell you what you have installed or uninstalled, and when? In other words, a logging feature? If I install something and all of its dependencies, I'd like to be able to go back at some future date and see what I installed at previous times.

  • There is nice wrapper around apt-get, dpkg, ... : wajig

    apt-get install wajig
    wajig help
    wajig doc

    To install a package :

    wajig install package_name/file/url

    and it calls the apropriate commands to do the job (wget with an url, then apt-get install ..)

  • Featherston

    Great job! You spelled it out well. It took me awhile to get a handle on apt-get but I didn't have something like this to go by.

    Solid!

  • One other useful command that you didnt mention to clean your cached files is

    # apt-get clean --purge

    This will completely remove files from the cache used or not. I do this after a large update and since I am running Debian testing I have to do this about once a week! Have fun!

  • Here's another useful dpkg command. dpkg -S searches for the package that owns the specified file. For example:

    $dpkg -S /bin/mkchk
    grub: /bin/mbchk

    This tells us that the package grub owns the file /bin/mbchk. Another example:

    $ dpkg -S `which xinit`
    xbase-clients: /usr/X11R6/bin/xinit

    Advanced example:

    $ find /bin /usr/bin -type f | xargs dpkg -S | grep 'not found'

    Try to figure out what that last one does ;-)

  • Another useful utility when you have a couple tasks to do is aptsh - it's a shell that you run and then can do things like:

    aptsh> ls emac

    and then hit the tab key, just like in bash, and you'll get a list of all the packages that start with "emac".

    You can use wildcards, and do the installation from within the shell as well.

    It's well worth checking out if you do a lot of package work.

  • I find that ...
    # dpkg-reconfigure packagename
    ...is useful at times. Many a time I have had to use:
    # dpkg-reconfigure xserver-xfree86

    ... to reconfigure X, usually to bump the resolution up from 800x600 to 1024x768.

  • Thanks for this very good tutorial on apt-get. I really was looking for such an article.

    You made my day!

  • check this debianhelp website for more help

    http://www.debianhelp.co.uk

  • apt-get is useful if you know the package name. for example if you use apt-get install thunderbird it fails. It must be given as mozilla-thunderbird. How to get over this kind of problems?

  • If you do not know the exact name You may use apt-cache search underbird, i.e. a (sub-) string, to find all packages containing the string either in package name or description.

    Nice tutorial!

  • There is a high level tool aviable called Wajig.

    Wajig is a python program that is a front end to aptitude, apt-get, apt-cache, and several of the dpkg-* programs.

    If your using a version of Debian that uses Bash shell 3.x and higher, I beleive, you can have more advanced autocompletion results if you enable them. But that's a bit much to go into here.

    The only real solution to finding the name of a package is to search for it..

    apt-cache search proggyname
    or
    wajig search proggyname

    Sometimes it helps to grep through the output or pipe it to less if there is to much output. You can search through less output by hitting the / button.

    So you can go like this:
    apt-cache search browser | less

    then when everything is displayed you can go /firefox and find what you need.. generally.

    Once you find the package name then if your in a xterm then highlight the entry, :q out of less and then go
    apt-get install *middle click* and it should do the copy and paste if everything is right.

    It's a bit complicated, but it's usefull for much more then just looking throug apt-cache output. It works for pretty much any program that does standard output on the command line.

  • apt-cache is your friend!

    # apt-cache search thunderbird

    will give you each package description which contains the word "thunderbird".

    Another command option, "pkgnames", of the apt-cache utility is very usefull too.

    # apt-cache pkgnames | grep thunderbird

    will list each available package whose name matches thunderbird.

  • Translated to Hungarian

    http://321.hu/Debian/apt-guide.html

    :-)

    (with little cosmetics e.g. # --> $ , etc... )

    Hali: sas ( at 321.hu )

  • hey! you appeared in debian weekly news congratulations.

    BTW, great pragmatic guide

  • Your example for listing all packages starting with "gcc" has a flaw: you need to enclose "gcc*" in quotes to prevent the shell from expanding the wild card. You want dpkg to see the asterisk and do the pattern matching.

  • Ravi

    @ michael olberg

    Not really. The * is expanded to denote all characters any number of times which is what is required here.

    Try it on your machine. It works.

  • Good summary!

    Minor point: In this line

    # dpkg -L beagle grep html$

    you need a 'pipe'.

    - Jim Van Zandt

  • Hello, I just wanted to answer to the second comment about dpkg --force. Beware of that, dpkg has little tolerance for anything that breaks its basic assumptions.

    For overwriting files, you might want to use the dpkg-divert command. That way dpkg knows what you want and can continue to update both packages without the risk of overwriting files behind your back.

    Any time you do use dpkg --force, make sure you also report the bug with severity serious (severe policy violation) and put the packages on hold to avoid further breakage until the problem is solved.

  • Unknown

    Any idea how I can get a list of the the packages I have installed using apt-get?

  • after a apt-get upgrade, I like to run deborphan to see what's useless and discardable. then I usually run "apt-get remove --purge `deborphan`".

  • I have a quick run-down of the keys used in aptitude's gui at my website, at http://jobezone.wordpress.com/2006/03/17/aptitude-in-30-seconds/
    . And remember, everything is available from the Top menu, opened with Ctrl+T .

  • I'm currently running Kubuntu Dapper 6.06 at home, without any hope for an internet connection. What would be the easiest way for me to update the apt cache?

  • @Dustin
    You could download the additional packages from the net at another location (cyber cafe??) and burn it into a CD. Then it is a matter of including your CD path in the /etc/apt/sources.list and doing an

    $ sudo apt-get update

    By the way, if you do not have an internet connection, you can/should remove all the links pointing to the external repositories from the sources.list file.

  • @anonymous:

    I know how to download the packages. What I'm looking for is a way to get an updated program list. With a fresh install, I'm stuck with installing only the packages that are available on the install CD. I want the effect of running 'apt-get update' and getting the updated package lists from the repositories, except with me doing the work by hand. Sorry if I worded my previous post confusingly. :)

  • I think I've found a tool that does exactly what I'm looking for. I'll test it when I get home and see how it works and then I'll post back with my results or other questions. Link at the end of the post.

    http://quozl.netrek.org/apt-walkabout/

  • Some other debian utils that help...

    deborphan returns a list of packages that can be safely purged.
    apt-get remove --purge `deborphan`
    is the way to run it.

    Also localepurge saves you disk space every upgrade, by deleting man pages for locales other than your preferred location. On a machine that has been running debian since slink this saved me about 3 Gb of disk

  • Great post. One thing, how could you get a list of everything you've installed?

  • Great post. One thing, how could you get a list of everything you've installed?

  • dpkg -l '*'

    seemed to work for me to list all packages that i have installed. like the commenter above, i had to put the * in quotes or else the shell expanded it for me.

  • Good tut'

    I installed some things with aptitude and then when I want to use apt-get it wants to uninstall them. Is that normal or is my computer confused?

  • Another utility, AptOnCD.

    Very nice primer, thanks.

    There is an app to burn your files on a CD/DVD.
    You can put this CD in your sources.list. Great for situations with limited Internet access.

    The app is called AptOnCD and you can get it at
    http://aptoncd.sourceforge.net.

  • Pankaj S Yadav

    Some commands that can help.
    dpkg -S | --search filename-search-pattern ...
    Search for a filename from installed packages. All standard shell wildchars can be used in the pattern.


    dpkg -p|--print-avail package
    Display details about package, as found in /var/lib/dpkg/available.


    dpkg --update-avail | --merge-avail Packages-file
    Update dpkg's and dselect's idea of which packages are available. With action --merge-avail, old information is combined with information from Packages-file. With action --update-avail, old information is replaced with the information in the Packages-file. The Packages-file distributed with Debian GNU/Linux is simply named Packages. dpkg keeps its record of available packages in /var/lib/dpkg/available.


    dpkg -A | --record-avail package_file ...
    Update dpkg and dselect's idea of which packages are available with information from the package package_file. If --recursive or -R option is specified, package_file must refer to a directory instead.


    dpkg -l | --list package-name-pattern ...
    List packages matching given pattern. If no package-name-pattern is given, list all packages in /var/lib/dpkg/available. Normal shell wildchars are allowed in package-name-pattern. (You will probably have to quote package-name-pattern to prevent the shell from performing filename expansion. For example, dpkg -l 'libc5*' will list all the package names starting with "libc5".)


    dpkg -s | --status package-name ...
    Report status of specified package. This just displays the entry in the installed package status database.

  • Nice! You've got all the basics covered - some of the comments are particularly useful too.