命令 apt --fix-broken install 主要用于修复因为依赖问题或损坏的软件包导致的未完成安装或卸载问题。这是一个非常有用的工具,可以帮助自动解决和修复在使用 Debian 及其派生系统(如 Ubuntu)的软件包管理过程中出现的问题。
该命令的具体作用包括:
-
修复损坏的依赖关系:
如果一个软件包因为某些原因没有正确安装,可能会留下未满足的依赖。这种情况下,系统的包管理状态可能会变得不一致。使用apt --fix-broken install可以尝试自动下载和安装缺失的依赖,并重新配置所有受影响的包以达到一个正常的、一致的状态。 -
完成未完成的安装:
在软件安装过程中如果遇到中断(比如电力中断、某些依赖无法获取等情况),可能会留下未完全配置的包。这个命令可以完成这些包的配置过程。 -
清理不需要的包:
在解决依赖关系时,apt --fix-broken install也会尝试清理那些已经不再需要的包,特别是那些作为依赖而安装但现在主依赖已经被移除的软件包
真实案例
root@ubuntu:/home/oneslide/middleware/mysql# ls
libmysqlclient21_8.0.11-1ubuntu18.04_amd64.deb mysql-community-server_8.0.11-1ubuntu18.04_amd64.deb
libmysqlclient-dev_8.0.11-1ubuntu18.04_amd64.deb mysql-community-server-core_8.0.11-1ubuntu18.04_amd64.deb
//...
# 安装mysql deb出现了缺少依赖报错
root@ubuntu:/home/oneslide/middleware/mysql# dpkg -i *.deb
Selecting previously unselected package libmysqlclient21:amd64.
(Reading database ... 73234 files and directories currently installed.)
Preparing to unpack libmysqlclient21_8.0.11-1ubuntu18.04_amd64.deb ...
Unpacking libmysqlclient21:amd64 (8.0.11-1ubuntu18.04) ...
Selecting previously unselected package libmysqlclient-dev.
Preparing to unpack libmysqlclient-dev_8.0.11-1ubuntu18.04_amd64.deb ...
Unpacking libmysqlclient-dev (8.0.11-1ubuntu18.04) ...
Preparing to unpack mysql-apt-config_0.8.29-1_all.deb ...
dpkg: dependency problems prevent configuration of mysql-community-server:
mysql-community-server depends on perl; however:
Package perl is not installed.
mysql-community-server depends on libaio1 (>= 0.3.93); however:
Package libaio1 is not installed.
mysql-community-server depends on libmecab2; however:
Package libmecab2 is not installed.
在安装mysql的过程中,我发现mysql的安装包因为系统缺少python、perl等依赖而无法安装成功。
dpkg: dependency problems prevent configuration of mysql-community-server:
mysql-community-server depends on perl; however:
Package perl is not installed.
mysql-community-server depends on libaio1 (>= 0.3.93); however:
Package libaio1 is not installed.
mysql-community-server depends on libmecab2; however:
Package libmecab2 is not installed.
dpkg: error processing package mysql-community-server (--install):
dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of mysql-community-server-core:
mysql-community-server-core depends on libaio1 (>= 0.3.93); however:
Package libaio1 is not installed.
mysql-community-server-core depends on libmecab2; however:
Package libmecab2 is not installed.
之后每次安装软件都会报错:
root@ubuntu:/home/oneslide/middleware/mysql# apt -y install vim
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt --fix-broken install' to correct these.
The following packages have unmet dependencies:
mysql-community-server : Depends: perl but it is not going to be installed
Depends: libaio1 (>= 0.3.93) but it is not going to be installed
Depends: libmecab2 but it is not going to be installed
mysql-community-server-core : Depends: libaio1 (>= 0.3.93) but it is not going to be installed
Depends: libmecab2 but it is not going to be installed
Recommends: mecab-ipadic-utf8 but it is not going to be installed
mysql-community-test : Depends: python but it is not going to be installed
Depends: libjson-perl but it is not going to be installed
Depends: mecab-ipadic-utf8 but it is not going to be installed
vim : Depends: vim-common (= 2:8.0.1453-1ubuntu1.13) but 2:8.0.1453-1ubuntu1.3 is to be installed
Depends: vim-runtime (= 2:8.0.1453-1ubuntu1.13) but it is not going to be installed
Depends: libgpm2 (>= 1.20.7) but it is not going to be installed
Depends: libpython3.6 (>= 3.6.5) but it is not going to be installed
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).
此时如果运行apt --fix-broken install就会将缺失的依赖包全部自动安装上:
root@ubuntu:/home/oneslide/middleware/mysql# apt --fix-broken install
Reading package lists... Done
Building dependency tree
Reading state information... Done
Correcting dependencies... Done
The following additional packages will be installed:
libaio1 libcommon-sense-perl libgdbm-compat4 libjson-perl libjson-xs-perl libmecab2 libperl5.26 libpython-stdlib libpython2.7-minimal
libpython2.7-stdlib libtypes-serialiser-perl mecab-ipadic mecab-ipadic-utf8 mecab-utils perl perl-base perl-modules-5.26 python python-minimal
python2.7 python2.7-minimal
Suggested packages:
perl-doc libterm-readline-gnu-perl | libterm-readline-perl-perl make python-doc python-tk python2.7-doc binutils binfmt-support
The following NEW packages will be installed:
libaio1 libcommon-sense-perl libgdbm-compat4 libjson-perl libjson-xs-perl libmecab2 libperl5.26 libpython-stdlib libpython2.7-minimal
libpython2.7-stdlib libtypes-serialiser-perl mecab-ipadic mecab-ipadic-utf8 mecab-utils perl perl-modules-5.26 python python-minimal python2.7
python2.7-minimal
The following packages will be upgraded:
perl-base
1 upgraded, 20 newly installed, 0 to remove and 160 not upgraded.
5 not fully installed or removed.
Need to get 24.4 MB of archives.
After this operation, 115 MB of additional disk space will be used.
# 这里安装了缺失的依赖包
Selecting previously unselected package libpython2.7-minimal:amd64.
(Reading database ... 89949 files and directories currently installed.)
Preparing to unpack .../0-libpython2.7-minimal_2.7.17-1~18.04ubuntu1.11_amd64.deb ...
Unpacking libpython2.7-minimal:amd64 (2.7.17-1~18.04ubuntu1.11) ...
Selecting previously unselected package python2.7-minimal.
Preparing to unpack .../1-python2.7-minimal_2.7.17-1~18.04ubuntu1.11_amd64.deb ...
Unpacking python2.7-minimal (2.7.17-1~18.04ubuntu1.11) ...
Selecting previously unselected package python-minimal.
Preparing to unpack .../2-python-minimal_2.7.15~rc1-1_amd64.deb ...
Compiling IPA dictionary for Mecab. This takes long time...
reading /usr/share/mecab/dic/ipadic/unk.def ... 40
emitting double-array: 100% |###########################################|
/usr/share/mecab/dic/ipadic/model.def is not found. skipped.
update-alternatives: using /var/lib/mecab/dic/ipadic-utf8 to provide /var/lib/mecab/dic/debian (mecab-dictionary) in auto mode
Setting up python (2.7.15~rc1-1) ...
Setting up perl (5.26.1-6ubuntu0.7) ...
Setting up libjson-perl (2.97001-1) ...
Setting up mysql-community-server (8.0.11-1ubuntu18.04) ...
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
Setting up libcommon-sense-perl (3.74-2build2) ...
Setting up mysql-server (8.0.11-1ubuntu18.04) ...
Setting up libtypes-serialiser-perl (1.0-1) ...
Setting up mysql-community-test (8.0.11-1ubuntu18.04) ...
Setting up mysql-testsuite (8.0.11-1ubuntu18.04) ...
Setting up libjson-xs-perl (3.040-1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for mime-support (3.60ubuntu1) ...
Processing triggers for libc-bin (2.27-3ubuntu1.2) ...
Processing triggers for ureadahead (0.100.0-21) ...
Processing triggers for systemd (237-3ubuntu10.42) ...