[教学] Apt 不止是 apt-get

新手涉及到的教学或入门贴,推荐新手必看,版主维护
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

[教学] Apt 不止是 apt-get

#1

帖子 BigSnake.NET » 2007-12-18 22:45

ubuntu(debian)下使用 apt-get 或 synaptic(新立得) 安装软件是件很轻松的事情. 除了简单的安装软件, apt工具集还有许多特别情况下的用法.

任何时候请记住 man 手册是良师.

1. 使用 aptitude 代替 apt-get .
aptitude 虽然没有 apt-get 的"超级牛力", 但功能却比 apt-get 更强大
首先, 像 apt-get 一样, 可以安装软件

代码: 全选

aptitude install pack-foo
aptitude 解决依赖关系比 apt-get 更智能. 有时用 apt-get 安装软件时会出现 "XXX 需要 XXXX, 但 XXX 已安装" 这类的错误. 这通常是因为某些软件包过新造成的. 此时使用 aptitude, aptitude 会发现问题并给出几个解决方案, 我们只需接受符合心意的方案就可以了.

aptitude 的另外一个优点是会自动记录那些包是"自动安装"(即因依赖关系而安装的), 如果依赖"自动安装"包的包都没被安装, 该"自动安装"包(此时已无用)就会自动删除.
使用这两个命令可以控制包的"自动安装"属性

代码: 全选

aptitude markauto pack-foo #标记成"自动安装"
aptitude unmarkauto pack-foo #标记成"手动安装"
删除软件时使用 markauto 代替 remove, 就可以避免误删重要的包了.
当然, 新版的新立得和 apt-get 也有相似功能, 但貌似不与 aptitude 使用同一个数据库, 所以建议只使用 aptitude 管理软件包.

aptitude 还提供了更灵活的控制软件包版本的方式

代码: 全选

 hold         - 将软件包置于保持状态
 unhold       - 取消对一个软件包的保持命令
 forbid-version - 禁止 aptitude 升级到某一特定版本的软件包。
以前几周的 libcairo 事件作例子:

代码: 全选

sudo aptitude install libcairo2=1.4.10-1ubuntu4 # 强制降级
sudo aptitude forbid-version libcairo2=1.4.10-1ubuntu4.1 # 封掉出错的版本
sudo aptitude markauto libcairo2 #恢复auto属性
参看 man 手册里对 install 操作的描述, 有更多控制方法.

aptitude reinstall 重新安装软件包, 相当于 apt-get --reinstall install
aptitude purge 彻底删除软件包, 相当于 apt-get --purge remove
一个小技巧

代码: 全选

aptitude purge `dpkg -l|grep '^rc'|awk '{print $2}'`
aptitude 还集合了多个 apt 工具的功能

代码: 全选

 search       - 按名称 和/或 表达式搜索软件包
 show       - 显示一个软件包的详细信息
 download     - 下载软件包的 .deb 文件
search 操作有点特别

代码: 全选

aptitude search "foo bar" # 关键词 foo 和 bar 都要出现
aptitude search foo bar # 相当于"或"
不过 aptitude 有个小缺点, 默认会将"推荐"的包当成"依赖"的包处理. 这样设置一下就可以解决问题
在自己和root的家目录下的 .aptitude/config 文件加一句

代码: 全选

aptitude::Recommends-Important "false";
更详细的用法参看 man aptitude.
另外:
1.aptitude 的交互模式貌似很混乱, 不如用新立得.
2.慎用 aptitude install -f .

2. 获得软件包的下载地址
很简单

使用新立得:
1.选中要安装的包
2.文件 -> 生成包下载脚本

使用 apt-get

代码: 全选

apt-get --print-uris install pack-foo
3. 安装齐编译需要的包
首先确定你添加了 deb-src 源, 就像这种

代码: 全选

deb-src http://ubuntu.cn99.com/ubuntu/ gutsy main restricted universe multiverse
并已经 apt-get update
然后就可以

代码: 全选

apt-get build-dep pack-foo
把编译 pack-foo 需要的包安装好

PS: 更强悍的方法是用 apt-build, 不过这样就把 ubuntu 搞的太像 gentoo 了. 有兴趣的朋友可以自己看 apt-build 的 man.

4. 找某个文件在哪个包里
如果是安装了的包, 可以用 dpkg -S, 但此法对未安装的包就无效了.
此时可使用 apt-file

代码: 全选

$ apt-file help
apt-file version 2.0.8.2
(c) 2002 Sebastien J. Gross <sjg@debian.org>


