0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

aptコマンドの実行時に`Could not get lock`エラーが出てもlockファイルを消すな!!

Last updated at Posted at 2024-10-01

この記事は次のようなエラーについての話です

myuser@ubuntu:~$ sudo apt update && sudo apt upgrade -y
Reading package lists... Done
E: Could not get lock /var/lib/apt/lists/lock. It is held by process 1519 (apt-get)
N: Be aware that removing the lock file is not a solution and may break your system.
E: Unable to lock directory /var/lib/apt/lists/

とか

myuser@ubuntu:~$ sudo apt update && sudo apt upgrade -y
Hit:1 http://ports.ubuntu.com/ubuntu-ports jammy InRelease
Hit:2 http://ports.ubuntu.com/ubuntu-ports jammy-updates InRelease
Hit:3 http://ports.ubuntu.com/ubuntu-ports jammy-backports InRelease
Hit:4 http://ports.ubuntu.com/ubuntu-ports jammy-security InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
34 packages can be upgraded. Run 'apt list --upgradable' to see them.
Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1863 (apt-get)
Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1863 (apt-get)
^Citing for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1863 (apt-get)... 2s

みたいなエラーを見かける時があります。

こんな時、Could not get lockの部分だけGoogleで調べたりすると「lockやlock-frontendを削除すれば直る」という情報と出会うことになります。

しかし、注目すべきはその箇所ではなく・・・

Could not get lock /var/lib/apt/lists/lock. It is held by process 1519 (apt-get)

Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1863 (apt-get)

It is held by process 1519 (apt-get) の部分です!

数字はPID(プロセスID)です。psコマンドにこのIDを渡すことでプロセスの正体を明らかにすることができます。

プロセスを調べてみよう

myuser@ubuntu:~$ ps 1863
    PID TTY      STAT   TIME COMMAND
   1863 ?        S      0:06 /usr/bin/apt-get --option=Dpkg::Options::=--force-confold --option=Dpkg::options::=--force-unsafe-io --assume-yes --quiet install avahi-daemon

何かのインストールをしているようです。しばらく待てば終わりそうなので数分待ってからもう一度psコマンドを実行してみると・・・

myuser@ubuntu:~$ ps 1863
    PID TTY      STAT   TIME COMMAND

無事に処理が終了したみたいですね😊

最初に失敗したaptコマンドを再実行してみよう

myuser@ubuntu:~$ sudo apt upgrade -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following NEW packages will be installed:
  linux-headers-5.15.0-1063-raspi linux-image-5.15.0-1063-raspi linux-modules-5.15.0-1063-raspi linux-raspi-headers-5.15.0-1063
The following packages will be upgraded:
  apparmor ca-certificates cloud-init curl libapparmor1 libcurl3-gnutls libcurl4 libexpat1 libmm-glib0 libpcap0.8 libpython3.10 libpython3.10-minimal libpython3.10-stdlib linux-firmware linux-headers-raspi
  linux-image-raspi linux-raspi python3-configobj python3-pkg-resources python3-setuptools python3.10 python3.10-minimal ubuntu-advantage-tools ubuntu-minimal ubuntu-pro-client ubuntu-pro-client-l10n
  ubuntu-server ubuntu-server-raspi ubuntu-standard vim vim-common vim-runtime vim-tiny xxd
34 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
21 standard LTS security updates
Need to get 392 MB of archives.
After this operation, 339 MB of additional disk space will be used.
Get:1 http://ports.ubuntu.com/ubuntu-ports jammy-updates/main arm64 libexpat1 arm64 2.4.7-1ubuntu0.4 [77.4 kB]
...

今度は問題なく動きました!

まとめ

lockファイル関連のエラーが出ても、焦らずエラーメッセージを読んで適切に対処していきましょう😊

Twitterでは技術関連の内容を日々ツイートしているので、興味があればぜひフォローしていってください🎉

Twitter: https://twitter.com/_matoruru

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?