How to fix E: Unable to locate package nodejs in Docker

DockerBEGINNERLOWUpdated Dec 6, 2025· 153 views

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
  1. Combine apt-get update and install in single RUN command
  2. Add cache cleanup to reduce image size
  3. Verify package name and availability
  4. Check for Dockerfile syntax errors
docker — error
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
E: 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

  1. Missing apt-get update before apt-get install in Dockerfile
  2. apt-get update and apt-get install in separate RUN commands (Docker caches outdated index)
  3. Package name typo or package doesn't exist in the Ubuntu version being used
  4. Outdated base image with archived or deprecated package repositories
  5. Network connectivity issues preventing repository access
  6. Comments or syntax errors in multi-line RUN commands breaking apt-get

How to fix "E: Unable to locate package nodejs"

1Combine apt-get update and install in single RUN command

This is the most common fix. Always run apt-get update immediately before apt-get install in the same RUN command:

dockerfile
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

The && 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.

2Add cache cleanup to reduce image size

After installing packages, remove the apt cache to keep your image lean:

dockerfile
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.

3Verify package name and availability

If the error persists, check that the package exists in your Ubuntu version:

1. Check your base image version:

dockerfile
FROM ubuntu:22.04  # Know which version you're using

2. Search for the package on packages.ubuntu.com or test in a temporary container:

bash
docker run -it ubuntu:22.04 bash
apt-get update
apt-cache search nodejs

3. If the package doesn't exist, find the correct name or use a different Ubuntu version:

dockerfile
# 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 nodejs
4Check for Dockerfile syntax errors

Comments and line continuations can break multi-line RUN commands:

dockerfile
# ❌ 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 \
    nodejs

Make 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

5Use --no-cache flag if cache corruption suspected

If you suspect Docker's build cache is causing issues, rebuild without cache:

bash
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:

dockerfile
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