Installing Python3.9 and pip in docker image (and not python 3.8)

12,250

ubuntu store system python package in /usr/lib/python3/dist-packages and share it for all python3 versions.

Inside an ubuntu 20.04 container (docker run -ti ubuntu:20.04):

apt-get update
apt-get install software-properties-common
add-apt-repository ppa:deadsnakes/ppa
# Install py39 from deadsnakes repository
apt-get install python3.9
# Install pip from standard ubuntu packages
apt-get install python3-pip

Then you can invoke pip with python3.9 -m pip ..., but be careful, everything with be installed in /usr/lib/python3/dist-packages.

But if all you need is a dockerized version of python3.9, it would be better to use the official python docker image, available with debian or alpine (very light) as a base. Both use a standard lib/pythonx.y/site-packages.

By default these images defines entrypoint to the python interpreter, but you can override why bash or busybox sh (alpine).

Edit for debian

launchpad repositories are build on ubuntu distribution. Using on over distribution can lead in libraries incompatibilities.

In deadsnakes ubuntu/xenial repository, python3.9 requires libssl1.0.0, but debian/buster offer libssl1.1.

The easiest way to get python3.9 in debian / buster is to use the official image (docker run -ti python:3.9-buster). Otherwise, build it ...

Share:
12,250
ThoughtfulHacking
Author by

ThoughtfulHacking

Updated on June 05, 2022

Comments

  • ThoughtfulHacking
    ThoughtfulHacking almost 2 years

    I'm trying to build a docker image to run some tests, and I'm having trouble convincing ubuntu to install python 3.9 and pip, and NOT python3.8.

    starting Docker unbuntu:latest, it's no problem to install python3.9, but there is no pip. Ok, I'll try doing what all the answers suggests:

    apt-get install python3-pip

    but this installs **all ** of python3.8, plus other junk. Now, it does work at this point, but it bugs me to have a completely unnecessary version of python in an image that I want to be as small as it can be.

    No, uninstalling 3.8 afterwards doesn't work, it removes pip.

    How can I get python3.9, with pip, and no python3.8?

    I did see this: How to install pip for Python 3.9 on Ubuntu 20.04

    curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

    And when you run the script, it complains about needing distutils. No problem:

    sudo apt-get install python3-distutils

    And what does python3-distutils depend on? Why, python3.8, of course!

    • Klaus D.
      Klaus D. almost 3 years
      Have you considered using one of the python base images? They are tailored to be used with Python projects. In most cases I personally use a -alpine image. It's not going to get much more simple and smaller.
  • OneCricketeer
    OneCricketeer almost 3 years
    tl;dr The PPA is needed because installing a new Python is going to use the Ubuntu bulit-in Python installation version
  • thadk
    thadk over 2 years
    For anyone else, this didn't seem to work on a Debian-based (buster) docker image.
  • Jackssn
    Jackssn over 2 years
    After command add-apt-repository ppa:deadsnakes/ppa you need again run command: apt-get update (because this actualize versions at now moment)
  • bayman
    bayman about 2 years
    apt-get install python3-9 does not work on ubuntu:20.04 and apt-get install python3-pip will install the pip for python3.8
  • vahlala
    vahlala about 2 years
    The command to install python is apt-get install python3.9 with a ., not apt-get install python3-9 with a -