できない.dev

apt「E: Could not get lock」の原因と直し方

E: Could not get lock は、ロックを握っている別の apt が終わるのを待てば直る。
相手はメッセージの process 番号で分かる。
apt-get は待たずに失敗するので、-o DPkg::Lock::Timeout で待たせる。

公開: 更新:

症状できない · exit 100
apt-get install -y nano
E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 194 (apt-get)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
解決後できた · exit 0
apt-get -o DPkg::Lock::Timeout=60 install -y nano
…
Unpacking nano (7.2-1+deb12u1) ...
Setting up nano (7.2-1+deb12u1) ...
update-alternatives: using /bin/nano to provide /usr/bin/editor (editor) in auto mode
update-alternatives: using /bin/nano to provide /usr/bin/pico (pico) in auto mode

2026-11-16 に debian:bookworm で実際に打って取った出力。検証環境の詳細

要約

別の apt がパッケージを入れている最中に apt-get install を打つと、次の 2 行で止まり、終了コード 100 で終わる。

E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 133 (apt-get)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?

E: Could not get lock は、パッケージを書き換える権利(ロック)を別の処理が握っているという意味である。
dpkg は同時に 1 つの処理しかパッケージを書き換えられない。
握っている処理が終わるのを待ってから打ち直せば直る。
Debian 12(apt 2.6.1)と Ubuntu 24.04(apt 2.8.3)のコンテナで、どちらも同じメッセージになった。

It is held by process 133 (apt-get) の数字が、ロックを握っている処理の番号である。
apt-get update 同士がぶつかったときは、/var/lib/apt/lists/lock の名前と E: Unable to lock directory /var/lib/apt/lists/ で止まる。
直し方は同じである。

実行例

debian:bookworm で別の apt-get がロックを握っている間に apt-get install を打つと、プロセス 194 が保持していると示されて 終了コード 100 で失敗した。
DPkg::Lock::Timeout=60 を付けると、ロックの解放を待ってインストールが進み、終了コードは 0 になった。

$ apt-get install -y nano
E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 194 (apt-get)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
$ echo $?
100
$ ps -p 194 -o pid,args
  PID COMMAND
  194 apt-get -o Acquire::http::Dl-Limit=8 install -y -q tree
$ apt-get -o DPkg::Lock::Timeout=60 install -y nano
Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 194 (apt-get)...
Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 194 (apt-get)...
Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 194 (apt-get)...
Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 194 (apt-get)...
Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 194 (apt-get)...
Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 194 (apt-get)...
Reading package lists...
Building dependency tree...
Reading state information...
Suggested packages:
  hunspell
The following NEW packages will be installed:
  nano
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 690 kB of archives.
After this operation, 2871 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian bookworm/main amd64 nano amd64 7.2-1+deb12u1 [690 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 690 kB in 0s (5392 kB/s)
Selecting previously unselected package nano.
(Reading database ... 6409 files and directories currently installed.)
Preparing to unpack .../nano_7.2-1+deb12u1_amd64.deb ...
Unpacking nano (7.2-1+deb12u1) ...
Setting up nano (7.2-1+deb12u1) ...
update-alternatives: using /bin/nano to provide /usr/bin/editor (editor) in auto mode
update-alternatives: using /bin/nano to provide /usr/bin/pico (pico) in auto mode
$ echo $?
0

— 2026-11-16 時点の出力

検証環境2026-11-16 検証
検証日
実行環境
debian:bookworm Debian GNU/Linux 12 (bookworm)

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. ロックを握っている処理を確かめて待つ

ps -p 133 -o pid,args

番号はメッセージに出たものに置き換える。
Ubuntu 24.04 では 133 apt-get install -y -q ... curl のように、相手のコマンドがそのまま出た。
その処理が終われば、同じ apt-get install は通る。

/var/lib/dpkg/lock-frontend のファイルを消す必要はない。
握っていた処理が終わったあとはファイルが残ったままでも、apt-get install -y nano は終了コード 0 で通った。

2. スクリプトでは待つ秒数を指定する

sudo apt-get -o DPkg::Lock::Timeout=60 install -y nano

DPkg::Lock::Timeout を付けると、apt-get も Waiting for cache lock: Could not get lock ... を出しながら、指定した秒数までロックが空くのを待つ。
相手が途中で終われば、そのまま install まで進んで終了コード 0 になった。
秒数を過ぎても空かないと、E: Unable to acquire the dpkg frontend lock を出して終了コード 100 で終わる。

3. 自分のスクリプトで並べて実行しない

同じマシンで複数の apt-get install を & などで同時に走らせると、後から始まったものがこのエラーで落ちる。
入れるパッケージは 1 回の apt-get install にまとめて渡す。

sudo apt-get install -y curl nano git

よくある原因

  1. 別の端末やスクリプトで apt が動いている。
    install の途中で打つと、相手がダウンロードしている間でも dpkg のロックはもう握られている。
  2. 裏で更新処理が動いている。
    Ubuntu Server は既定でセキュリティ更新を自動で当てる(Install and manage packages(新しいタブで開く))。
    その更新が apt を動かしている間に打つと、同じメッセージになる。
  3. apt-get を使っている。
    apt は 1.9.11 からロックが空くのを待つようになった(Bug#754103(新しいタブで開く))。
    apt install は Waiting for cache lock を出して待ち続けたが、apt-get install は待たずにすぐ終わった。