執行 apt upgrade 時出現 dpkg status database is locked by another process 錯誤

今天要幫樹莓派更新套件時出現了 dpkg: error: dpkg status database is locked by another process 這個錯誤。

大概是有某個 process 正佔據了資源,錯誤的地方是:

Unpacking apt (1.4.10) over (1.4.9) ...
dpkg: error: dpkg status database is locked by another process
E: Sub-process /usr/bin/dpkg returned an error code (2)

我是用 sudo apt upgrade -y 升級,大概是因為要升級 apt 所以出錯了?

解決的方法是:

$ sudo rm -rf /var/lib/dpkg/lock
$ sudo dpkg --configure -a
$ apt --fix-broken install