Cannot install packages inside docker Ubuntu image

DockerUbuntu 14.04

Docker Problem Overview


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?

Docker Solutions


Solution 1 - Docker

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

Solution 2 - Docker

From the docs in May 2017 2018 2019 2020 2021 2022

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

Solution 3 - Docker

Add following command in Dockerfile:

RUN apt-get update

Solution 4 - Docker

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.

Solution 5 - Docker

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.

Solution 6 - Docker

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.

Solution 7 - Docker

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!

Solution 8 - Docker

You need to update the package list in your Ubuntu:

$ sudo apt-get update
$ sudo apt-get install <package_name>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeveloperView Question on Stackoverflow
Solution 1 - DockerISanychView Answer on Stackoverflow
Solution 2 - DockercreimersView Answer on Stackoverflow
Solution 3 - DockerGourav SinglaView Answer on Stackoverflow
Solution 4 - DockerConnorView Answer on Stackoverflow
Solution 5 - DockerGroboclownView Answer on Stackoverflow
Solution 6 - Dockerbecause_im_batmanView Answer on Stackoverflow
Solution 7 - Docker以司之名View Answer on Stackoverflow
Solution 8 - Dockerunnamed-bullView Answer on Stackoverflow