Create JupyterLab container with python version 3.11

Motivation

I was trying a graph RAG and got an error “TypeError: ‘NoneType’ object is not iterable”. I did a lot of research, but could not find the direct cause of the error and just could not get around it. The python version of the container I was using at the time was 3.10.12. The version of python in the article I was referring to was 3.11.0, so I decided to update the python version of the container to 3.11 as a trial.

This post summarizes the creation of the JupyterLab container with a python version of 3.11.

Sources and Background

  1. [Ubuntu 22.04: Running pip installed with python3-pip with python3.12 -m pip gives the error No module named 'distutils'](https://qiita.com/yuji38 kwmt/items/e582686f358814c851da) - As the title of the article says, the article is about updating to python 3.12, but I referred to the Dockerfile that creates a docker container with python 3.11 embedded.
  2. [After changing from ubuntu22.04 to 24.04, the error “externally-managed-environment” occurred during the installation of pip](https://qiita.com/yuji38kwmt/items/f3f3505991 bb4859592f) - This is the same person who wrote the above article. Actually, I first thought that if I upgraded my ubuntu22.04-based nvidia/cuda container to ubuntu24.04-based, the python version would also be upgraded, so I modified only the corresponding parts in the Dockerfile I had been using and tried to build it, but I got the following error message error: externally-managed-environment” occurred and I found this article. After skimming through it, I felt it was a bit of a hurdle, so I referred to the Dockerfile in the above article as a way to upgrade python without changing ubuntu22.04.
  3. [How to stop this message: “Would you like to receive official Jupyter news?”](https://stackoverflow.com/questions/75511508/how-to-stop-this -message-would-you-like-to-receive-official-jupyter-news) - This article shows how to stop the popup display in the lower right corner of JupyterLab.

Dockerfile

The Dockerfile is as follows, with the first half referring to the Dockerfile in Source 1. and the second half, the installation of the python package, in this post Dockerfile as it is.

 Dockerfile for creating a JupyterLab container
# containing python version 3.11

FROM nvidia/cuda:12.1.1-cudnn8-devel-ubuntu22.04

# Set bash as the default shell
ENV SHELL=/bin/bash

# Install prerequisites
RUN set -x \
        && export DEBIAN_FRONTEND=noninteractive \
        && apt-get update \
        && apt-get install -y --no-install-recommends \
        gnupg2 \
        software-properties-common \
        language-pack-ja \
        tzdata \
        curl \
        lsb-release \
        && apt-get -y clean \
        && rm -rf /var/lib/apt/lists/*

# Set locale & timezone
RUN update-locale LANG=ja_JP.UTF-8 LANGUAGE=ja_JP:ja \
    && ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime \
    && echo "Asia/Tokyo" > /etc/timezone

ENV LANG=ja_JP.UTF-8
ENV LC_ALL=ja_JP.UTF-8
ENV LC_CTYPE=ja_JP.UTF-8

# Install packages
RUN set -x \
    # add deadsnakes PPA to install Python 3.11
    && add-apt-repository ppa:deadsnakes/ppa \
    && apt-get update \
    && apt-get install -y --no-install-recommends \
        python3.11-dev \
        python3-pip \
        # common tools
        bash-completion \
        build-essential \
        git \
        iputils-ping \
        jq \
        less \
        net-tools \
        openssh-client \
        sudo \
        tar \
        time \
        unzip \
        vim \
        wget \
        xz-utils \
        zip \
    && apt-get -y clean \
    && rm -rf /var/lib/apt/lists/*

# Python / pip
RUN ln -s $(which python3.11) /usr/bin/python

# Python / pip
RUN ln -s $(which python3.11) /usr/bin/python

# Install python packages used so far
RUN pip install --upgrade pip setuptools \
        && pip install torch torchvision torchaudio \
         --index-url https://download.pytorch.org/whl/cu121 \
        && pip install jupyterlab matplotlib pandas scikit-learn ipywidgets \
        && pip install transformers accelerate sentencepiece einops \
        && pip install auto-gptq optimum \
        && pip install langchain bitsandbytes protobuf \
        && pip install langchain-community langchain_openai wikipedia \
        && pip install langchain-huggingface unstructured html2text rank-bm25 janome \
        && pip install langchain-chroma sudachipy sudachidict_full \
        && pip install langchain-experimental neo4j \
        && pip install json-repair langchain-mistralai \
        && pip install mysql-connector-python \
        && pip install ragas datasets neo4j-graphrag \
        && pip install pypdf tiktoken sentence_transformers faiss-gpu trafilatura \
        && pip install ollama langchain-ollama

# Stop message on jupyterlab
RUN jupyter labextension disable "@jupyterlab/apputils-extension:announcements"

# Create a working directory
WORKDIR /workdir

# Port number in container side
EXPOSE 8888

ENTRYPOINT ["jupyter-lab", "--ip=0.0.0.0", "--port=8888", "--no-browser", "--allow-root", "--NotebookApp.token=''"]

CMD ["--notebook-dir=/workdir"]

The “# Stop message on jupyterlab” part is to hide the following popup that appeared in the lower right corner during JupyterLab operation, which was depressing. See source 3.

Would you like to receive official Jupyter news?

Conclusion

I have been trying to upgrade my python and have been exposed to the world of ubuntu24.04, but it seems that pyhon’s module management has been updated and I can no longer comfortably do “pip/pip3 install” as I have in the past. I plan to move to ubuntu24.04 soon, but I’ll have to wait until I have a little more information.