Things You Need To Know To Become An Apt Guru

aptQuite possibly the most distinguishing feature of Debian-based Linux distributions (such as Ubuntu, Mepis, Knoppix, etc) is their package system – APT. Also known as the Advanced Package Tool, APT was first introduced in Debian 2.1 in 1999.  APT is not so much a specific program as it is a collection of separate, related packages.

With APT, Linux gained the ability to install and manage software packages in a much simpler and more efficient way than was previously possible. Before its introduction, most software had to be installed either by manually compiling the source code, or using individual packages with no automatic dependency handling (such as RPM files).  This could mean hours of  “dependency hell” even to install a fairly trivial program.

In this article, we are going to highlight some of APT’s best features, and share a few of the lesser known features of APT and its cousin dpkg.  Believe it or not, APT can even be used to solve Sodoku puzzles!

dpkg

The base of Debian’s package system is dpkg. It performs all the low level functions of software installation. If you were so inclined, you could use dpkg alone to manage your software. It can install, remove, and provide information on your system’s software collection. Here are some of my favorite features.

Basic installation of local file

Some software authors create Debian packages of their programs, but do not provide a repository for APT to fetch from. In this case they just provide a downloadable .deb file. This is very similar to RPM packages, or even Windows .msi files. It contains all the files and configuration information necessary to install the program. To install a program from a .deb file, you simply need

dpkg -i MyNewProgram.deb

The -i, as you may guess, tells dpkg to install this piece of software.

Listing a package’s contents

You may find yourself, after installing a program, unable to figure out how to run that program.  Sometimes, you need to know where to find the config files for your new game. Dpkg provides an easy way to list all the files that belong to a particular package.

dpkg -L MyNewProgram

Note that case matters.  -L and -l are entirely different options.

Often, a package has so many files it can be difficult to sift through the list to find the one(s) you’re looking for. If that’s the case, we can use grep to filter the results. The following command does the same as above, but only shows results that have “bin” in the path, such as /usr/bin.

dpkg -L MyNewProgram | grep bin

I won’t even begin to go into the awesome power that is grep, but in its simplest form it can be used, like above, to quickly and easily filter a program’s output.

Finding the owner of a file

Occasionally, you find yourself in a situation that’s the reverse of the section above. You have a file, but you don’t know what package it belongs to. Once again, dpkg has you covered.

dpkg -S mysteryfile.cfg

This will tell you which package created/owns that file.

Listing what you’ve got installed

Let’s say you’re about to reinstall your system, and you want to know exactly what you’ve already got installed. You could open up an app like Synaptic and set a filter to show everything marked as “installed”, or you could do it quickly and easily from the command line with dpkg.

dpkg -l

or

dpkg --get-selections

That will give you one big long list of everything you’ve got installed. Advanced users could use these commands to create a text file with all their packages listed, which could be fed into APT later to reinstall everything at once!

Reconfiguring a package

When a .deb package is installed, it goes through a few stages. One of those is the configuration stage, where developers can put a series of actions that take place once all the files have been installed to a proper location. This includes things like start/stopping services, or creating logs, or other such things. Sometimes you need to repeat those steps, without going through the whole reinstallation process.  For that, you use:

dpkg-reconfigure (packagename)

This will redo all the post-install steps needed for that package without forcing you to reinstall. Believe me, this one comes in handy.

Aptitude/Apt-get

There’s some debate and confusion regarding these two tools. Many Linux users have a hard time telling when/why to use one over the other, as they do roughly the same thing.

Short answer: use Aptitude.

Long answer: Both can be used to manage all software installations removals, and both will do a good job. The Debian team officially recommends using Aptitude. It’s not that it’s a LOT better than apt-get, but that it’s a little better, in lots of ways. You can use either one and it will meet your package management needs, but don’t mix and match on the same system. Pick one and stick with it.

Finding the right package

I often find myself in need of software to do a certain thing, but I don’t know the name of any programs to do it. For example, I may need a FLAC player, but don’t know offhand what player will work…

