How do I install Python packages in Spacelift runners?

Last updated: June 10, 2025

Context

When running Python scripts in Spacelift, you may need to install additional Python packages that aren't available in the default runner environment. Since the default runner uses Alpine Linux, there are specific considerations for package installation.

Answer

There are two main approaches to installing Python packages in Spacelift runners:

Option 1: Using a Virtual Environment (Recommended)

The simplest approach is to create a virtual environment during your run. Add the following command to your before_init hook in either your spacelift.yml file or through the Spacelift UI (Stack → Settings → Hooks → before_init):

python3 -m venv ./venv && . ./venv/bin/activate && pip install <package_name>

For example, to install boto3:

python3 -m venv ./venv && . ./venv/bin/activate && pip install boto3

Option 2: Custom Runner Image

For more complex requirements or if you need the packages persistently available, you can create a custom runner image with pre-installed packages. This approach is documented in the Customizing the Runner Image guide.

Note: Direct installation using pip (without a virtual environment) will fail in the default Alpine-based runner due to external management restrictions.