Monday, 27 October 2008

5 Simple APT Tricks for Debian and Ubuntu

Here are five simple tricks for APT, the Advanced Packaging Tool used on Debian and Debian-based distributions like Ubuntu.

View all the packages installed on a system

The easiest way to do it is:

dpkg --get-selections

It does not require to be root, and will display all the packages installed via APT. For example, the first lines may look something like:
$ dpkg --get-selections | head
acetoneiso2 install
acidrip install
acpi-support-base install
acpid install
adduser install
akregator install
alien install
alsa-base install
alsa-oss install
alsa-utils install
You can also put the entire list in a text file by redirecting the output, like this:

dpkg --get-selections > installed_packages.txt

Then read this file with a text editor or using less installed_packages.txt.

List files which get installed by a package
-L is a handy parameter to dpkg which will show you what files a package will install.

dpkg -L package_name

For example:

dpkg -L amarok

Will show all the files which are going to be installed by package amarok. You don't have to be root to run it.

Upgrade your system using a one-liner
Type as root:

apt-get update && apt-get dist-upgrade

This command will update your list of packages, and if its return status is successful, it will proceed to the next command, apt-get dist-upgrade, which will upgrade all of your packages and will install new dependencies if needed. Use dist-upgrade when you want all of your packages to be installed, and no package left as being kept back.

Install the dependencies of an application
Sometimes you need to compile from source a newer version of an application which is already included in the repositories. For example, to install the development libraries for BasKet, you would run as root:

apt-get build-dep basket

And then you can proceed to compile your application. Note that sometimes newer versions of applications may depend on newer libraries or other packages than you do not have in the repositories, so this won't always work.

Remove unused package files
When you install software using APT, the DEB packages are kept inside the /var/cache/apt/archives directory. In time, the size of it could get very large. To see what is the size of that directory, you can use:
$ du -h /var/cache/apt/archives/
4.0K /var/cache/apt/archives/partial
956M /var/cache/apt/archives/
In order to clean up all these packages you can use, as root:

apt-get clean

This command will remove all the packages except files which are locked. There is another command, apt-get autoclean, which will remove all the packages which are no longer available in the repository (older versions of packages which can't be downloaded any more).

20 comments:

Unknown said...

It is said in the Debian policy: "don't use apt-get anymore, use aptitude instead"

And most of the time you can use the same commands with apt-get and aptitude

Anonymous said...

Hi Dan Craciun, I have a favour to ask you: can I translate and publish your article on Polish site of Ubuntu? Here is link: http://planet.ubuntu-pl.org/ . Of course under my translation I will put link to your site, as always I mention about source, where I get article.

I hope for your quick and positive answer :)

lukass_s

Craciun Dan said...

Sure you can! Actually, as always, I'm glad when people link to my articles!

Unknown said...

A small correction regarding this command:

dpkg -L package

This will only work for packages you've already installed.

To list files in packages not yet installed, use this:

apt-file list package

Before you can use it the first time, you'll need to install it and update it like this:

sudo apt-get install apt-file
sudo apt-file update

Viktor said...

for me it would be a trick to know how to search for packages using regex, similar way how uses Synaptic PM...
Can I do it? ;)

Anonymous said...

Lets not forget apt-cache search PackageName

Anonymous said...

You can do this as well:

dpkg --get-selections > inst-pkgs.txt

transfer inst-pkgs.txt to the already installed same version of .deb based Linux.

On the destination machine:

cat inst-pkgs.txt | dpkg --set-selections

and then do the following command:

apt-get dselect-upgrade

You have just installed all the packages from the source machine and made the destination machine setup very similarly.

I've done this to move to new machines and keep services and so on exactly the same.

Anonymous said...

Instead of 'dpkg --get-selections', I use 'dpkg -l', it's quicker to type and it gives more output like package version and description.
F.

Anonymous said...

