Linux 编译安装Python
下载
下载指定版本(我这里下载的是Python3.6.8),使用淘宝镜像源地址下(https://npmmirror.com/mirrors)
[root@centos-7 tmp]# wget https://npmmirror.com/mirrors/python/3.6.8/Python-3.6.8.tgz
--2021-11-12 14:43:56-- https://npmmirror.com/mirrors/python/3.6.8/Python-3.6.8.tgz
Resolving npmmirror.com (npmmirror.com)... 47.96.233.62
Connecting to npmmirror.com (npmmirror.com)|47.96.233.62|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://oss.npmmirror.com/dist/python/3.6.8/Python-3.6.8.tgz [following]
--2021-11-12 14:43:56-- https://oss.npmmirror.com/dist/python/3.6.8/Python-3.6.8.tgz
Resolving oss.npmmirror.com (oss.npmmirror.com)... 119.96.71.242, 119.96.71.248, 119.96.71.243, ...
Connecting to oss.npmmirror.com (oss.npmmirror.com)|119.96.71.242|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 23010188 (22M) [application/octet-stream]
Saving to: ‘Python-3.6.8.tgz’
100%[======================================================================================================================================================================================================>] 23,010,188 11.2MB/s in 2.0s
2021-11-12 14:43:58 (11.2 MB/s) - ‘Python-3.6.8.tgz’ saved [23010188/23010188]
安装依赖包
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
...
...
Complete!
安装编译器gcc
yum -y install gcc
...
...
Complete!
解压
tar -zxvf Python-3.6.8.tgz
安装
配置安装目录
cd Python-3.6.8
./configure --prefix=/usr/local/python3
编译源码
make
执行源码安装
make install
...
...
Requirement already up-to-date: setuptools in /usr/local/python3/lib/python3.6/site-packages (40.6.2)
Requirement already up-to-date: pip in /usr/local/python3/lib/python3.6/site-packages (18.1)
静静等待安装完成,可以看出setuptools与pip已经安装完成,接下来就是配置环境了。
创建软连接
先看一下Linux系统自带python
[root@centos-7 local]# python -V
Python 2.7.5
自带的python为2.7版本,而且该版本在yum命令中会使用到,因此我们最好不去修改该python。
看一下当前python软连接数
[root@centos-7 local]# ll /usr/bin/python*
lrwxrwxrwx. 1 root root 7 Aug 11 01:59 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root 9 Nov 10 14:53 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7144 Nov 17 2020 /usr/bin/python2.7
我们只需要在软连接中加一个python3,用来调用即可
[root@centos-7 local]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@centos-7 local]# ll /usr/bin/python*
lrwxrwxrwx. 1 root root 7 Aug 11 01:59 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root 9 Nov 10 14:53 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7144 Nov 17 2020 /usr/bin/python2.7
lrwxrwxrwx. 1 root root 30 Nov 12 15:11 /usr/bin/python3 -> /usr/local/python3/bin/python3
可以看出多了一个python3,对应的是我们安装python3的地址
验证Python
[root@centos-7 local]# python3 -V
Python 3.6.8
pip3也需要创建一条软连接用来访问
[root@centos-7 local]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
[root@centos-7 local]# ll /usr/bin/pip*
-rwxr-xr-x. 1 root root 282 Sep 3 2020 /usr/bin/pip
-rwxr-xr-x. 1 root root 284 Sep 3 2020 /usr/bin/pip2
-rwxr-xr-x. 1 root root 288 Sep 3 2020 /usr/bin/pip2.7
lrwxrwxrwx. 1 root root 27 Nov 12 15:16 /usr/bin/pip3 -> /usr/local/python3/bin/pip3
验证pip3
[root@centos-7 local]# pip3 -V
pip 18.1 from /usr/local/python3/lib/python3.6/site-packages/pip (python 3.6)
至此python3即安装成功,需要使用python3,pip3来访问。配置pip镜像源地址和Windows一样,这里就不做介绍了。
个人感觉设置软连接后就不需要设置环境变量了,因为软连接的地址就是在环境变量path下,相当于在环境变量目录下创建了一个快捷访问方式。