apt-file [options] action [pattern]

Configuration options:
    --sources-list      -s  <file>      sources.list location
    --cache             -c  <dir>       Cache directory
    --architecture      -a  <arch>      Use specific architecture
    --cdrom-mount       -d  <cdrom>     Use specific cdrom mountpoint
    --package-only      -l              Only display packages name
    --fixed-string      -F              Do not expand pattern
    --ignore-case       -i              Ignore case distinctions
    --regexp            -x              pattern is a regular expression
    --verbose           -v              run in verbose mode
    --dummy             -y              run in dummy mode (no action)
    --help              -h              Show this help.
    --version           -V              Show version number

Action:
    update                      Fetch Contents files from apt-sources.
    search|find <pattern>       Search files in packages
    list|show   <pattern>       List files in packages
    purge                       Remove cache files
此工具在寻找编译时缺的文件时很有用, 用前记得先 apt-file update

PS: auto-apt 有相同功能, 而且可以检测进程需要哪些缺少的文件, 自动提示用户选择安装相应的包.

<完>
上次由 BigSnake.NET 在 2007-12-19 12:39,总共编辑 1 次。
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
头像
hcym
帖子: 15634
注册时间: 2007-05-06 2:46

#2

帖子 hcym » 2007-12-18 22:53

很有用
:lol:
头像
mystical
帖子: 170
注册时间: 2007-11-19 19:53

#3

帖子 mystical » 2007-12-19 8:13

谢谢。很不错。
bitman
帖子: 24
注册时间: 2007-12-17 8:42

#4

帖子 bitman » 2007-12-19 8:52

哇哈,学知识啊!! :em66
头像
u194252
帖子: 402
注册时间: 2007-08-04 11:06

#5

帖子 u194252 » 2007-12-19 10:30

牛啊
图片
头像
Amankwah
帖子: 624
注册时间: 2006-10-16 7:24
来自: 安康/西安/广州
联系:

#6

帖子 Amankwah » 2007-12-19 10:42

这个要顶,谢谢了
读书取正,读易取变,读骚取幽,读庄取达,读汉文取坚,最有味卷中岁月。
与菊同野,与梅同疏,与莲同洁,与兰同芳,与海棠同韵,定自称花里神仙。
头像
leeaman
帖子: 30702
注册时间: 2007-02-02 18:14
系统: debian sid

#7

帖子 leeaman » 2007-12-19 11:14

<待续..>
占位等看下一集 :D
醉了星星,醉月亮●●●●●The Long Way To Go(*^_^*)
头像
rhfcaesar
帖子: 2996
注册时间: 2005-11-17 15:45
来自: 湖北

#8

帖子 rhfcaesar » 2007-12-19 12:06

功德无量啊! :D :D 我用aptitude比较多一些。

代码: 全选

枪杆子里面出政权
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#9

帖子 BigSnake.NET » 2007-12-19 12:21

leeaman 写了:
<待续..>
占位等看下一集 :D
其实..貌似没有了..
再把一些地方写得详细点
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
头像
iblicf
帖子: 3766
注册时间: 2007-01-15 17:15

#10

帖子 iblicf » 2007-12-19 12:28

此工具在寻找编译时缺的文件时很有用,
:) what's this mean ? e.g.

代码: 全选

./configrure 
........................

checking for MONO... configure: error: Package requirements (mono >= 1.1) were not met:

No package 'mono' found
...............

代码: 全选

iblicf@ubuntu:~/readings$ dpkg --get-selections |grep '^mono'
mono                                            install
mono-common                                     install
mono-devel                                      install
mono-gac                                        install
mono-gmcs                                       install
mono-jay                                        install
mono-jit                                        install
mono-mcs                                        install
mono-runtime                                    install
mono-utils                                      install
monodoc-gtk2.0-manual                           install
monodoc-manual                                  install

代码: 全选

iblicf@ubuntu:~/readings$ aptitude show mono
Package: mono
State: installed
Automatically installed: yes
Version: 1.2.4-6ubuntu6.1
Priority: optional
.............
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#11

帖子 BigSnake.NET » 2007-12-19 12:41

iblicf 写了:
此工具在寻找编译时缺的文件时很有用,
:) what's this mean ? e.g.

代码: 全选

./configrure 
........................

checking for MONO... configure: error: Package requirements (mono >= 1.1) were not met:

No package 'mono' found
...............

代码: 全选

