2009年5月2日星期六

Debian Linux新手Apt-get/dpkg简明入门

Debian是最早的Linux发行版之一。它能引起公众注意就因为它让安装与卸载软件非常简单。在其他发行版还陷在依赖性的泥潭里时,Debian用户因为有更好的包管理系统而免受这些困扰。

所有基于Debian的发行都使用这个包管理系统。Deb包可以把一个应用的文件包在一起,大体就如同Windows上的安装文件。这里要介绍的就是如何使用安装包处理软件来达到我们的目的。

用apt-get的第一步就是引入必需的软件库,Debian的软件库也就是所有Debian软件包的集合,它们存在互联网上的一些公共站点上。把它们的 地址加入,apt-get就能搜索到我们想要的软件。/etc/apt/sources.list是存放这些地址列表的配置文件,其格式如下:

deb [web或ftp地址] [发行版名字][main/contrib/non-free]

比如Ubuntu是一个基于Debian的发行,它的Sources.list可以是这样的:

deb http://in.archive.ubuntu.com/ubuntu breezy main restrcted

你可以自己加上自己的地址。apt-get.org 上面有一些不错的地址列表。

设好地址之后,就要把本机上的软件库跟网上的库同步(只是软件描述信息,不包含软件本身)。这样,本机上就有了一个可用的软件的清单,命令如下:

# apt-get update

运行成功之后,你就可以搜到在你的发行版中有哪些软件了,命令是apt-cache,运行这个命令是在本机是检索,而不连到网上。如:

# apt-cache search baseutils

这个命令可以列出baseutils这个软件包的情况,有还是没有,还有版本等。

看到库里有这个软件包后,就可以安装它:

# apt-get install baseutils

这时我们就可以看到apt-get的威力了。如果baseutils依赖于某个另外的软件,或者一个运行库xyz.0.01.so,apt-get会自动 下载这个包(或含有这个库的软件包)。这叫做自动依赖性处理。通常,如果你只用Debian软件库内的软件,是不会发生找不到包或包版本不对的情况的,除 非用的是正在开发的Testing或Unstable版本。

卸载软件:

# apt-get remove baseutils

如果想看一下库里有多少软件:

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

把本机所有软件升级到最新版:

# apt-get upgrade

最后是最强力的--把整个发行版都升到新版本:

# apt-get dist-upgrade

升级时注意那些影响系统起动的东西,比如升级了内核,升级了grub或lilo等,这之后你应当重新运行grub或是lilo,让它们指向正确的位置,否则升级之后会工作不正常的。

用apt-get安装软件时,它会从网上(在sources.list里指的那个站点)下载所用的软件包,这个包将存在本机上,目录是:/var /cache/apt/archives/。时间长了,这里会变得非常巨大,占用大量的硬盘空间。要想清理这个目录,可以运行:

# apt-get clean

还有一个自动清理功能,它只清除那些没用的或者是不完整的软件包:

# apt-get autoclean

这样在重装某软件时就依然能使用已经下载好的,而不是再到网上去下载。

dpkg - 底层一些的包管理工具

用上apt(高级包管理工具)之后,一般是不需要处理单个的deb文件的。如果需要,就要用dpkg命令。比如想自己装gedit:

# dpkg -i gedit-2.12.1.deb

卸载:

# dpkg -r gedit

这里只写名字即可。还可以加上--purge(-P)标志:

# dpkg -P gedit

这会连同gedit的配置文件一起删除,只用-r的话是不删除配置文件的。

如果不想安装一个deb包,但想看一下它里面有什么文件:

# dpkg -c gedit-2.12.1.deb

如果想多看点信息:

# dpkg -I gedit-2.12.1.deb

也可以用通配符来列出机器上的软件:

# dpkg -l gcc*

这会列出所有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)

其中,第1个 i 表示希望安装,第2个 i 表示已经安装,第3个字段是问题(如果有)(这3个字的含义可以看上面那3行,desired, status, err),后面是名字,版本和描述。un就表示,Unknown, not-installed。

如果想看某包是否已经安装:

# dpkg -s gedit

如果想看某软件都有哪些文件,都装到了什么地方:

# dpkg -L gedit

如果只想看其中的某些文件,就加上Grep:

# dpkg -L gedit grep png

当然还有aptitude和Aptitude,前者是终端上运行的带菜单的工具,后者是X窗口上运行的图形化的程序。

还有,列出某包的详情:apt-cache show gedit
一些强制的命令:dpkg --force-help

原文地址: http://linuxhelp.blogspot.com/2005/12/concise-apt-get-dpkg-primer-for-new.html
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
Related Content


没有评论:

发表评论