Use apt-get (or equivalent) dist-upgrade with caution ;) especially with the Unstable branch of Debian. Dependency upgrades needed for (a) package(s) do not always get uploaded in sychronism.
Like, e.g. , just this very morning 0900 hrs GMT, the upgrade to kdelibs-data is held back automatically otherwise it would REMOVE what looks like ALL other KDE applications and libraries !

Przemysław Kulczycki said...

You can do all that and much more with wajig - a better alternative to apt-get and aptitude.
Wajig is actually a tool which encompasses apt, dpkg, and others in a simple intuitive CLI and GUI. Using wajig is easier to remember - the only thing you need to remember is "wajig help" which shows you the list of all commands.

Anonymous said...

Just for the record - I revisited the Debian Unstable updates at 11.55 hrs GMT, and the kdelibs package updates are now synchronised again. Thanks as ever to the good work of the Debian developers :-)
NB I was just posting a warning to be careful with dist-upgrade during the "time-window" of the uploads to the repositories.

Don il said...

How do I tell apt I just have changed my mind?
I downloaded Realplayer11gold.deb and I tried to install it using dpkg -i. Apt complained about missing packages. When I tried to install them, it warned me that lpr was going to be removed.
Now I don't want to install RealPlayer. I'd rather keep lpr in place. How do I tell apt to forget about it? It keeps suggesting I issue apt-get -f install to go ahead and install missing packages --and remove lpr--.

Anonymous said...

Try
dpkg --purge Realplayer11gold
(or do you actually have RealPlayer11GOLD ? - it's case sensitive.)

Don il said...

Well, I guess RealPlayer was partially installed. I tried:

dpkg --purge realplay

And it said:
uninstalling realplay ...

Thanks a lot!

Anonymous said...

Nobody says "don't use apt-get". There was a situation when upgrading to etch worked better with aptitude, but now you can savely use apt-get, AFAIK. apt-get also now has all the nice features of aptitude, such as recording which packages are installed automatically and which manually.

Anonymous said...

Btw. you can find a lot of useful dpkg and apt tricks on the Debian reference card ("apt-get install debian-refcard"). It's a PDF cheat sheet in more than twenty languages.

Unknown said...

The post was definitely informative for me. I tried installing Linux 10+ years ago when I was a bumbling teenager and couldn't get it to work.

I'm going to switch to Ubuntu this evening - Linux seems to be a lot more user friendly these days.

Is Ubuntu the best version of Linux for someone who is used to running Windows XP - or is Ubuntu just the most user friendly for a novice user?

Thanks!
Gary

Craciun Dan said...

syntekz
There are people arguing on both sides over which one is more 'appropriate' or more 'user-friendly'. For example some say KDE is more like Windows (and will more likely be easier for a Windows user to switch) while GNOME aims for simplicity. Users have personal opinions and usually will guide you to the distribution/desktop environment they use and like so there is no exact answer as in 'use Ubuntu, it rocks' or 'use Debian, it's stable and rock-solid' or use Fedora and so on. I can recommend you to try Ubuntu and see how it works for you, eventually try other distros too. Ubuntu is famous for detecting and installing drivers for mostly everything Linux supports, but this doesn't mean you have to go with it. Ubuntu also comes with some easy to use graphical tools which will easily guide you to installing audio/video codecs, but this doesn't mean you can't do exactly the same stuff on other distributions.

If you want to experience, I recommend Debian, if you want everything to 'just work' I say go with Ubuntu (that's the only two choices I recommend because they are the only two I worked with). I recommend against using Kubuntu although I'm a KDE fan. Just don't, try SuSE if you liked KDE4 and want it. What you need is to put some effort, no matter what you expect from it. It won't work like Windows, because it is not designed to work like it, that is for sure. Hope it helps you.

Anonymous said...

Dan, multumesc pt aceste "tricks".sunt fan Gnome in ubuntu, dar nu detest nici xfce. pe cand si alte "simple tricks"? Chiar sunt folositoare!
English: Thank you for those "tricks"; I am Gnome fan but like XFCE too. When you will add more "tricks"? The tricks are really useful!

Craciun Dan said...

Sorry for the late answer. Sure I will, as soon as I grab a few more.