【Ubuntu備忘録】パッケージエラー「Try ‘apt –fix-broken install’」で問題が解決しないときの対処法


アイキャッチ画像

MongoDBをインストールしようとしたらなんかこんなエラーが。

apt install mongodb
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:
mongodb : Depends: mongodb-server (>= 1:2.4.1-2) but it is not going to be installed
mongodb-org-database-tools-extra : Conflicts: mongodb but 1:3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5 is to be installed
mongodb-org-mongos : Conflicts: mongodb but 1:3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5 is to be installed
mongodb-org-server : Conflicts: mongodb but 1:3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5 is to be installed
mongodb-org-shell : Conflicts: mongodb but 1:3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5 is to be installed
mongodb-org-tools : Depends: mongodb-database-tools but it is not going to be installed
Conflicts: mongodb but 1:3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5 is to be installed
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).

そこから本体を消そうとしたり他のパッケージの操作ができなくなってしまった。なので記載の通りapt –fix-broken installをやってみるもまたエラー。そんなこと最初からわかってるんですけどね。どうせ解決しないから。こういうのは。

/var/cache/apt/archives# 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:
mongodb-database-tools
The following NEW packages will be installed:
mongodb-database-tools
0 upgraded, 1 newly installed, 0 to remove and 86 not upgraded.
4 not fully installed or removed.
Need to get 52.7 MB of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4/multiverse amd64 mongodb-database-tools amd64 100.3.1 [52.7 MB]
Fetched 52.7 MB in 1s (38.4 MB/s) 
(Reading database ... 197327 files and directories currently installed.)
Preparing to unpack .../mongodb-database-tools_100.3.1_amd64.deb ...
Unpacking mongodb-database-tools (100.3.1) ...
dpkg: error processing archive /var/cache/apt/archives/mongodb-database-tools_100.3.1_amd64.deb (--unpack):
trying to overwrite '/usr/bin/bsondump', which is also in package mongo-tools 3.6.3-0ubuntu1
dpkg-deb: error: paste subprocess was killed by signal (Broken pipe)
Errors were encountered while processing:
/var/cache/apt/archives/mongodb-database-tools_100.3.1_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

なのでエラー丸投げ検索とか色々してみたらStackOverflowにアンサーが。さすが英語圏。因みにQiitaの記事もありましたが(案の定)解決できませんでした。こんなことザラなので手こずってるの場合日本語の検索ヒットは除外しましょう。

解決策

予めrootで入っててください。そして以下を一括にコピペ。これでよし。ぜひご参考にしてください。

rm /etc/apt/sources.list.d/<任意のパッケージ名>.list
apt clean
apt update
apt install -f