-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_python_debian.sh
More file actions
172 lines (155 loc) · 4.86 KB
/
install_python_debian.sh
File metadata and controls
172 lines (155 loc) · 4.86 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/bash
# Check if user is root
[ $(id -u) != "0" ] && {
echo "${CFAILURE}Error: You must be root to run this script${CEND}"
exit 1
}
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
echo=echo
for cmd in echo /bin/echo; do
$cmd >/dev/null 2>&1 || continue
if ! $cmd -e "" | grep -qE '^-e'; then
echo=$cmd
break
fi
done
CSI=$($echo -e "\033[")
CEND="${CSI}0m"
CDGREEN="${CSI}32m"
CRED="${CSI}1;31m"
CGREEN="${CSI}1;32m"
CYELLOW="${CSI}1;33m"
CBLUE="${CSI}1;34m"
CMAGENTA="${CSI}1;35m"
CCYAN="${CSI}1;36m"
CSUCCESS="$CDGREEN"
CFAILURE="$CRED"
CQUESTION="$CMAGENTA"
CWARNING="$CYELLOW"
CMSG="$CCYAN"
name="Python"
default_version="3.7.12"
command -v curl >/dev/null 2>&1 || { apt-get install -y curl; }
while :; do
echo
read -e -p "Please enter the version number you need (Default version: ${default_version}): " version
if [ "$version" = "" ]; then
version="${default_version}"
fi
pyfile="${name}-${version}.tgz"
src_url=https://www.python.org/ftp/python/${version}/${pyfile}
if [[ $(echo $(curl -sIL -w "%{http_code}" -o /dev/null ${src_url})) != '200' ]]; then
echo "${CWARNING}The python version is wrong, please re-enter${CEND}"
else
break
fi
done
while :; do
echo
read -e -p "Do you want to use Chinese pypi? [y/n]: " switch_pypi
if [[ ! ${switch_pypi} =~ ^[y,n]$ ]]; then
echo "${CWARNING}Input error! Please only input 'y' or 'n'${CEND}"
else
break
fi
done
check_ver=$(echo $version | awk '{printf ("%3.1f\n",$1)}')
main_version=${check_ver//./}
dir="/usr/local/python${main_version}"
if [ -d ${dir} ]; then
while :; do
echo
read -e -p "Dir ${dir} [found]. Python${main_version} already installed! Whether to remove the existing directory and reinstall python${main_version}? [y]: " continue_flag
if [[ ! ${continue_flag} == 'y' ]]; then
echo "${CWARNING}Input error! Please only input 'y'${CEND}"
kill -9 $$
exit 1
else
break
fi
done
echo "${CWARNING} Python${main_version} [no found] ${CEND}"
fi
Install_Python() {
# start Time
startTime=$(date +%s)
pkgList="gcc wget make dialog libaugeas0 augeas-lenses zlib1g-dev libssl-dev libffi-dev ca-certificates"
for Package in ${pkgList}; do
apt-get -y install ${Package}
done
if [ -d ${dir} ]; then
rm -rf /usr/local/python${main_version}
rm -rf /usr/bin/python${main_version}
rm -rf /usr/bin/pip${main_version}
else
echo "${CWARNING} Python${main_version} [no found] ${CEND}"
fi
if [ -s ./${pyfile} ]; then
echo "${CWARNING} ${pyfile} [found]${CEND}"
rm -f ./${pyfile}
fi
if [ -d ./${name}-${version} ]; then
echo "${CWARNING} Dir ${name}-${version} [found]${CEND}"
rm -rf ./${name}-${version}
fi
echo "Download python..."
wget --limit-rate=100M --tries=6 -c ${src_url}
sleep 1
if [ ! -s ./${pyfile} ]; then
echo "${CFAILURE}Auto download failed! You can manually download ${src_url} into the current directory [${PWD}].${CEND}"
kill -9 $$
exit 1
fi
tar zxf ./${pyfile}
pushd ./${name}-${version} >/dev/null
./configure --prefix=${dir}
#./configure --prefix=${dir} --enable-optimizations --with-ssl
if [ $? -ne 0 ]; then
popd >/dev/null
echo "${CFAILURE}Python install failed! ${CEND}"
kill -9 $$
exit 1
fi
make && make install
if [ $? -ne 0 ]; then
popd >/dev/null
echo "${CFAILURE}Python install failed! ${CEND}"
kill -9 $$
exit 1
fi
popd >/dev/null
# eg: link python37 to /usr/bin/
ln -s ${dir}/bin/python${check_ver} /usr/bin/python${main_version}
ln -s ${dir}/bin/pip${check_ver} /usr/bin/pip${main_version}
if [ -e "${dir}/bin/python${check_ver}" ]; then
echo "${CSUCCESS}Python ${version} installed successfully!${CEND}"
rm -rf ./${name}-${version}
fi
if [ "${switch_pypi}" == 'y' ]; then
if [ ! -e "/root/.pip/pip.conf" ]; then
[ ! -d "/root/.pip" ] && mkdir /root/.pip
cat >/root/.pip/pip.conf <<EOF
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=pypi.tuna.tsinghua.edu.cn
disable-pip-version-check = true
timeout = 6000
EOF
else
echo "${CWARNING}Pip file is [found]${CEND}"
fi
fi
pip${main_version} install --upgrade pip
echo -e "${CSUCCESS} \nPython ${version} Installed Successfully!${CEND}"
echo -e "\nInstalled Python and pip version is ... "
python${main_version} -V && pip${main_version} -V
endTime=$(date +%s)
((installTime = ($endTime - $startTime) / 60))
echo "####################Congratulations########################"
echo "Total Install Time: ${CQUESTION}${installTime}${CEND} minutes"
echo -e "\n$(printf "%-32s" "Python install dir":)${CMSG}${dir}${CEND}"
echo -e "\n$(printf "%-32s" "Python bin file":)${CMSG}/usr/bin/python${main_version}${CEND}"
echo -e "\n$(printf "%-32s" "Pip bin file":)${CMSG}/usr/bin/pip${main_version}${CEND}"
}
Install_Python 2>&1 | tee -a ./install_python.log