最近服务器需要安装python3.7环境,centos7.3默认最高可以安装python3.6版本,3.7版本以上需要另行安装依赖环境,测试安装后,发现通过以下步骤可以较快完成安装,以3.9.0为例:
准备:首先需要下载python3.9.0的安装包
一、安装python3.9所需依赖包
依赖包主要有:zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
可以通过yum进行安装,如下:
[root@localhost ~]# yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-develer
二、安装python3.9.0
1、将已经下载的好的python3.9.0的tar包上传至服务器
2、新建一个文件夹,用于存放python3.9
[root@localhost local]# mkdir python3.9
3、将Python-3.9.0.tgz包进行解压
[root@localhost local]# tar -zxvf Python-3.9.0.tgz
4、编译安装(本例将其安装至/usr/local/python3.9下)
[root@localhost local]# cd Python-3.9.0/
[root@localhost Python-3.9.0]# ./configure --prefix=/usr/local/python3.9
[root@localhost Python-3.9.0]# make && make install
安装成功后显示如下字节
5、建立软链接
软链接类似Windows下的快捷方式,正常应该添加python3.9的环境变量,但因为系统默认已经安装python2.7,所以只建立一个软链接,如果不想建立软链接,也可以命令别名,在~/.bashrc文件里加 alias python3='具体路径' 。本文以增加软链接为例:
[root@localhost Python-3.9.0]# ln -s /usr/local/python3.9/bin/python3.9 /usr/bin/python3
[root@localhost Python-3.9.0]# ln -s /usr/local/python3.9/bin/pip3.9 /usr/bin/pip3
6、测试
[root@localhost Python-3.9.0]# python3
Python 3.9.0 (default, Oct 20 2021, 16:17:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
至此,安装完成