Linux版本:CentOS-7.8-x86_64-Minimal-2003
操作用户:root
(1)依赖包安装:
如果是Linux的minimal系统,需要安装:
yum install -y vim wget tftp lrzsz bzip2 zip unzip net-tools bind-utils traceroute tcpdump telnet tree mlocate bash-completion rsync readline readline-devel gdisk
编译安装需要的包:
yum install -y make.x86_64 gcc gcc-c++ zlib zlib-devel openssl-devel
Python需要的包:
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
(2)下载 python3.9:
下载命令:
wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz
(3)创建安装目录:
我们在/usr/local下面创建目录python3.9,命令如下:
mkdir -p /usr/local/python3.9
根据需要也可以创建其他目录。
(4)编译安装:
解压缩下载的源码包:
tar -zxvf Python-3.9.0.tgz
编译配置:
./configure --prefix=/usr/local/python3.9
编译并安装:
make && make install
检测安装是否成功,执行以下命令:
/usr/local/python3.9/bin/python3.9
执行结果如下即成功:
- Python 3.9.1 (default, Feb 18 2021, 08:36:51)
- [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
- Type "help", "copyright", "credits" or "license" for more information.
- >>> print('Hello world');
- Hello world
- >>>
(5)配置root用户环境变量:
编辑root用户下的.bashrc文件,追加以下内容:
- export PYTHON_HOME=/usr/local/python3.9
- export PATH=${PYTHON_HOME}/bin:$PATH
执行以下命令使以上变更生效:
source /root/.bashrc
(6)创建软连接:
为了方便,我们习惯使用python命令,而不是python3.9,所以我们要在/usr/bin/python目录下创建一个软件连接。
ln -s /usr/local/python3.9/bin/python3.9 /usr/bin/python
执行python命令测试一下:
- # python
- Python 3.9.1 (default, Feb 18 2021, 13:54:12)
- [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
- Type "help", "copyright", "credits" or "license" for more information.
- >>>
结束。