CentOS系统安装Python

安装环境:CentOS_8.0_64位
登录用户:root
python版本:3.7.0

wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
tar -zxvf Python-3.7.0.tgz
cd Python-3.7.0
./configure
make
make install

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@vultr Python-3.7.0]# ./configure
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.7... no
checking for python3... python3
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/opt/Python-3.7.0':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

出现上面的错误是因为没有安装GCC模块,执行如下命令安装:

yum install gcc

1
2
[root@vultr Python-3.7.0]# make
-bash: make: command not found

发现没有安装make指令,执行如下命令安装:

yum install make

1
2
3
4
5
6
7
8
……

File "/opt/Python-3.7.0/Lib/ensurepip/__init__.py", line 117, in _bootstrap
return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
File "/opt/Python-3.7.0/Lib/ensurepip/__init__.py", line 27, in _run_pip
import pip._internal
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [Makefile:1122: install] Error 1

发现没有zlib模块,执行如下命令安装:

yum -y install zlib*

1
2
3
4
5
6
7
8
9
10
……
File "/tmp/tmpccibitju/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/__init__.py", line 42, in <module>
File "/tmp/tmpccibitju/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/cmdoptions.py", line 16, in <module>
File "/tmp/tmpccibitju/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/index.py", line 25, in <module>
File "/tmp/tmpccibitju/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/download.py", line 39, in <module>
File "/tmp/tmpccibitju/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/utils/glibc.py", line 3, in <module>
File "/opt/Python-3.7.0/Lib/ctypes/__init__.py", line 7, in <module>
from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'
make: *** [Makefile:1122: install] Error 1

出现上面错误是缺少 libffi-devel,执行如下命令安装:

yum -y install libffi-devel

配置环境变量
cd /usr/local/bin/
可以看到python3.7和pip3.7都安装好了
下一步创建/bin目录下的软链接
ln -s /usr/local/bin/python3.7 /usr/bin/python
ln -s /usr/local/bin/pip3.7 /usr/bin/pip

1
2
3
4
[root@vultr opt]# python -V
Python 3.7.0
[root@vultr opt]# pip -V
pip 10.0.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

至此,python就安装成功了。

后面在使用的时候又出现了报错,需要安装openssl模块。

1
2
3
4
5
  Could not fetch URL https://pypi.org/simple/shadowsocks/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/shadowsocks/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
Could not find a version that satisfies the requirement shadowsocks (from versions: )
No matching distribution found for shadowsocks
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping

yum -y install openssl-devel

./configure –with-openssl=/usr/bin/openssl

------ 本文完 ------