BrowseAtWork.com:   [ UP ]   [Manage cookies]
December 13, 2005

A Concise apt-get / dpkg primer for new Debian users

Debian is one of the earliest Linux distribution around. It caught the public's fancy because of the ease of installing and uninstalling applications on it. When many other linux distributions were bogged down in dependency hell, Debian users were shielded from these problems owing to Debian's superior package handling capablities using apt-get.

All Linux distributions which claim their roots in the Debian distribution use this versatile package manager. For the uninitiated, Debian uses the deb package format for bundling together files belonging to an application. You can look at it as something like a setup installer (Eg: Installshield) in windows counterpart.

Here I will explain how to go about using this package handling utility to get the results that you desire.

The first step needed to use apt-get to your advantage is including the necessary repositories. Repositories are merely collections of softwares which are stored in a public location on the internet. By including the web address of these repositories, you are directing apt-get to search these locations for the desired software. You use the /etc/apt/sources.list file to list the addresses of the repositories. It takes the following format:
deb  [web address] [distribution name][maincontribnon-free]
For example, in Ubuntu a debian based distribution, it could be something like this:
deb http://in.archive.ubuntu.com/ubuntu breezy main restrcted
You can add any repository you like. apt-get.org contains an excellent collection of repositories to suite all tastes.

Once you have set the repositories, the next step is to sync the local software database with the database on the repositories. This will cache a copy of the list of all the remotely available softwares to your machine. This is achieved by running the following command:
# apt-get update
An advantage of this is you now have the power to search for a particular program to see if it is available for your version of distribution using the apt-cache command. And you don't need a net connection to do this. For example,
# apt-cache search baseutils
... will tell me if the package baseutils is available in the repository or not by searching the locally cached copy of the database.

Once you have figured that the package (in our case baseutils) is available, then installing it is as simple as running the following command:
# apt-get install baseutils
The real power of apt-get is realised now. If the baseutils package depends on the availability of a version of the library say, "xyz1.5.6.so". Then apt-get will download the library (or package containing the library) from the net and install it before installing baseutils package. This is known as automatic dependency resolution.

And removing a package is as simple as running the command:
# apt-get remove baseutils
Get statistics about the packages available in the repositories by running the command :
# apt-cache stats
Total package names : 22502 (900k)
Normal packages: 17632
Pure virtual packages: 281
Single virtual packages: 1048
Mixed virtual packages: 172
Missing: 3369
...
To upgrade all the softwares on your system to the latest versions, do the following:
# apt-get upgrade
And finally the king of them all - upgrading the whole distribution 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 package is actually cached in a location on your hard disk. It is usually stored in the location /var/cache/apt/archives/ . Over a period of time, all the cached packages will eat up your valuable hard disk space. You can clear the cache and release hard disk space by using the following command:
# apt-get clean
You could also use autoclean where in, only those packages in the cache which are found useless or partially complete are deleted.
# apt-get autoclean
dpkg - The low level Package management utility
As I said earlier, Debian based distributions use the Deb package format. Usually normal users like you and me are shielded from handling individual deb packages. But if you fall into a situation where you have to install a deb package you use the dpkg utility.
Lets assume I have a deb package called gedit-2.12.1.deb and I want to install it on my machine. I do it using the following command:
# dpkg -i gedit-2.12.1.deb
To remove an installed package, run the command:
# dpkg -r gedit
The main thing to note above is I have used only the name of the program and not the version number while removing the software.
You may also use the --purge (-P) flag for removing software.
# dpkg -P gedit
This will remove gedit along with all its configuration files. Where as -r (--remove) does not delete the configuration files.

Now lets say I do not want to actually install a package but want to see the contents of a Deb package. This can be achieved using the -c flag:
# dpkg -c gedit-2.12.1.deb
To get more information about a package like the authors name,the year in which it was compiled and a short description of its use, you use the -I flag:
# dpkg -I gedit-2.12.1.deb
You can even use wild cards to list the packages on your machine. For example, to see all the gcc packages on your machine, do the following:
# dpkg -l gcc*

Desired=Unknown/Install/Remove/Purge/Hold
Status=Not/Installed/Config-files/Unpacked/Failed-config/.
/ Err?=(none)/Hold/Reinst-required/X=both-problems
/ Name Version Description
+++-===============-==============-========================
ii gcc 4.0.1-3 The GNU C compiler
ii gcc-3.3-base 3.3.6-8ubuntu1 The GNU Compiler Colletio
un gcc-3.5 none (no description available)
un gcc-3.5-base none (no description available)
un gcc-3.5-doc none (no description available)
ii gcc-4.0 4.0.1-4ubuntu9 The GNU C compiler
...
In the above listing, the first 'i' denotes desired state which is install. The second 'i' denotes the actual state ie gcc is installed. The third column gives the error problems if any. The fourth, fifth and sixth column gives the name, version and description of the packages respectively. And gcc-3.5 is not installed on my machine. So the status is given as 'un' which is unknown not-installed.

To check if an individual package is installed, you use the status -s flag:
# dpkg -s gedit
Two days back, I installed beagle (a real time search tool based on Mono) on my machine. But I didn't have a clue about the location of the files as well as what files were installed along with beagle. That was when I used the -L option to get a list of all the files installed by the beagle package.
# dpkg -L beagle
Even better, you can combine the above command with grep to get a listing of all the html documentation of beagle.
# dpkg -L beagle | grep html$
These are just a small sample of the options you can use with dpkg utility. To know more about this tool, check its man page.
If you are alergic to excessive command line activities, then you may also use dselect which is a curses based menu driven front-end to the low level dpkg utility.

GUI front-ends for apt-get

43 comments:

Anonymous said...

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

Anonymous said...

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 :-)

Anonymous said...

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.

Anonymous said...

Thanks for this very useful tutorial on apt-get.

Sammy
NY.

Anonymous said...

"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.

Haas said...

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.

Anonymous said...

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?

elcin said...

thank you for usefull paper
ubuntum@gmail.com

Tim McCormack said...

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.

Anonymous said...

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 said...

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!

Anonymous said...

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!

Anonymous said...

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 ;-)

Anonymous said...

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.

Anonymous said...

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.

Anonymous said...

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

You made my day!

Anonymous said...

check this debianhelp website for more help

http://www.debianhelp.co.uk

Anonymous said...

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?

Anonymous said...

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!

Anonymous said...

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.

Anonymous said...

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.

SZERVÁC Attila - sas - said...

Translated to Hungarian

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

:-)

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

Hali: sas ( at 321.hu )

izaac said...

hey! you appeared in debian weekly news congratulations.

BTW, great pragmatic guide

Michael Olberg said...

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 said...

@ 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.

Anonymous said...

Good summary!

Minor point: In this line

# dpkg -L beagle grep html$

you need a 'pipe'.

- Jim Van Zandt

Anonymous said...

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.

Rajiv Vyas said...

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

Anonymous said...

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`".

kingston said...

Hi, I want to quote your site as a reference, but I can't find the trackback, so I use your permalink instead. My blog is blog.ruintower.net, plz feel free to contact me if any problem

Anonymous said...

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 .

Dustin Mays said...

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?

Anonymous said...

@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.

Dustin Mays said...

@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. :)

Dustin Mays said...

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/

Criggie said...

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

Anonymous said...

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

Anonymous said...

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

Anonymous said...

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.

pnguine said...

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?

SponsoredByTheWind said...

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 said...

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.

Richard said...

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

Get Posts via email