Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Micromamba and uv for Python package management #801

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/build/python/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# name: python
# group: build
# depends: [build-essential, pip_cache]
# notes: installs core `python3` packages and `pip`
# notes: installs core `python`, `uv` and `pip` in a conda environment
#---
ARG BASE_IMAGE
FROM ${BASE_IMAGE}
Expand All @@ -20,7 +20,12 @@ ENV PYTHON_VERSION=${PYTHON_VERSION_ARG} \
PIP_CACHE_PURGE=true \
PIP_ROOT_USER_ACTION=ignore \
TWINE_NON_INTERACTIVE=1 \
DEBIAN_FRONTEND=noninteractive
DEBIAN_FRONTEND=noninteractive \
PATH=/opt/conda/bin:$PATH \
MAMBA_ROOT_PREFIX=/opt/conda \
MAMBA_EXE=/usr/local/bin/micromamba \
CONDA_PREFIX=/opt/conda \
UV_COMPILE_BYTECODE=1

COPY install.sh /tmp/install_python.sh
COPY install.sh /tmp/install_python.sh
RUN /tmp/install_python.sh
4 changes: 2 additions & 2 deletions packages/build/python/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ def python(version, requires=None) -> list:

pkg['name'] = f'python:{version}'
pkg['build_args'] = {'PYTHON_VERSION_ARG': version}

if Version(version) == PYTHON_VERSION:
pkg['alias'] = 'python'

if requires:
pkg['requires'] = requires

return pkg

package = [
Expand Down
89 changes: 47 additions & 42 deletions packages/build/python/install.sh
Original file line number Diff line number Diff line change
@@ -1,54 +1,59 @@
#!/usr/bin/env bash
# Python installer
# Python installer using Micromamba
set -x

apt-get update
apt-get install -y --no-install-recommends \
python${PYTHON_VERSION} \
python${PYTHON_VERSION}-dev

which python${PYTHON_VERSION}
return_code=$?
set -e

if [ $return_code != 0 ]; then
echo "-- using deadsnakes ppa to install Python ${PYTHON_VERSION}"
add-apt-repository ppa:deadsnakes/ppa
apt-get update
apt-get install -y --no-install-recommends \
python${PYTHON_VERSION} \
python${PYTHON_VERSION}-dev
curl -Ls https://micro.mamba.pm/api/micromamba/linux-$(uname -m | sed 's/x86_64/64/;s/aarch64/aarch64/')/latest | tar -xvj bin/micromamba
mv bin/micromamba /usr/local/bin/

export MAMBA_ROOT_PREFIX=/opt/conda
export CONDA_PREFIX=/opt/conda
export PATH=/opt/conda/bin:$PATH
mkdir -p $MAMBA_ROOT_PREFIX
eval "$(micromamba shell hook -s posix)"
micromamba shell init -s bash -r $MAMBA_ROOT_PREFIX

# Install Python and core packages in base environment
micromamba install -y -n base \
python=${PYTHON_VERSION} \
setuptools \
packaging \
"cython<3" \
wheel \
pip \
uv \
twine \
psutil \
pkginfo

# Add micromamba initialization to /etc/profile.d for all shells
cat > /etc/profile.d/mamba.sh << 'EOF'
export MAMBA_EXE=/usr/bin/micromamba
export MAMBA_ROOT_PREFIX=/opt/conda
export PATH=/opt/conda/bin:$PATH

if [ -f "${MAMBA_EXE}" ]; then
__mamba_setup="$("$MAMBA_EXE" shell hook --shell bash --root-prefix "$MAMBA_ROOT_PREFIX" 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__mamba_setup"
else
alias micromamba="$MAMBA_EXE"
fi
unset __mamba_setup
fi

rm -rf /var/lib/apt/lists/*
apt-get clean
micromamba activate
EOF

curl -sS https://bootstrap.pypa.io/get-pip.py | python${PYTHON_VERSION} || \
curl -sS https://bootstrap.pypa.io/pip/3.6/get-pip.py | python3.6
chmod +x /etc/profile.d/mamba.sh
source /etc/profile.d/mamba.sh

ln -s /usr/bin/python${PYTHON_VERSION} /usr/local/bin/python3
#ln -s /usr/bin/pip${PYTHON_VERSION} /usr/local/bin/pip3

# this was causing issues downstream (e.g. Python2.7 still around in Ubuntu 18.04, \
# and in cmake python enumeration where some packages expect that 'python' is 2.7) \
#RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 1 && \ \
# update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1 \
ln -sf /opt/conda/bin/python /usr/local/bin/python3
ln -sf /opt/conda/bin/python /usr/local/bin/python
ln -sf /opt/conda/bin/pip /usr/local/bin/pip3
ln -sf /opt/conda/bin/pip /usr/local/bin/pip

# Verify installation
which python3
python3 --version

which pip3
pip3 --version

python3 -m pip install --upgrade pip pkginfo --index-url https://pypi.org/simple

pip3 install --no-cache-dir --verbose --no-binary :all: psutil
pip3 install --upgrade --no-cache-dir \
setuptools \
packaging \
'Cython<3' \
wheel

pip3 install --upgrade --no-cache-dir --index-url https://pypi.org/simple \
twine