Skip to content

Instantly share code, notes, and snippets.

@13am
Last active May 6, 2020 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 13am/a6bb1a1c4803afbf78457a57e0d47dad to your computer and use it in GitHub Desktop.
Save 13am/a6bb1a1c4803afbf78457a57e0d47dad to your computer and use it in GitHub Desktop.
Download and compile python 3 on macOS
#!/bin/bash
# Usage: bash download-and-compile-python3.sh 3.7.2
set -eu
destination_dir="${HOME}/lib/python"
start_dir="$(pwd)"
pythonversion=$1
# Install the dependencies with homebrew.
# This should be run once, commented out here so that it'll not
# be run on every execution of the script:
# brew install openssl xz zlib wget sqlite libffi
# Download the source and uncompress
mkdir -p ${destination_dir}
cd ${destination_dir}
wget https://www.python.org/ftp/python/${pythonversion}/Python-${pythonversion}.tgz
tar -xvf Python-${pythonversion}.tgz
# Set the compiler to find openssl, zlib and sqlite.
# Note, on a Mac these exports may not work, so they
# need to be given to ./configure on the command line,
# see https://stackoverflow.com/questions/46457404/how-can-i-compile-python-3-6-2-on-macos-with-openssl-from-homebrew/46476640
# and https://bugs.python.org/issue34028
requirements=(openssl zlib sqlite libffi)
CPPFLAGS=""
LDFLAGS=""
for requirement in ${requirements[@]}; do
CPPFLAGS="${CPPFLAGS} -I$(brew --prefix ${requirement})/include"
LDFLAGS="${LDFLAGS} -L$(brew --prefix ${requirement})/lib"
done
export CPPFLAGS
export LDFLAGS
# Compile the source into the uncompressed folder
cd ${destination_dir}/Python-${pythonversion}
./configure CPPFLAGS="${CPPFLAGS}" LDFLAGS="${LDFLAGS}" \
--with-openssl="$(brew --prefix openssl)" \
--prefix "$(pwd)/installation" \
--exec-prefix "$(pwd)/installation"
make
make install
echo "The python executable is available here:"
echo `pwd`/installation/bin/python3
echo "Trying it to see if it works!"
`pwd`/installation/bin/python3 --version
cd ${start_dir}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment