Skip to content

Instantly share code, notes, and snippets.

@odashi
Last active September 1, 2024 18:02
Show Gist options
  • Save odashi/76d1969a42c686fecd82411c68a9976c to your computer and use it in GitHub Desktop.
Save odashi/76d1969a42c686fecd82411c68a9976c to your computer and use it in GitHub Desktop.
Local Python installer
#!/bin/bash
# This script installs a Python environment built from source.
# Usage:
# URL='https://gist.githubusercontent.com/odashi/...copy-paste raw path.../install_local_python.sh'
# bash <(curl -sL ${URL}) ./env 3.10.14 64
set -eu -o pipefail
if [ $# -ne 3 ]; then
>&2 echo "Usage: install_local_python.sh INSTALL_DIR MAJOR.MINOR.PATCH BUILD_THREADS"
exit 1
fi
INSTALL_DIR=$1; shift
PYTHON_VERSION=$1; shift
BUILD_THREADS=$1; shift
if [ -e ${INSTALL_DIR} ]; then
>&2 echo "${INSTALL_DIR} already exists."
exit 1
fi
mkdir -p ${INSTALL_DIR}
pushd ${INSTALL_DIR}
mkdir -p src
pushd src
# install Python
git clone https://github.com/python/cpython -b v${PYTHON_VERSION}
pushd cpython
./configure --prefix="${INSTALL_DIR}/python" --enable-optimizations
make -j ${BUILD_THREADS}
make install
popd # cpython
popd # src
# prepare venv
python/bin/python3 -m venv venv
source venv/bin/activate
python -m pip install --no-cache-dir -U pip
popd # ${INSTALL_DIR}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment