Pyenv and Jenkins
Jan 22, 2023
3 minute read


Pyenv lets you manage multiple versions of Python. It’s a very useful tool for developers and maintainers of projects that require support of earlier or later versions of Python.

Jenkins is a popular CI/CD tool. One particularly important feature of Jenkins is the pipeline. Through pipelines, one can automate the process of building, testing, and deploying software.

Pyenv and Jenkins is a powerful combination of tools especially when managing changes such as upgrading Python versions.

Pyenv in Jenkins

There are lots of ways to install Pyenv in Jenkins. One way is to install Pyenv in the Jenkins Docker image. For new Jenkins integration, this is good enough.

However, one should consider other ways if not applicable as this approach requires rebuilding the image every time there’s a new version of Pyenv. So if there are other options such as using a plugin that is well-maintained and supported by the community and integrates well with your Jenkins build items, it’s better to use that.


The Jenkins CI Github Documentation has this to say about installing more tools.

You can run your container as root - and install via apt-get, install as part of build steps via jenkins tool installers, or you can create your own Dockerfile to customise, for example:

FROM jenkins/jenkins:lts-jdk11
# if we want to install via apt
USER root
RUN apt-get update && apt-get install -y ruby make more-thing-here
# drop back to the regular jenkins user - good practice
USER jenkins

Using the same principle we can create a Dockerfile that looks like the following:

# 1. Build essentials
FROM jenkins/jenkins:lts-jdk11
USER root
RUN apt-get update && \
    apt-get install -y \
    python3-pip \
    make \
    build-essential \
    libssl-dev \
    zlib1g-dev \
    libbz2-dev \
    libreadline-dev \
    libsqlite3-dev \
    wget \
    curl \
    llvm \
    libncurses5-dev \
    libncursesw5-dev \
    xz-utils \
    tk-dev \
    libffi-dev \
    liblzma-dev \
    python3-openssl && \
    pip install --upgrade pip
# 2. Installing Pyenv
USER jenkins
ENV PYENV_ROOT /var/jenkins_home/.pyenv
# Install pyenv
RUN set -ex \
    && curl | bash \
    && pyenv update \
    && pyenv install 3.7.15 3.8.15 3.9.15 3.10.8 3.11.0 3.12-dev \
    && pyenv rehash

The Dockerfile comprises of two parts: 1. Build essentials and 2. Installing Pyenv. The build essentials are required to build related software components. The second part is installing Pyenv and the Python versions we want to use.

Jenkins build step

Within any Jenkins configuration, we can test the availability of Pyenv from an “Execute shell” build step.

echo "Installed Python versions"
echo "$(pyenv versions)"
echo "Using Python version"
echo "$(python3 --version)"

Below is an example console output of a Jenkins build.

alt Output the availability of Pyenv in Jenkins!

🐋 hello there! If you enjoy this, a "Thank you" is enough.

Or you can also ...

Buy me a teaBuy me a tea

comments powered by Disqus