How to fix E: Unable to locate package nodejs in Docker
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.
Quick fix
Fixes E: Unable to locate package nodejs
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 \
git- Combine apt-get update and install in single RUN command
- Add cache cleanup to reduce image size
- Verify package name and availability
- Check for Dockerfile syntax errors
FROM ubuntu:22.04# ❌ WRONG - Separate commands (update gets cached)RUN apt-get updateRUN apt-get install -y nodejs# ✅ CORRECT - Combined commandRUN apt-get update && apt-get install -y \nodejs \curl \gitE: Unable to locate package nodejs
What this error means
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.
Typical symptoms
- Build fails with 'E: Unable to locate package <package-name>'
- apt-get install commands succeed on host but fail in Dockerfile
- Error appears even for common packages like curl, git, or nodejs
- Same Dockerfile worked previously but now fails unexpectedly
Common causes
- Missing apt-get update before apt-get install in Dockerfile
- apt-get update and apt-get install in separate RUN commands (Docker caches outdated index)
- Package name typo or package doesn't exist in the Ubuntu version being used
- Outdated base image with archived or deprecated package repositories
- Network connectivity issues preventing repository access
- Comments or syntax errors in multi-line RUN commands breaking apt-get
How to fix "E: Unable to locate package nodejs"
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.
Advanced notes
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.
Frequently asked questions
- What does "E: Unable to locate package nodejs" mean?
- 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.
- How do I fix "E: Unable to locate package nodejs"?
- 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.
- What causes "E: Unable to locate package nodejs"?
- Missing apt-get update before apt-get install in Dockerfile apt-get update and apt-get install in separate RUN commands (Docker caches outdated index) Package name typo or package doesn't exist in the Ubuntu version being used
- Is the "E: Unable to locate package nodejs" error serious?
- It is a low-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚Docker Official Documentation
- 💬Cannot install packages inside docker Ubuntu image(Stack Overflow)
- 💬Docker RUN apt-get install - Unable to locate package(Stack Overflow)
- 📝
- 📖100 Common Docker Errors & Solutions(Official Docs)