Today I Learned (March 25, 2024)

How to install Python 3.12 via the ppa:deadsnakes repo

Python3.12 may be installed on Ubuntu with apt if we add the ppa:deadsnakes/ppa repo to the apt sources list.

sudo add-apt-repository ppa:deadsnakes/ppa \
  && sudo apt update \
  && sudo apt install -y python3.12 

:information_source: The add-apt-repository command requires installation of the software-properties-common package

apt install -y software-properties-common 

To pick Python 3.12 as the new default Python version—in case you have other version installed as well—, run the update-alternatives command as follows:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 0
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.12 0

To confirm, run

$ sudo update-alternatives --config python3
sudo update-alternatives --config python3
There is only one alternative in link group python3 (providing /usr/bin/python3): /usr/bin/python3.12
Nothing to configure.
$ sudo update-alternatives --config python
There is only one alternative in link group python (providing /usr/bin/python): /usr/bin/python3.12
Nothing to configure.

How to install pip with the script

Unfortunately, after installing Python 3.12 you may run in to problems with the apt installation of pip (apt install python3-pip). To fix this, run the install Python script script provided by the pip developer team.

:information_source: script documentation

curl --location --silent --output /tmp/ && \
python /tmp/ "pip==24.0"

Confirm with

$ pip --version
pip 24.0 from /usr/local/lib/python3.12/dist-packages/pip (python 3.12)
$ pip3 --version
pip 24.0 from /usr/local/lib/python3.12/dist-packages/pip (python 3.12)

How to run pre-commit checks in a pipeline for the latest commit

pre-commit run --from-ref HEAD~ --to-ref HEAD~

:bulb: Note that when running this in a pipeline, you may have to init the Git repository and add the directory of your repository to the list in Git config:

git init .
git config --global --add "$(pwd)"

How to build ARM64 images on an X86 machine

docker buildx create --name the-builder
docker buildx use the-builder
docker buildx build 
  --platform linux/arm64
  --file arm-64.Dockerfile 
  --tag "${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}_arm-64"
  • :bulb: Note that creating and using a builder is required to enable cross-arch capabilities for the builder.
  • :information_source: Don’t forget to login to your Docker registry (e.g. in GitLab