ARM & Linux 基础学习 / Ubuntu 下的包管理 / apt工具

编辑整理 by Staok

注:在 Github 上的原版文章日后可能会更新,在其它位置发的不会跟进。文章的 Gitee 仓库地址,Gitee 访问更流畅

Ubuntu 下的包管理 / apt工具

包管理系统的功能和优点大致相同,但打包格式和工具会因平台(不同的 Linux 发行版)而异,如下表所示。

操作系统格式工具
Debian.debapt, apt-cache, apt-get, dpkg
Ubuntu.debapt, apt-cache, apt-get, dpkg
CentOS.rpmyum
Fedora.rpmdnf
FreeBSDPorts, .txzmake, pkg

一般来说 Ubuntu 下很多软件是需要先自行提供源码,使用源码自行编译,编译完成以后使用命令 “install” 来安装到系统中。当然 Ubuntu 下也有其它的软件安装方法,使用得最多的方法就是自行编译源码后进行安装,尤其是嵌入式 Linux 开发。

自行对软件源码编译的一个好处是可以针对不同平台进行编译和部署。

我们利用软件包管理系统可以直接下载并安装所有通过认证的软件,其中 Ubuntu 下我们用的最多的下载工具: APT 下载工具, APT 下载工具可以实现软件自动下载、配置、安装二进制或者源码的功能。 在我们使用 APT 工具下载安装或者更新软件的时候,首先会在下载列表中与本机软件进行对比,看一下需要下载哪些软件,或者升级哪些软件,默认情况下 APT 会下载并安装最新的软件包,被安装的软件包所依赖的其它软件也会被下载安装或者更新,非常智能省心。

包管理工具 apt

Linux apt 命令 | 菜鸟教程 (runoob.com)

# "package" 替换为 包名。
sudo apt-get update                         更新源
sudo apt-get upgrade                        更新已安装的包
sudo apt-get install package                安装包
sudo apt-get remove package                 删除包
​
sudo apt-cache search package               搜索软件包
sudo apt-cache show package                 获取包的相关信息,例如说明、大小、脚本等
​
sudo apt-get install package --reinstall    重新安装包
sudo apt-get -f install                     修复安装
sudo apt-get remove package --purge         删除包,包括配置文件等
sudo apt-get build-dep package              安装相关的编译环境
sudo apt-get dist-upgrade                   升级系统
sudo apt-cache depends package              了解使用该包依赖那些包
sudo apt-cache rdepends package             查看该包被那些包依赖
sudo apt-get source package                 下载该包的源代码
换源 和 添加系统变量

换源

Ubuntu Ports更换成阿里云镜像源-阿里云开发者社区 (aliyun.com)

  1. 首先备份源列表:sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup

  2. 编辑 /etc/apt/sources.list 文件,在文件最前面添加镜像源:

    # 阿里源
    deb https://mirrors.aliyun.com/ubuntu-ports/ xenial main restricted universe multiverse
    deb https://mirrors.aliyun.com/ubuntu-ports/ xenial-updates main restricted universe multiverse
    deb https://mirrors.aliyun.com/ubuntu-ports/ xenial-backports main restricted universe multiverse
    deb https://mirrors.aliyun.com/ubuntu-ports/ xenial-security main restricted universe multiverse
    # deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
    # deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
    # deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
    # deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
    # deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
    # deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
    # deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
    # deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
    # deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
    # deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
    ​
    # 清华源
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
     
    # 中科大源
    deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
    deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
    deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
    deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
    deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
    deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
    deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
    deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
    deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
    deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
    ​
    # 163源
    deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
  3. 更新源:

    sudo apt update
    sudo apt upgrade

添加系统变量

  • 临时:终端中键入 export PATH=$PATH:<目录/要添加的系统变量>,重启后丢失。

  • 永久(只对当前用户有效):修改 ~/.bashrc 文件,在行尾添加 export PATH=$PATH:<目录/要添加的系统变量>,然后终端键入 source ~/.bashrc 使之生效,即可。

Ubuntu 下的卸载包

Ubuntu GUI 界面操作:

  • 使用 Synaptic 软件包管理器进行卸载。系统里面若没有 Synaptic Pack Manager 软件,则在终端安装 sudo apt-get install synaptic具体使用方法

  • 使用 Ubuntu 的软件中心进行卸载。略。

Ubuntu 终端里操作:

  • 列出所有软件包,以可翻页的形式:dpkg --list | less;以搜索特定包名的形式:dpkg --list | grep -n "python";还可以加通配符,很灵活。

  • 卸载程序和所有配置文件:sudo apt-get --purge remove <package-name>

  • 只卸载程序但保留配置文件:sudo apt-get remove <package-name>

  • 删除没用的依赖包:sudo apt-get autoremove <package-name>,加上 --purge 选项就是程序和配置文件都删除。资源不紧张时,此条慎用。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值