iblicf@ubuntu:~/readings$ dpkg --get-selections |grep '^mono'
mono                                            install
mono-common                                     install
mono-devel                                      install
mono-gac                                        install
mono-gmcs                                       install
mono-jay                                        install
mono-jit                                        install
mono-mcs                                        install
mono-runtime                                    install
mono-utils                                      install
monodoc-gtk2.0-manual                           install
monodoc-manual                                  install

代码: 全选

iblicf@ubuntu:~/readings$ aptitude show mono
Package: mono
State: installed
Automatically installed: yes
Version: 1.2.4-6ubuntu6.1
Priority: optional
.............
提示的是缺包

代码: 全选

$ aptitude search 'mono dev'
p   libmono-dev                                                                                    - libraries for the Mono JIT - Development files
p   mono-devel                                                                                     - Mono CLI runtime with development tools
p   mono-tools-devel                                                                               - Various development tools for mono
p   monodevelop                                                                                    - C#/Boo/Java/Nemerle/ILasm/ASP.NET Development Environment
p   monodevelop-boo                                                                                - Boo plugin for MonoDevelop
p   monodevelop-java                                                                               - Java plugin for MonoDevelop
p   monodevelop-nunit                                                                              - NUnit plugin for MonoDevelop
p   monodevelop-query                                                                              - MonoQuery plugin for MonoDevelop
p   monodevelop-versioncontrol                                                                     - VersionControl plugin for MonoDevelop
可能是这个: libmono-dev
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
头像
chattan
帖子: 3922
注册时间: 2007-07-11 20:59
联系:

#12

帖子 chattan » 2007-12-19 12:49

学习了!!
头像
aerowolf
帖子: 1778
注册时间: 2006-02-25 17:09
来自: 西安

#13

帖子 aerowolf » 2007-12-19 13:01

感谢分享,收藏了,谢谢。

共享是一种美德,也是开源精神的精髓。

祝你好运。
早起的鸟儿有虫吃
早起的虫儿被鸟吃

http://aerowolf.blog.sohu.com/
亲,点一下免费获得Dropbox空间!
头像
soliton
帖子: 338
注册时间: 2006-05-13 14:00

#14

帖子 soliton » 2007-12-19 13:06

此帖具有超级牛力!
头像
iblicf
帖子: 3766
注册时间: 2007-01-15 17:15

#15

帖子 iblicf » 2007-12-19 13:30

aptitude search 似乎比 apt-cache search 好用 ...

代码: 全选

iblicf@ubuntu:~$ aptitude search "mono dev"
i   libmono-dev                     - libraries for the Mono JIT - Development f
i   mono-devel                      - Mono CLI runtime with development tools   
p   mono-tools-devel                - Various development tools for mono        
p   monodevelop                     - C#/Boo/Java/Nemerle/ILasm/ASP.NET Developm
p   monodevelop-boo                 - Boo plugin for MonoDevelop                
p   monodevelop-java                - Java plugin for MonoDevelop               
p   monodevelop-nunit               - NUnit plugin for MonoDevelop              
p   monodevelop-query               - MonoQuery plugin for MonoDevelop          
p   monodevelop-versioncontrol      - VersionControl plugin for MonoDevelop     
Each search result is listed on a separate line. The first
character of each line indicates the current state of the package:
the most common states are p, meaning that no trace of the package
exists on the system, c, meaning that the package was deleted but
its configuration files remain on the system, i, meaning that the
package is installed, and v, meaning that the package is virtual.
The second character indicates the stored action (if any; otherwise
a blank space is displayed) to be performed on the package, with
the most common actions being i, meaning that the package will be
installed, d, meaning that the package will be deleted, and p,
meaning that the package and its configuration files will be
removed. If the third character is A, the package was automatically
installed.
-------------------------------------------------------------------
每个搜索结果占一行,没行的第一个字符表示包的当前状态,以下是最常见的状态 :
p : 表示软件包尚未安装
c : 表示软件包已经删除,但是系统中还保留着配置文件
i : 表示软件包已经安装
v : 表示软件包是个 virture 包,meta 包
每行第二个字符表示,软件包的<存在状态>? :)
i : 表示软件包将要安装
d :表示将要删除
p : 表示软件包及其配置文件将要删除
如果有第三个字符 A , 表示 package 是自动安装
你们觉得 foo foo-dev lib-foo lib-foo-dev 什么区别?
我理解为 :
foo :bin + some essential lib
foo-dev : somthing "foo*.h" needed
lib-foo: ? extra lib file ?
lib-foo-dev ? if exist ?
上次由 iblicf 在 2007-12-19 14:14,总共编辑 1 次。
回复