2019-04-23 10:23:33 -04:00
#!/usr/bin/env bash
#
2013-12-27 18:44:54 +09:00
# Summary: Install a Python version using python-build
2013-01-31 13:20:59 +09:00
#
2026-06-30 04:17:58 +03:00
# Usage: pyenv install [-f] [-kvp] <version>[:<alias>]...
# pyenv install [-f] [-kvp] <definition-file>[:<alias>]
2026-07-28 17:01:20 +05:30
# pyenv install -l|--list [--bare]
2016-02-08 00:01:02 +00:00
# pyenv install --version
2013-01-31 13:20:59 +09:00
#
2014-04-09 21:23:46 +09:00
# -l/--list List all available versions
# -f/--force Install even if the version appears to be installed already
# -s/--skip-existing Skip if the version appears to be installed already
2013-12-27 18:44:54 +09:00
#
# python-build options:
#
2014-04-09 21:23:46 +09:00
# -k/--keep Keep source tree in $PYENV_BUILD_ROOT after installation
# (defaults to $PYENV_ROOT/sources)
# -p/--patch Apply a patch from stdin before building
2016-02-08 00:01:02 +00:00
# -v/--verbose Verbose mode: print compilation status to stdout
# --version Show version of python-build
2014-04-09 21:23:46 +09:00
# -g/--debug Build a debug version
2013-01-31 13:20:59 +09:00
#
2026-06-30 06:42:16 +05:30
# Append `:<alias>' to a version to install it under a custom name, so that
# several builds of the same version can coexist:
#
# pyenv install 3.12.0:my-3.12
#
# This installs into $PYENV_ROOT/versions/my-3.12.
#
2013-01-31 13:20:59 +09:00
# For detailed information on installing Python versions with
# python-build, including a list of environment variables for adjusting
2017-03-06 04:31:25 +00:00
# compilation, see: https://github.com/pyenv/pyenv#readme
2013-01-31 13:20:59 +09:00
#
2012-08-31 15:32:23 +09:00
set -e
[ -n " $PYENV_DEBUG " ] && set -x
2014-10-29 03:35:29 -07:00
# Add `share/python-build/` directory from each pyenv plugin to the list of
# paths where build definitions are looked up.
shopt -s nullglob
for plugin_path in " $PYENV_ROOT " /plugins/*/share/python-build; do
2026-10-30 14:33:24 +03:00
PYTHON_BUILD_DEFINITIONS = " ${ PYTHON_BUILD_DEFINITIONS :+ ${ PYTHON_BUILD_DEFINITIONS } : }${ plugin_path } "
2014-10-29 03:35:29 -07:00
done
export PYTHON_BUILD_DEFINITIONS
shopt -u nullglob
2012-08-31 15:32:23 +09:00
# Provide pyenv completions
if [ " $1 " = "--complete" ] ; then
2026-07-28 17:01:20 +05:30
echo --bare
2016-02-08 00:01:02 +00:00
echo --list
echo --force
echo --skip-existing
echo --keep
echo --patch
echo --verbose
echo --version
echo --debug
2012-08-31 15:32:23 +09:00
exec python-build --definitions
fi
# Load shared library functions
eval " $( python-build --lib) "
usage() {
2015-08-25 15:37:31 +09:00
pyenv-help install 2>/dev/null
2012-08-31 15:32:23 +09:00
[ -z " $1 " ] || exit " $1 "
}
2013-04-19 19:26:11 +09:00
definitions() {
local query = " $1 "
2023-02-22 18:11:48 +01:00
python-build --definitions | $( type -P ggrep grep | head -n1) -F " $query " || true
2013-04-19 19:26:11 +09:00
}
indent() {
sed 's/^/ /'
}
2013-01-31 13:20:59 +09:00
unset FORCE
2014-04-09 21:23:46 +09:00
unset SKIP_EXISTING
2012-08-31 15:32:23 +09:00
unset KEEP
unset VERBOSE
2013-12-27 18:44:54 +09:00
unset HAS_PATCH
2013-03-28 12:04:58 +09:00
unset DEBUG
2026-07-28 17:01:20 +05:30
unset BARE
unset LIST
2012-08-31 15:32:23 +09:00
2022-05-01 05:49:09 +03:00
[ -n " $PYENV_DEBUG " ] && VERBOSE = "-v"
2012-08-31 15:32:23 +09:00
parse_options " $@ "
for option in " ${ OPTIONS [@] } " ; do
case " $option " in
"h" | "help" )
usage 0
;;
2026-07-28 17:01:20 +05:30
"bare" )
BARE = 1
;;
2012-08-31 15:32:23 +09:00
"l" | "list" )
2026-07-28 17:01:20 +05:30
LIST = 1
2012-08-31 15:32:23 +09:00
;;
2013-01-31 13:20:59 +09:00
"f" | "force" )
FORCE = true
;;
2014-04-09 21:23:46 +09:00
"s" | "skip-existing" )
SKIP_EXISTING = true
;;
2012-08-31 15:32:23 +09:00
"k" | "keep" )
[ -n " ${ PYENV_BUILD_ROOT } " ] || PYENV_BUILD_ROOT = " ${ PYENV_ROOT } /sources"
;;
"v" | "verbose" )
VERBOSE = "-v"
;;
2013-12-27 18:44:54 +09:00
"p" | "patch" )
HAS_PATCH = "-p"
;;
2013-03-28 12:04:58 +09:00
"g" | "debug" )
DEBUG = "-g"
;;
2012-08-31 15:32:23 +09:00
"version" )
exec python-build --version
;;
* )
2015-05-27 01:05:46 +09:00
usage 1 >& 2
2012-08-31 15:32:23 +09:00
;;
esac
done
2026-07-28 17:01:20 +05:30
if [[ -n $LIST ]] ; then
if [[ -n $BARE ]] ; then
2026-10-16 13:42:17 +05:30
exec python-build --definitions
2026-07-28 17:01:20 +05:30
else
echo "Available versions:"
definitions | indent
fi
exit
fi
2013-01-31 13:20:59 +09:00
unset VERSION_NAME
2021-06-14 17:08:46 +03:00
# The first argument contains the definition to install. If the
# argument is missing, try to install whatever local app-specific
# version is specified by pyenv. Show usage instructions if a local
# version is not specified.
2022-12-22 18:35:25 +01:00
DEFINITIONS =( " ${ ARGUMENTS [@] } " )
2023-01-22 05:49:40 +03:00
[[ " ${# DEFINITIONS [*] } " -eq 0 ]] && DEFINITIONS =( $( pyenv-local 2>/dev/null || true ) )
[[ " ${# DEFINITIONS [*] } " -eq 0 ]] && usage 1 >& 2
2013-01-31 13:20:59 +09:00
2026-06-30 06:42:16 +05:30
# A `<version>:<alias>` argument installs <version> under the custom name
# <alias>, so that several builds of the same version can coexist. The `latest`
# suffix is reserved for latest-version resolution (see the `install` hook), so
# it is left untouched here.
declare -a ALIASES
for i in " ${ !DEFINITIONS[@] } " ; do
definition = " ${ DEFINITIONS [ $i ] } "
alias = " ${ definition ##*: } "
if [[ " $definition " == *:* ]] && [ " $alias " != "latest" ] ; then
DEFINITIONS[ $i ]= " ${ definition %:* } "
ALIASES[ $i ]= " $alias "
fi
done
2013-01-31 13:20:59 +09:00
# Define `before_install` and `after_install` functions that allow
# plugin hooks to register a string of code for execution before or
# after the installation process.
declare -a before_hooks after_hooks
before_install() {
local hook = " $1 "
before_hooks[ " ${# before_hooks [@] } " ]= " $hook "
}
after_install() {
local hook = " $1 "
after_hooks[ " ${# after_hooks [@] } " ]= " $hook "
}
2022-12-22 18:35:25 +01:00
# Plan cleanup on unsuccessful installation.
cleanup() {
[ -z " ${ PREFIX_EXISTS } " ] && rm -rf " $PREFIX "
}
trap cleanup SIGINT
2014-01-10 20:05:01 +09:00
OLDIFS = " $IFS "
IFS = $'\n' scripts =( ` pyenv-hooks install` )
IFS = " $OLDIFS "
for script in " ${ scripts [@] } " ; do source " $script " ; done
2012-08-31 15:32:23 +09:00
2023-01-22 05:49:40 +03:00
COMBINED_STATUS = 0
2026-06-30 06:42:16 +05:30
for i in " ${ !DEFINITIONS[@] } " ; do
DEFINITION = " ${ DEFINITIONS [ $i ] } "
VERSION_ALIAS = " ${ ALIASES [ $i ] } "
2023-01-22 05:49:40 +03:00
STATUS = 0
2022-12-22 18:35:25 +01:00
# Try to resolve a prefix if user indeed gave a prefix.
# We install the version under the resolved name
# and hooks also see the resolved name
2024-07-20 22:06:13 +03:00
DEFINITION = " $( pyenv-latest -f -k " $DEFINITION " ) "
2022-12-22 18:35:25 +01:00
# Set VERSION_NAME from $DEFINITION. Then compute the installation prefix.
2026-06-30 06:42:16 +05:30
# With a `<version>:<alias>` argument, install under the alias instead;
# VERSION_NAME still reflects the real version so version-specific build logic
# (e.g. the bootstrap version detection below) keeps working.
2022-12-22 18:35:25 +01:00
VERSION_NAME = " ${ DEFINITION ##*/ } "
[ -n " $DEBUG " ] && VERSION_NAME = " ${ VERSION_NAME } -debug"
2026-06-30 06:42:16 +05:30
PREFIX = " ${ PYENV_ROOT } /versions/ ${ VERSION_ALIAS :- $VERSION_NAME } "
2022-12-22 18:35:25 +01:00
[ -d " ${ PREFIX } " ] && PREFIX_EXISTS = 1
# If the installation prefix exists, prompt for confirmation unless
# the --force option was specified.
if [ -d " ${ PREFIX } /bin" ] ; then
if [ -z " $FORCE " ] && [ -z " $SKIP_EXISTING " ] ; then
echo "pyenv: $PREFIX already exists" >& 2
read -p "continue with installation? (y/N) "
case " $REPLY " in
y | Y | yes | YES ) ;;
2023-01-22 05:49:40 +03:00
* ) { STATUS = 1; [[ $STATUS -gt $COMBINED_STATUS ]] && COMBINED_STATUS = $STATUS ; } ; continue ;;
2022-12-22 18:35:25 +01:00
esac
elif [ -n " $SKIP_EXISTING " ] ; then
# Since we know the python version is already installed, and are opting to
# not force installation of existing versions, we just `exit 0` here to
# leave things happy
continue
fi
2014-04-09 21:23:46 +09:00
fi
2013-01-31 13:20:59 +09:00
2022-12-22 18:35:25 +01:00
# If PYENV_BUILD_ROOT is set, always pass keep options to python-build.
if [ -n " ${ PYENV_BUILD_ROOT } " ] ; then
2026-06-30 06:42:16 +05:30
export PYTHON_BUILD_BUILD_PATH = " ${ PYENV_BUILD_ROOT } / ${ VERSION_ALIAS :- $VERSION_NAME } "
2022-12-22 18:35:25 +01:00
KEEP = "-k"
fi
2012-08-31 15:32:23 +09:00
2022-12-22 18:35:25 +01:00
# Set PYTHON_BUILD_CACHE_PATH to $PYENV_ROOT/cache, if the directory
# exists and the variable is not already set.
if [ -z " ${ PYTHON_BUILD_CACHE_PATH } " ] && [ -d " ${ PYENV_ROOT } /cache" ] ; then
export PYTHON_BUILD_CACHE_PATH = " ${ PYENV_ROOT } /cache"
fi
2013-01-31 13:20:59 +09:00
2022-12-22 18:35:25 +01:00
if [ -z " ${ PYENV_BOOTSTRAP_VERSION } " ] ; then
case " ${ VERSION_NAME } " in
[ 23] "." * )
# Default PYENV_VERSION to the friendly Python version. (The
# CPython installer requires an existing Python installation to run. An
# unsatisfied local .python-version file can cause the installer to
# fail.)
for version_info in " ${ VERSION_NAME %-dev } " " ${ VERSION_NAME %.* } " " ${ VERSION_NAME %%.* } " ; do
# Anaconda's `curl` doesn't work on platform where `/etc/pki/tls/certs/ca-bundle.crt` isn't available (e.g. Debian)
for version in $( pyenv-whence "python ${ version_info } " 2>/dev/null || true ) ; do
if [[ " ${ version } " != "anaconda" * ]] && [[ " ${ version } " != "miniconda" * ]] ; then
PYENV_BOOTSTRAP_VERSION = " ${ version } "
break 2
fi
done
2015-12-07 05:10:11 +00:00
done
2022-12-22 18:35:25 +01:00
;;
"pypy" *"-dev" | "pypy" *"-src" )
# PyPy/PyPy3 requires existing Python 2.7 to build
if [ -n " ${ PYENV_RPYTHON_VERSION } " ] ; then
PYENV_BOOTSTRAP_VERSION = " ${ PYENV_RPYTHON_VERSION } "
else
for version in $( pyenv-versions --bare | sort -r) ; do
2026-09-21 23:32:12 +03:00
if [[ " ${ version } " = ~ ^( 2\. 7| pypy2\. 7) ]] ; then
2022-12-22 18:35:25 +01:00
PYENV_BOOTSTRAP_VERSION = " $version "
break
fi
done
fi
if [ -n " $PYENV_BOOTSTRAP_VERSION " ] ; then
for dep in pycparser; do
if ! PYENV_VERSION = " $PYENV_BOOTSTRAP_VERSION " pyenv-exec python -c "import ${ dep } " 1>/dev/null 2>& 1; then
echo "pyenv-install: $VERSION_NAME : PyPy requires \` ${ dep } ' in $PYENV_BOOTSTRAP_VERSION to build from source." >& 2
exit 1
fi
done
else
2026-09-21 23:32:12 +03:00
echo "pyenv-install: $VERSION_NAME : PyPy requires Python 2.7 to build from source. You can specify the version to use by setting the PYENV_BOOTSTRAP_VERSION environment variable." >& 2
2022-12-22 18:35:25 +01:00
exit 1
fi
;;
esac
fi
2013-04-19 19:26:11 +09:00
2022-12-22 18:35:25 +01:00
if [ -n " ${ PYENV_BOOTSTRAP_VERSION } " ] ; then
export PYENV_VERSION = " ${ PYENV_BOOTSTRAP_VERSION } "
fi
2013-01-31 13:20:59 +09:00
2022-12-22 18:35:25 +01:00
# Execute `before_install` hooks.
for hook in " ${ before_hooks [@] } " ; do eval " $hook " ; done
# Invoke `python-build` and record the exit status in $STATUS.
2023-01-22 05:49:40 +03:00
python-build $KEEP $VERBOSE $HAS_PATCH $DEBUG " $DEFINITION " " $PREFIX " || \
{ STATUS = $? ; [[ $STATUS -gt $COMBINED_STATUS ]] && COMBINED_STATUS = $STATUS ; }
2022-12-22 18:35:25 +01:00
# Display a more helpful message if the definition wasn't found.
if [ " $STATUS " == "2" ] ; then
{ candidates = " $( definitions " $DEFINITION " ) "
if [ -n " $candidates " ] ; then
echo
echo "The following versions contain \` $DEFINITION ' in the name:"
echo " $candidates " | indent
fi
2013-04-19 19:26:11 +09:00
echo
2022-12-22 18:35:25 +01:00
echo "See all available versions with \`pyenv install --list'."
echo
echo -n "If the version you need is missing, try upgrading pyenv"
2025-12-24 08:00:09 +01:00
declare brew_prefix
if brew_prefix = " $( brew --prefix 2>/dev/null) " && [[ $_PYENV_INSTALL_PREFIX / == " $brew_prefix /" * ]] ; then
2022-12-22 18:35:25 +01:00
printf ":\n\n"
echo " brew update && brew upgrade pyenv"
2025-12-24 08:00:09 +01:00
elif [ -d " ${ _PYENV_INSTALL_PREFIX } /.git" ] ; then
2022-12-22 18:35:25 +01:00
printf ":\n\n"
2025-12-24 08:00:09 +01:00
echo " cd ${ _PYENV_INSTALL_PREFIX } && git pull && cd -"
2022-12-22 18:35:25 +01:00
else
printf ".\n"
fi
} >& 2
fi
2013-04-19 19:26:11 +09:00
2022-12-22 18:35:25 +01:00
# Execute `after_install` hooks.
for hook in " ${ after_hooks [@] } " ; do eval " $hook " ; done
2013-01-31 13:20:59 +09:00
2022-12-22 18:35:25 +01:00
# Run `pyenv-rehash` after a successful installation.
2023-01-22 05:49:40 +03:00
if [[ $STATUS -eq 0 ]] ; then
2022-12-22 18:35:25 +01:00
pyenv-rehash
else
cleanup
2023-01-22 05:49:40 +03:00
break
2022-12-22 18:35:25 +01:00
fi
done
2013-01-31 13:20:59 +09:00
2023-01-22 05:49:40 +03:00
exit " ${ COMBINED_STATUS } "