483

I installed Ubuntu 14.04 image on docker. After that, when I try to install packages inside the ubuntu image, I'm getting unable to locate package error:

apt-get install curl

Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package curl

How to fix this error?

Melursus
  • 10,328
  • 19
  • 69
  • 103

11 Answers11

943

It is because there is no package cache in the image, you need to run:

apt-get update

before installing packages, and if your command is in a Dockerfile, you'll then need:

apt-get -y install curl

To suppress the standard output from a command use -qq. E.g.

apt-get -qq -y install curl
Connor
  • 4,216
  • 2
  • 29
  • 40
ISanych
  • 21,590
  • 4
  • 32
  • 52
  • 1
    this works for me, should i run -qq for all the times – Ramesh Kumar Thiyagarajan Dec 03 '14 at 14:03
  • 6
    -qq suppress output which you usually don't need in Dockerfile. Another nice trick - tell debconf that it is working in non interactive environment: echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections – ISanych Dec 03 '14 at 14:11
  • 25
    For some reason this doesn't work for me, still the same "E: Unable to locate package .." message – tblancog Aug 28 '16 at 02:48
  • "apt-get-update" and "apt-get install curl" worked fine! – Sindhu Oct 29 '17 at 00:28
  • 1
    I found that this error also happens in docker when it runs out of space. I ran `docker image prune` to free up space and that fixed it for me. – Jonathan Rys Nov 22 '19 at 16:26
  • Many thanks. After auto-installing 438th package I realized, that apt-get by default installs recommendations (oh boy), so it is useful to add `--no-install-recommends` to the mix. – greenoldman Oct 13 '21 at 19:48
  • 1
    This doesn't seem to work. I would suggest checking @creimers answer since it has some nice explanation what goes wrong. – rbaleksandar Jun 19 '22 at 20:18
228

From the docs in May 2017 2018 2019 2020 2021 2022 2023

Always combine RUN apt-get update with apt-get install in the same RUN statement, for example

RUN apt-get update && apt-get install -y package-bar

(...)

Using apt-get update alone in a RUN statement causes caching issues and subsequent apt-get install instructions fail.

(...)

Using RUN apt-get update && apt-get install -y ensures your Dockerfile installs the latest package versions with no further coding or manual intervention. This technique is known as “cache busting”.

creimers
  • 4,975
  • 4
  • 33
  • 55
  • 1
    I have to agree on this one. But is there no fix for this behaviour? It is quite annoying not to mention that it 1)increases the build time (since every update will go through the cache to see if there is something old) and 2)uses connection/bandwidth that can be used for something more useful. – rbaleksandar Jun 19 '22 at 20:13
  • I'm not a docker wizzzzard, but it is my understanding that this is necessary because of the overlay filesystem of the containers and how each line in a Dockerfile represents one self contained 'layer'. – creimers Jun 20 '22 at 07:19
  • 1
    Do you come here every year to edit this answer? Just Curios :) – Destiny Franks Aug 19 '22 at 10:26
  • 2
    I do. To indicate that the answer is still up to date. – creimers Aug 19 '22 at 13:20
  • Is there a "next step" after trying this and getting the same error? I have also done my best to purge all cached images/layers on the host system. There does not appear to be a networking or DNS issue in the container; there are no network-related errors, only "cannot locate package". – shadowtalker Sep 30 '22 at 08:03
  • @shadowtalker Not quite sure, I've never had problems with this approach. Naive question + no offense: Are you certain the package actually does exist? – creimers Sep 30 '22 at 08:59
  • @creimers I copied and pasted the name from packages.debian.org because I thought I must have typed it wrong! I also confirmed that the package (`libgeos`) is available for my host machine architecture (arm64), but it's possible that Docker Desktop for Mac does something weird here. I will try to investigate further. – shadowtalker Sep 30 '22 at 12:09
  • Following up here: I don't have a solution, but I think a sensible "next step" is to 1) make sure that the package exists _for your machine architecture_, and 2) make sure that the machine architecture inside your Docker container is what you expect! In my case, I got `uname -m` as `aarch64`, whereas on my host system isn't `arm64`. – shadowtalker Sep 30 '22 at 17:13
  • 1
    6 years later and I can confirm this has solved my problem. – Piroinno Mar 20 '23 at 01:20
  • This answer should be marked as the correct answer for this question. Great Job – Mickey Hovel Jun 14 '23 at 13:22
22

Add following command in Dockerfile:

RUN apt-get update
halfer
  • 19,824
  • 17
  • 99
  • 186
Gourav Singla
  • 1,728
  • 1
  • 15
  • 22
12

Make sure you don't have any syntax errors in your Dockerfile as this can cause this error as well. A correct example is:

RUN apt-get update \
    && apt-get -y install curl \
    another-package

It was a combination of fixing a syntax error and adding apt-get update that solved the problem for me.

Connor
  • 4,216
  • 2
  • 29
  • 40
10

Running apt-get update didn't solve it for me because, it always seemed to read the result of this command from cache and somehow the cache seemed to be corrupted. My suspicion is that, this happens if you have multiple docker containers having the same 'base image' ( for me it was python:3.8-slim).

So, here's what worked for me:

Stopping all Docker containers in the system

docker stop $(sudo docker ps -aq) 

Removing all dangling containers

docker container prune 

Removing all dangling images

docker image prune 

After running these commands the docker build seems to lose the corrupted cache and does a clean apt-get update which fetches the correct package info and the package installations progress as expected.

because_im_batman
  • 975
  • 10
  • 26
4

I found that mounting a local volume over /tmp can cause permission issues when the "apt-get update" runs, which prevents the package cache from being populated. Hopefully, this isn't something most people do, but it's something else to look for if you see this issue.

Groboclown
  • 51
  • 4
2

Out of the blue I started getting this problem. I did this first and it seemed to resolve the problem:

RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/*
Arkham Angel
  • 309
  • 1
  • 5
  • 18
1

I was getting the same error when trying to install cron and nginx-extras in my Dockerfile. I tried all the answers so far and no dice.

I then simply ran sudo service docker restart which fixed the issue for me.

David
  • 91
  • 1
  • 4
0

This has since been answered but I encountered this issue earlier and none of these steps worked.

The issue, it turns out, was that I had saved a list of packages to install in a separate file. This was saved on a windows machine with a CRLF line separator (\r\n and not just \n). Forcing the line endings to only \n on top of the steps provided in the accepted answer resolved the issue.

Ronan
  • 407
  • 3
  • 5
  • 13
-1

I have met the same question when I try to install jre and I try the command "apt-get update" and then try "apt install default-jre" again and it worked !

I wish it can help you, good luck!

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 16 '21 at 17:22
-4

You need to update the package list in your Ubuntu:

$ sudo apt-get update
$ sudo apt-get install <package_name>
unnamed-bull
  • 361
  • 4
  • 15