Home > Net >  Installing Python 3.11.1 on a docker container
Installing Python 3.11.1 on a docker container

Time:01-19

I want to use debian:bullseye as a base image and then install a specific Python version - i.e. 3.11.1. At the moment I am just learning docker and linux.

From what I understand I can either:

  1. Download and compile sources
  2. Install binaries (using apt-get)
  3. Use a Python base image

I have come across countless questions on here and articles online. Do I use deadsnakes? What version do I need? Are there any official python distributions (who is deadsnakes anyway)?

But ultimately I want to know the best means of getting Python on there. I don't want to use a Python base image - I am curious in the steps involved. Compile sources - I am far from having that level of knowhow - and one for another day.

Currently I am rolling with the following:

FROM debian:bullseye

RUN apt update && apt upgrade -y
RUN apt install software-properties-common -y
RUN add-apt-repository "ppa:deadsnakes/ppa"
RUN apt install python3.11

This fails with:

#8 1.546 E: Unable to locate package python3.11
#8 1.546 E: Couldn't find any package by glob 'python3.11'

Ultimately - it's not the error - its just finding a good way of getting a specific Python version on my container.

CodePudding user response:

In case you want to install Python 3.11 in debian bullseye you have to compile it from source following the next steps (inside the Dockerfile):

sudo apt update
sudo apt install software-properties-common wget
wget https://www.python.org/ftp/python/3.11.1/Python-3.11.1.tar.xz
sudo tar -xf Python-3.11.1.tar.xz
cd Python-3.11.1
sudo ./configure --enable-optimizations
sudo make altinstall

Another option (easiest) would be to use the official Python Docker image, in your case:

FROM 3.11-bullseye

You have all the versions available in docker hub.

Other option that could be interesting in your case is 3.11-slim-bullseye, that is an image that does not contain the common packages contained in the default tag and only contains the minimal packages needed to run python.

CodePudding user response:

Based on @tomasborella answer, to do this in docker:

Dockerfile

FROM debian:bullseye

RUN apt-get update -y \
    && apt-get upgrade -y \
    && apt-get -y install build-essential \
        zlib1g-dev \
        libncurses5-dev \
        libgdbm-dev \ 
        libnss3-dev \
        libssl-dev \
        libreadline-dev \
        libffi-dev \
        libsqlite3-dev \
        libbz2-dev \
        wget \
    && export DEBIAN_FRONTEND=noninteractive \
    && apt-get purge -y imagemagick imagemagick-6-common 

RUN cd /usr/src \
    && wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz \
    && tar -xzf Python-3.11.0.tgz \
    && cd Python-3.11.0 \
    && ./configure --enable-optimizations \
    && make altinstall

RUN update-alternatives --install /usr/bin/python python /usr/local/bin/python3.11 1

update-alternatives - will update the links to allow you to run python as opposed to specifying python3.11 when you want to run it.

It takes a while to compile those sources!

  • Related