Skip to content

Instantly share code, notes, and snippets.

@darkvertex
Created March 9, 2022 15:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkvertex/b21ec95124e28473d96d4aa11d14dccd to your computer and use it in GitHub Desktop.
Save darkvertex/b21ec95124e28473d96d4aa11d14dccd to your computer and use it in GitHub Desktop.
Example of using Earthly for compiling Python for CentOS via pyenv
# Super easy Python compiler using Earthly.
# (See https://earthly.dev to learn more about the syntax. Docker & Earthly required.)
#
# Arguments:
# --pyver=<version> (Python version to compile. *REQUIRED*)
# --centos_ver=<version> (CentOS version to compile under. Defaults to "7".)
# --pyenv_root=<path> (Path to the Python environment. Optional. Recommendation is to match your current compilation root path.)
#
# For example, run me like this:
# > earthly +pythonwithpip --pyver=2.7.18
# > earthly +pythonwithpip --pyver=3.7.5
# > earthly +pythonwithpip --pyver=3.9.10
# and you will have a ./versions/<version> subdirectory with the compiled Python release upon success.
#
# NOTE: If you get failures, pass "-i" to the earthly command if you want to pause within the container upon error.
#
# ---------------------------------------------------------------------------------------------------------------------
ARG centos_ver=7
FROM centos:${centos_ver}
# Build dependencies:
deps:
# Python compilation base requirements:
RUN yum install -y make gcc patch zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel libffi-devel xz-devel curl bash git && \
yum clean all
# Deploy mini pip intall helper:
RUN printf '\n\
function pip_for_py2() {\n\
curl -o /tmp/get-pip.py https://bootstrap.pypa.io/pip/2.7/get-pip.py;\n\
python2 /tmp/get-pip.py\n\
rm -f /tmp/get-pip.py\n\
python2 -m pip install --no-cache-dir --upgrade pip==20.3.4;\n\
}\n\
\n\
function pip_for_py3() {\n\
python3 -m ensurepip;\n\
python3 -m pip install --no-cache-dir --upgrade pip;\n\
}\n\
\n\
command -v python3 && pip_for_py3 || pip_for_py2\n\
' >> /pip_installer.sh && chmod +x /pip_installer.sh
# Install "pyenv" to manage Python versions:
ARG pyenv_root="/root/.pyenv"
ENV PYENV_ROOT="${pyenv_root}"
ENV PATH="${PYENV_ROOT}/bin:${PYENV_ROOT}/shims:${PATH}"
RUN curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
# Compile Python:
compilation:
FROM +deps
ARG pyver
ENV pyver=${pyver}
RUN pyenv install ${pyver} && pyenv global ${pyver}
# Deploy and upgrade pip:
pip:
FROM +compilation
RUN /pip_installer.sh
pythonwithpip:
FROM +pip
SAVE ARTIFACT ${PYENV_ROOT}/versions/${pyver} AS LOCAL ./versions/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment