What the message looks like
$ sudo apt install htop
Reading package lists... Done
Building dependency tree... Done
E: Unable to locate package htopCommon causes
apt updatewas never run, for example on a fresh server or in a new container, so the package lists are empty.- The package name is wrong, or the package has another name on your release.
- The repository component that carries the package is not enabled:
universeon Ubuntu,contribornon-freeon Debian. - The release is end of life and its repositories moved to an archive server, so
apt updateends with 404 errors. - A broken or typed-wrong entry in the sources makes
apt updatefail, so the lists stay incomplete.
Check
- Does apt know the package at all?
apt-cache policy htop - Search for the real package name
apt-cache search htop | head -n 10 - Which repositories and components are active?
grep -rE '^(deb |Types:|URIs:|Suites:|Components:)' /etc/apt/sources.list /etc/apt/sources.list.d/ 2>/dev/null - Which release is this?
cat /etc/os-release
Fix
- Refresh the lists and read the output for errors:
sudo apt update. - Use the exact package name that
apt-cache searchshows for your release. - Enable the missing component: on Ubuntu
sudo add-apt-repository universe, on Debian addcontribornon-freeto theComponentsof your sources, then runapt updateagain. - If
apt updateshows 404 for your release, it is end of life. Upgrade to a supported release instead of pointing the sources to an archive for production. - In a Dockerfile, run
apt-get updateandapt-get installin the sameRUNstep.
FAQ
Why does it work on my laptop but not in the container?
Container images ship without package lists to stay small. Run apt-get update first, in the same build step as the install.
Is it safe to add third-party repositories to fix this?
Only if you trust the source. First check whether the package exists under another name or in an official component of your release.