forked from matthewfeickert/Docker-Python3-Ubuntu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_python.sh
125 lines (109 loc) · 3.84 KB
/
install_python.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env bash
set -e
# This is being run as root and so sudo is not needed
function download_cpython () {
# 1: the version tag
printf "\n### Downloading CPython source as Python-%s.tgz\n" "${1}"
wget "https://www.python.org/ftp/python/${1}/Python-${1}.tgz" &> /dev/null
tar -xvzf "Python-${1}.tgz" > /dev/null
rm "Python-${1}.tgz"
}
function set_num_processors {
# Set the number of processors used for build
# to be 1 less than are available
if [[ -f "$(command -v nproc)" ]]; then
NPROC="$(nproc)"
else
NPROC="$(grep -c '^processor' /proc/cpuinfo)"
fi
echo "$((NPROC - 1))"
}
function build_cpython () {
# 1: the prefix to be passed to configure
# c.f. https://docs.python.org/3/using/unix.html#python-related-paths-and-files
# 2: the Python version being built
# https://docs.python.org/3/using/unix.html#building-python
# https://github.com/python/cpython/blob/3.8/README.rst
# https://github.com/python/cpython/blob/3.7/README.rst
# https://github.com/python/cpython/blob/3.6/README.rst
printf "\n### ./configure --help\n"
./configure --help
printf "\n### ./configure\n"
if [[ "${2}" > "3.7.0" ]]; then
# --with-threads is removed in Python 3.7 (threading already on)
./configure --prefix="${1}" \
--exec_prefix="${1}" \
--with-ensurepip \
--enable-optimizations \
--with-lto \
--enable-loadable-sqlite-extensions \
--enable-ipv6
else
./configure --prefix="${1}" \
--exec_prefix="${1}" \
--with-ensurepip \
--enable-optimizations \
--with-lto \
--enable-loadable-sqlite-extensions \
--enable-ipv6 \
--with-threads
fi
printf "\n### make -j%s\n" "${NPROC}"
make -j"${NPROC}"
printf "\n### make -j%s test\n" "${NPROC}"
make -j"${NPROC}" test
printf "\n### make install\n"
make install
}
function update_pip {
# Update pip, setuptools, and wheel
if [[ "$(id -u)" -eq 0 ]]; then
# If root
printf "\n### pip3 install --upgrade --no-cache-dir pip setuptools wheel\n"
pip3 install --upgrade --no-cache-dir pip setuptools wheel
else
printf "\n### pip3 install --user --upgrade --no-cache-dir pip setuptools wheel\n"
pip3 install --user --upgrade --no-cache-dir pip setuptools wheel
fi
}
function symlink_python_to_python3 {
local python_version
python_version="$(python3 --version)"
local which_python
which_python="$(command -v python3)${python_version:8:-2}"
local which_pip
which_pip="$(command -v pip3)"
# symlink python to python3
printf "\n### ln -s -f %s %s\n" "${which_python}" "${which_python::-3}"
ln -s -f "${which_python}" "${which_python::-3}"
# symlink pip to pip3 if no pip exists or it is a different version than pip3
if [[ -n "$(command -v pip)" ]]; then
if [[ "$(pip --version)" = "$(pip3 --version)" ]]; then
return 0
fi
fi
printf "\n### ln -s -f %s %s\n" "${which_pip}" "${which_pip::-1}"
ln -s -f "${which_pip}" "${which_pip::-1}"
return 0
}
function main() {
# 1: the Python version tag
# 2: bool of if should symlink python and pip to python3 versions
PYTHON_VERSION_TAG=3.8.7
LINK_PYTHON_TO_PYTHON3=0 # By default don't link so as to reserve python for Python 2
if [[ $# -gt 0 ]]; then
PYTHON_VERSION_TAG="${1}"
if [[ $# -gt 1 ]]; then
LINK_PYTHON_TO_PYTHON3="${2}"
fi
fi
NPROC="$(set_num_processors)"
download_cpython "${PYTHON_VERSION_TAG}"
cd Python-"${PYTHON_VERSION_TAG}"
build_cpython /usr "${PYTHON_VERSION_TAG}"
update_pip
if [[ "${LINK_PYTHON_TO_PYTHON3}" -eq 1 ]]; then
symlink_python_to_python3
fi
}
main "$@" || exit 1