Feb. 27, 2009, 2:18 p.m.
posted by void
Search for Packages from the Command Line
When you aren't exactly sure which Ubuntu package has the files you need, use these handy command-line tools to find out. The Ubuntu package-management system is truly impressive. Simply choose a package name, and Ubuntu will automatically download and install it along with any dependencies it may have. What happens, though, when you don't know the exact name of the package you want to install? The graphical package manager Synaptic has a search tool, but there are two separate command-line tools, apt-cache and apt-file, that you'll find are even more powerful. Each tool suits a particular type of search. apt-cache lets you search for a package based on keywords (among some of its other uses), and apt-file allows you to search for a package based on files inside that package. Each approach is useful in different circumstances, and this hack introduces you to how to use both tools effectively. Search for Packages Based on KeywordsThe apt-cache tool is actually much more than a keyword-based package search utility. apt-cache is a general tool used to query and manipulate the apt package cache. To search within the apt package cache, use the search argument followed by one or more keywords. For instance, if you wanted to find packages having to do with Adobe's Acrobat Reader, you could run the following search: $ apt-cache search acrobat reader xpdf - Portable Document Format (PDF) suite xpdf-utils - Portable Document Format (PDF) suite -- utilities acroread - Adobe Acrobat Reader: Portable Document Format file viewer acroread-debian-files - Debian specific parts of Adobe Acrobat Reader acroread-plugins - Plugins for Adobe Acrobat(R) Reader mozilla-acroread - Adobe Acrobat(R) Reader plugin for mozilla / konqueror xpdf-chinese-simplified - Portable Document Format (PDF) suite -- simplified Chinese language support xpdf-chinese-traditional - Portable Document Format (PDF) suite -- traditional Chinese language support xpdf-japanese - Portable Document Format (PDF) suite -- Japanese language support xpdf-korean - Portable Document Format (PDF) suite -- Korean language support The resulting packages listed in the output either directly relate to Acrobat Reader or indirectly relate to the PDF format. Note that because this is a keyword-based search, keywords will sometimes turn up packages that are only marginally related to what you are looking for. Use multiple keywords to narrow things down. Alternatively, you can pipe the results through grep to look for particular keywords in the package names themselves. For instance, if you knew the package you were looking for contained the word acro, you could type: $ apt-cache search acrobat reader | grep acro acroread - Adobe Acrobat Reader: Portable Document Format file viewer acroread-debian-files - Debian specific parts of Adobe Acrobat Reader acroread-plugins - Plugins for Adobe Acrobat(R) Reader mozilla-acroread - Adobe Acrobat(R) Reader plugin for mozilla / konqueror Search for Packages Based on Files They ContainThere are times when you wish to have a particular file, and you want to track down which package installs this file. Or perhaps you know that you need a particular library for a program, but you don't know the exact name of the package that contains it. While you could use apt-cache and guess, Ubuntu offers a more precise approach with the apt-file tool. apt-file lets you specify a particular file or directory name, and it will search through a package database and print out the name of a package that contains that file or directory, along with the complete path to it. By default, Ubuntu does not include the apt-file tool. To install it, you will need to make sure that you include the universe repository [Hack #60] in your list of package repositories. Then choose "apt-file" from the Base System (universe) section if you use Synaptic [Hack #55]; otherwise, open a terminal and type: $ sudo apt-get install apt-file
Unlike apt-cache, apt-file does not use the included apt package cache for its searches. Instead it maintains its own database that you need to update as you do with apt-get update. Before you use apt-file for the first time, you will need to do a full update: $ sudo apt-file update
Depending on your Internet connection, this might take some time, since apt-file needs to download all of the repository lists you currently have configured. Every once in a while, you will want to update this repository list, especially if you intend to do a number of searches and want newer packages to show up in the results. After the repository list is up-to-date, you can search for packages much as you do with apt-cache. For instance, to find out what package contains the configuration file /etc/jvm, type: $ apt-file search /etc/jvm java-common: etc/jvm Notice that the results not only tell you which package contains this file (java-common), but also give you the full path to that file. This can be useful when more than one package provides a file with the same name but in different directories. With the full path displayed, you can more easily figure out which package you were looking for. |
- Comment