Aptitude:

aptitude search flac

Classic APT:

apt-cache search flac

You’ll get a list of available packages that have “flac” in the name or description.

Preventing a package from updating

On some occasions, I have a version of a package that I want to keep even though there may be upgrades. When it comes to my kernel, for example, I prefer to update manually.

Aptitude:

aptitude hold (packagename)

dpkg:

echo "(packagename) hold" | dpkg --set-selections

Upgrading

Both Aptitude and classic APT provide two methods of upgrading your system: upgrade and dist-upgrade. This is another thing that causes some confusion. An upgrade is an upgrade, right? Well not exactly.

A regular upgrade will read your list of packages, check online for newer versions, and install them as needed. It will NOT, however, perform any upgrades that would require new packages installed, or existing ones removed. This is what dist-upgrade is for. It will get every newer version it finds, even if it involves installing something new (such as a dependency) or removing an existing package (if it’s obsolete or is no longer needed).

Aptitude:

aptitude safe-upgrade
aptitude full-upgrade

Classic APT:

apt-get upgrade
apt-get dist-upgrade

Learn about a package

Finally, some times you just need to know a little about a package. What version is it? Who maintains it? Is it already installed? All these things and more you can find with:

Aptitude:

aptitude show (packagename)

APT:

apt-cache showpkg (packagename)

All of the programs mentioned here are capable of far more than I’ve shown. The tips here should go a long way in helping you use this amazing package system to its full potential.

Also, for those with the patience to read all the way to the end,

apt-get moo

Josh Price is a Linux professional and contributing writer to MakeTechEasier.com

Tags: , , , , ,

 

Enjoyed the article? Subscribe to Make Tech Easier today to get your daily updates of technology tutorials, tips and tricks.

Enter your email:

10 pingbacks/trackbacks (Click to open)

