This error occurs when apt-get cannot find a package in its cache during Docker image builds. The most common cause is missing apt-get update before installation commands.
The "Unable to locate package" error appears when Docker's apt package manager cannot find a requested package in its local package index. This happens because Docker base images ship with minimal or outdated package lists to keep image sizes small. When you try to install a package without first updating the package index, apt has no knowledge of available packages and their locations in Ubuntu's repositories. This is a Docker-specific manifestation of a standard Linux package management issue, but it's more common in containers because each layer is isolated and package indexes aren't automatically refreshed like they might be on a traditional server.
This is the most common fix. Always run apt-get update immediately before apt-get install in the same RUN command:
FROM ubuntu:22.04
# ❌ WRONG - Separate commands (update gets cached)
RUN apt-get update
RUN apt-get install -y nodejs
# ✅ CORRECT - Combined command
RUN apt-get update && apt-get install -y \
nodejs \
curl \
gitThe && operator ensures that if the update fails, the install won't run. This pattern prevents Docker from using a stale cached package index from a previous build.
After installing packages, remove the apt cache to keep your image lean:
RUN apt-get update && apt-get install -y \
nodejs \
&& rm -rf /var/lib/apt/lists/*This removes the package lists and prevents them from being stored in the Docker layer. The /var/lib/apt/lists/* directory contains the package index files that apt-get update downloads.
If the error persists, check that the package exists in your Ubuntu version:
1. Check your base image version:
FROM ubuntu:22.04 # Know which version you're using2. Search for the package on packages.ubuntu.com or test in a temporary container:
docker run -it ubuntu:22.04 bash
apt-get update
apt-cache search nodejs3. If the package doesn't exist, find the correct name or use a different Ubuntu version:
# nodejs might not be available; use specific version
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y nodejs
# Or install from NodeSource if you need a specific version
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
RUN apt-get install -y nodejsComments and line continuations can break multi-line RUN commands:
# ❌ WRONG - Comment breaks the command
RUN apt-get update && apt-get install -y \
curl \ # this is curl
nodejs
# ✅ CORRECT - Comments before the command
# Install build dependencies
RUN apt-get update && apt-get install -y \
curl \
nodejsMake sure:
- Backslashes (\) at the end of each line except the last
- No comments inline with package names
- Proper line continuation without trailing spaces after backslashes
If you suspect Docker's build cache is causing issues, rebuild without cache:
docker build --no-cache -t myapp .This forces Docker to run all commands fresh, including apt-get update, which will fetch the latest package indexes. Use this as a diagnostic step rather than a permanent solution.
Base image selection matters: Using FROM ubuntu:latest can lead to unexpected package availability changes when Ubuntu releases new versions. Pin to specific versions like ubuntu:22.04 or ubuntu:focal for reproducible builds.
Non-interactive mode: Set ARG DEBIAN_FRONTEND=noninteractive at the top of your Dockerfile to prevent apt from prompting for input during package installation, which would cause builds to hang.
Minimal installations: Use apt-get install --no-install-recommends to install only essential dependencies and reduce image size. This can reduce installation size by 50% or more.
Archived releases: If you're stuck with an old Ubuntu version (14.04, 16.04), you may need to update /etc/apt/sources.list to point to archive.ubuntu.com instead of standard mirrors:
RUN echo "deb http://archive.ubuntu.com/ubuntu/ trusty main universe" > /etc/apt/sources.list
RUN apt-get update && apt-get install -y <package>Network/DNS issues: If apt-get update itself fails with connection errors, you may need to configure Docker's DNS settings in /etc/docker/daemon.json or check corporate firewall/proxy settings.
dockerfile parse error line 5: unknown instruction: RRUN
How to fix 'unknown instruction' Dockerfile parse error in Docker
Error response from daemon: manifest for nginx:nonexistent not found: manifest unknown: manifest unknown
How to fix 'manifest for image:tag not found' in Docker
Error response from daemon: invalid reference format: repository name must be lowercase
How to fix 'repository name must be lowercase' in Docker
Error response from daemon: No such image
How to fix 'No such image' in Docker
Error response from daemon: Container is not running
How to fix 'Container is not running' when using docker exec