Fix apt "E: Unable to locate package" on Ubuntu 24.04
"E: Unable to locate package" means no apt update, universe disabled, a wrong name, or a package missing from 24.04. Tell them apart with apt-cache policy.
E: Unable to locate package htopThe name is not in apt's package lists. Either the lists were never downloaded (apt update missing), the component that carries the package is disabled, the name is wrong, or this Ubuntu release does not have the package. Rule them out in this order.
[1] Run apt update first. Fresh containers and cloud images start with an empty lists directory, so no name can be found.
# ls /var/lib/apt/lists/# apt-get install -y htopReading package lists...Building dependency tree...Reading state information...E: Unable to locate package htopAfter the lists are downloaded, the same name shows up. In a Dockerfile, put apt-get update && apt-get install -y … in the same RUN line.
# apt-cache policy htop | head -3htop: Installed: (none) Candidate: 3.3.0-4build1[2] Check the name with apt-cache policy. Empty output means apt does not know that name. Names are case-sensitive.
# apt-get install -y HtopReading package lists...Building dependency tree...Reading state information...E: Unable to locate package HtopIf you do not remember the exact name, search names only, as in apt search --names-only "^neo" (without --names-only, descriptions are searched too and the list gets long).
[3] Make sure you did not type a command name. ifconfig is a command; the package is net-tools. To find the package for a command or file path, use apt-file (run apt install apt-file && apt-file update first).
# apt-get install -y ifconfigReading package lists...Building dependency tree...Reading state information...E: Unable to locate package ifconfig# apt-file search --regexp "bin/ifconfig$"net-tools: /sbin/ifconfigPackage python is not available, but is referred to by another package.This may mean that the package is missing, has been obsoleted, oris only available from another sourceHowever the following packages replace it: 2to3 python-is-python3 E: Package 'python' has no installation candidate[4] Check that universe is enabled. A universe package such as neofetch fails with the same error as in [1] when components are cut down to main restricted.
# grep ^Components /etc/apt/sources.list.d/ubuntu.sourcesComponents: main restrictedComponents: main restricted# apt-get install -y neofetchReading package lists...Building dependency tree...Reading state information...E: Unable to locate package neofetchUbuntu 24.04 keeps its repositories in the deb822 file /etc/apt/sources.list.d/ubuntu.sources, and universe and multiverse are enabled by default (Ubuntu Server docs); the default is Components: main universe restricted multiverse. Put universe back on both Components: lines (regular and security), run apt update, and the package is found. Section: universe/utils in apt-cache show tells you which component it is in.
# apt-cache policy neofetch | head -6neofetch: Installed: (none) Candidate: 7.1.0-4 Version table: 7.1.0-4 500 500 http://archive.ubuntu.com/ubuntu noble/universe amd64 Packages[5] Check whether the package exists in this release at all. If every component is enabled, apt update has run, and apt-cache policy still prints nothing, noble simply does not carry that name. fastfetch was such a case in the test.
# apt-cache policy fastfetch; echo "lines=$(apt-cache policy fastfetch | wc -l)"lines=0Search the name on packages.ubuntu.com to see which release first ships it, or use the vendor's official repository or .deb. If adding a vendor repository triggers a key warning, see the legacy keyring warning.
[6] For a downloaded .deb file, prefix it with ./. Without a path, apt treats the file name as a package name.
# apt-get install -y hello_*.debReading package lists...Building dependency tree...Reading state information...E: Unable to locate package hello_2.10-3build1_amd64.debE: Couldn't find any package by glob 'hello_2.10-3build1_amd64.deb'E: Couldn't find any package by regex 'hello_2.10-3build1_amd64.deb'# apt-get install -y ./hello_*.deb 2>&1 | tail -2Unpacking hello (2.10-3build1) ...Setting up hello (2.10-3build1) ...If install stops on a lock error, see apt dpkg lock-frontend; if pip is blocked instead, see pip externally-managed-environment.