27 Responses

  • bigbr says:

    Thanks mate very useful especially for someone like me that administer many headless ubuntu servers. although am familiar with some of the commands and use them every day. some are new to me and would help me in my day 2 day task. keep up the good work

    Reply

    Joshua Price Reply:

    Thanks. Yeah those developers really took the time to make some good package software, the “dpkg -L” in particular is a real lifesaver for me.

    Reply

  • etu says:

    You say that Debian’s dependencies etc. started to work since APT, 1999. Naturally it worked quite identically from the beginning, five years earlier. APT just made it slightly easier. I still occasionally use dselect to browse packets faster.

    Reply

    Joshua Price Reply:

    My experience with Debian began in about 2001, so I wasn’t privy to the early days, I used Wikipedia as a reference for APT’s beginnings. Thanks for the clarification.

    Reply

  • em4r1z says:

    The commands to upgrade with aptitude are:
    ‘aptitude safe-upgrade’ (installed packages won’t be removed unless unused)
    ‘aptitude full-upgrade’ (removes and installs packages as necessary)

    ‘upgrade’ isn’t a listed parameter and ‘dist-upgrade’ was depreciated in favour of ‘full-upgrade’ (though they’re recognized as synonyms.)

    Reply

    Joshua Price Reply:

    Until quite recently, I had always stuck with apt-get which I know well. My research and tests showed near-perfect option compatibility for the major functions, though it seems it’s not as perfect as I thought. Aptitude is still the new kid on the block for me, always glad to get more info. Thanks for the comment.

    Reply

  • Mark Blakeney says:

    For listing the files in a package I prefer to use “apt-file list package” as it is easier to remember and it can list packages which are not installed.

    Reply

  • Mark Blakeney says:

    To list all files in a package I prefer to use “apt-file list package” as it can list packages which are not installed. Also slightly easier to remember.

    Reply

  • Alan says:

    To see one of the most significant differences between apt-get and aptitude (apart from more robust dependency conflict resolution), run “aptitude” at a prompt with no arguments.

    Reply

  • JOh nDavis says:

    Cool an Apt Guru, just what I always wanted to be!

    RT
    http://www.privacy-center.pro.tc

    Reply

  • John says:

    Great article! Can you post instructions on how to create a text file with all the packages listed and how to then feed that into APT later to reinstall everything at once?

    Thanks

    Reply

    LinuxClassicist Reply:

    to create the text file: dpkg –get-selections >

    to reinstall, using the list: as root, dpkg –set-selections < ; dselect

    Reply

    LinuxClassicist Reply:

    The formatter of this reply area munged my reply.

    The commands are: to create the text file: dpkg dash-dash-get-selections greater-than filename

    to reinstall, using the list in the text file: as root, dpkg dash-dash-set-selections less-than filename semi-colon dselect

    Hope this helps.

    Reply

    The Doctor Reply:

    Fantastic tip for server administration. +1 Internets to you sir.

  • Tony P says:

    dpkg and apt-get are my absolute favorites and what has made me a Debian fan.

    Reply

  • blueyez says:

    this is why i love ubuntu, for apt :D

    Reply

  • John says:

    I thought this is pretty much common knowledge. Why does a post that contains basic apt usage gets on the first page of digg. Doesn’t everyone know this already ?

    Reply

  • Hadret says:

    If you’re upgrading system using Aptitude, you’d rather use those two commands:

    # aptitude safe-upgrade

    or

    # aptitude full-upgrade

    Dist-upgrade in Aptitude is deprecated.

    Reply

  • Bish says:

    The first statement of this article is terribly wrong: the apt package tool is *not* unique to debian-derived distros. To those afraid to try RPM-based distros, don’t worry: apt is there if you want it.

    I’ve never been on a debian-based system, just bad luck, and I’ve been happily using APT for probably a decade. In my case, ‘apt-get build-dep’ is the most helpful thing I could ever have learned.

    Reply

  • ernesto says:

    how do these tools know if a package is “no longer used”? i.e. in a dist-upgrade, which packages get deleted?

    and: what is dselect good for?

    btw: recently i have updated debian etch to lenny, and i had answer about 100 times the question “config file has local modifications”. very annoying.

    Reply

  • Jason says:

    Forget aptitude. Use wajig. It’s a python-based CLI front-end to all of the packaging tools. It’s great.

    Reply

    JoshuaRL Reply:

    @Jason

    Just for my own wonderment, what’s so good about wajig? I installed it and played with it a bit, but so far this was the coolest part:

    “wajig remove wajig”

    Heehee. And it worked too!

    Reply

  • steaven says:

    this is why i love ubuntu, for apt.

    Reply

  • Jonathan says:

    When you dpkg -i package.deb and it tells you that there are missing dependencies there is a very simple fix for this problem as well.

    enter apt-get install -f

    This will install the dependencies you were missing (assuming they are available in the repos) and then automatically continue with your failed dpkg install.

    Reply

    Joshua Price Reply:

    Great tip, Jonathan, thanks!

    Reply

  • Ben says:

    See the following Google tech talk:
    “Anatomy of a Debian package”
    http://video.google.com/videoplay?docid=-6726522426109060914

    for more information about the .deb format. The .deb file itself is an ‘ar’ archive.

    Reply

  • Kadimi says:

    Who said dist-upgarde was deprecated, it’s may be in future version…

    me@computer:~$ man aptitude
    [...]
    full-upgrade
    Upgrades installed packages to their most recent
    version, removing or installing packages as necessary.
    This command is less conservative than safe-upgrade and
    thus more likely to perform unwanted actions. However,
    it is capable of upgrading packages that safe-upgrade
    cannot upgrade.

    Note
    This command was originally named dist-upgrade for
    historical reasons, and aptitude still recognizes
    dist-upgrade as a synonym for full-upgrade.
    [...]

    Reply

Leave a Comment




 

Sign in with Twitter

Comment

Please Note: Comments maybe under moderation after you submit your comments so there is no need to resubmit your comment again