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

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

```
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