Thursday, 9 July 2009

5 Simple APT Tips for Debian and Ubuntu, Part II

Several months ago I created an article with 5 APT (Advanced Packaging Tool) tips for both Debian and Ubuntu available here. APT is the package manager in Debian and Debian-based distributions, like Ubuntu. Here's part two of that article, with 5 more tips and tricks for APT.

Notice: I specified which commands need root privileges. In Debian, type su, followed by your root password, while in Ubuntu just precede each command which needs root privileges with sudo. Notice that by default there is no feedback when entering the password, so you won't be able to see it, but it goes in. Just press Enter when you're done.

Add repositories from a CD or DVD
If you need an offline repository for packages, you can download all the ISO images for your distribution, eventually burn them on CDs/DVDs and add them manually to the /etc/apt/sources.list file using apt-cdrom (as root):

apt-cdrom add

Enter the CD/DVD in the drive and let APT scan for packages.

Another great solution is to not even burn the ISO images to CD, just mount them and add them to your sources.list this way. Here's what you have to do:

First, type:

apt-cdrom -d=/cdrom add

Or replace /cdrom with some empty directory of your choice, that's where the ISO images will be mounted. I use here /mnt/iso0, /mnt/iso1 etc. Just make sure the directory is empty. The output of the above command looks something like this:

# apt-cdrom -d=/cdrom add
Using CD-ROM mount point /cdrom/

Unmounting CD-ROM

Waiting for disc...

Please insert a Disc in the drive and press enter


Now don't press Enter, instead open another terminal session and mount the image in that directory (as root):

mount -o loop /path/to/iso/file.iso /cdrom

Then return to the shell where you started apt-cdrom and press Enter. It should scan the directory where the ISO image was mounted. I'm not very sure this is the only method, but this worked very well for me. If you mount the ISO the first time and then execute apt-cdrom, it first umounts it and then it fails with the message E: Failed to mount the cdrom.

Use dpkg -S to search for a specific file
dpkg -S filename will search for filename in all the packages available and return all the packages which have a file (or the path to that file) with the same name. Notice that the name can also be included in the found files. For example, typing dpkg -S bash will return all the packages which contain the name 'bash' in their files, including mybashburn, bash and several other packages. On the other hand, searching for dpkg -S /bin/bash will only return package bash, since dpkg will match the whole path to the bash binary. See the screenshots below for examples:


Use apt-file to search for files in packages
This is a tool which creates a cache with info about all the packages available in the repositories, and it allows to search for specific files in a package and show what files a package will install (including packages which are not already installed). To get it, type as root:

sudo apt-get install apt-file

Next, update its cache:

sudo apt-file update

Use it like this:

apt-file search filename

Or:

apt-file search file_path

This will also return paths which include the name file_path. To show what files a package will install, use:

apt-file show package_name

Here's a more comprehensive tutorial I wrote a while ago about apt-file.

Remove unnecessary packages
This was a feature which existed for while in aptitude, but not in apt. Once you remove an application, it may be possible that some libraries on which that application depended to remain installed in the system. In order to get rid of those too, use:

apt-get autoremove --purge

Notice that this command will only remove packages which are not needed any more by some other application or that are not marked as 'installed manually'. This means packages which you did not specifically installed yourself.

List files in a package
Just use dpkg -L package_name for this one. Example:

dpkg -L amarok
dpkg -L bash
...

This will list all the packages installed by the respective package. Notice that it won't list anything if the specified package is not installed.

Hope this will be useful. Have some more, or maybe something wrong in the article? Please share other APT-related tips and tricks in the comments below.

3 comments:

Craigevil said...

/usr/share/doc/apt-dpkg-ref/apt-dpkg-ref.html is a great apt/dpkg cheatsheet to have on hand

Grant Wagner said...

Let's not forget my favorate tool, apt-mirror. A simple little config file (which is mostly a copy of sources.list) is all it takes to create complete mirrors of all your favorate repos. I keep an copy of all of mine on my netbook, which in turns keeps all my home machines (where I have no internet) completely updated.

apt-build is also a package which I would like to see brought up to date. It promisses complete bsd ports style compilation on demand, and it sort of works, but it could use an overhaul.

Wiggins said...

Almost...
In my case I am trying to install Emacs22 and xterm onto a Ubuntu 9.04 server with no network connections outside the lab. Neither of those packages are on the install CD.

In this limited case, the ideal solution would be to merge the /var/cache/apt from a configured machine(outside the lab) with the one in the lab. But I haven't found a tool that does the pkgcache.bin merging. Simply copying the file in the Archives wasn't enough.

The next option is the loop mount of the ISOs. I simulated that on a 904s VM and found that 'apt-cdrom' processing threw an I/O error reading the 'dist/..../Packages.gz' file (so did sha1sum and file). This might get tricky if there aren't VirtTTYs on the console driver.

That leaves apt-cdrom with physical media. The tough decision is which CD/DVD to leave in the drive when the rack gets locked and sealed.