Linux下源码安装Python3.7.6

查看Linux服务器信息

  • 使用lsb_release -a命令查看服务器信息,如出现“未找到命令”的错误,需要安装对应的包
[root@yz_server_001 ~]# lsb_release -a
-bash: lsb_release: 未找到命令
  • 安装redhat-lsb软件包
[root@yz_server_001 ~]# yum install -y redhat-lsb
  • 再次执行lsb_release -a
[root@yz_server_001 ~]# lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.6.1810 (Core)
Release:    7.6.1810
Codename:   Core

安装依赖包

  • 安装gcc编译器
# 执行gcc --version命令,如果有就不用安装,没有就执行以下安装命令
[root@yz_server_001 ~]# yum -y install gcc
  • 安装其它依赖包
[root@yz_server_001 ~]# 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-devel

下载Python3.7.6源码包

[root@yz_server_001 ~]# wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tgz

解压及安装

# 解压安装包
[root@yz_server_001 ~]# tar zxvf Python-3.7.6.tgz

# 执行安装
[root@yz_server_001 ~]# cd python-3.7.6

[root@yz_server_001 ~]# ./configure --prefix=/usr/local/python3
[root@yz_server_001 ~]# make && make install

建立软连接

[root@yz_server_001 ~]# ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3
[root@yz_server_001 ~]# ln -s /usr/local/python3/bin/pip3.7 /usr/bin/pip3

测试

[root@yz_server_001 ~]# which python3
/usr/bin/python3
[root@yz_server_001 ~]# python3
Python 3.7.6 (default, Feb  9 2020, 20:11:28)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

修改Linux默认python版本

修改默认的版本,并不是删除你不需要的版本,因为系统的许多底层是依赖python的,删除后可能会导致系统无法正常运行。
只需要删除/usr/bin目录下的python link文件,然后再建立新的链接关系即可:

[root@yz_server_001]# python
Python 2.7.5 (default, Aug  7 2019, 00:51:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[root@yz_server_001]# ll /usr/bin/python*
lrwxrwxrwx 1 root root    7 Nov  5 23:09 /usr/bin/python -> python2
lrwxrwxrwx 1 root root   14 Feb  2 21:24 /usr/bin/python-config -> python2-config
lrwxrwxrwx 1 root root    9 Nov  5 23:09 /usr/bin/python2 -> python2.7
lrwxrwxrwx 1 root root   16 Feb  2 21:24 /usr/bin/python2-config -> python2.7-config
-rwxr-xr-x 1 root root 7216 Aug  7  2019 /usr/bin/python2.7
-rwxr-xr-x 1 root root 1835 Aug  7  2019 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root   32 Feb  9 20:13 /usr/bin/python3 -> /usr/local/python3/bin/python3.7
[root@yz_server_001]# cd /usr/bin/
[root@yz_server_001 bin]# rm -rf /usr/bin/python
[root@yz_server_001 bin]# ln -s /usr/bin/python3  /usr/bin/python
[root@yz_server_001 bin]# ll /usr/bin/python*
lrwxrwxrwx 1 root root   16 Feb 22 10:27 /usr/bin/python -> /usr/bin/python3
lrwxrwxrwx 1 root root   14 Feb  2 21:24 /usr/bin/python-config -> python2-config
lrwxrwxrwx 1 root root    9 Nov  5 23:09 /usr/bin/python2 -> python2.7
lrwxrwxrwx 1 root root   16 Feb  2 21:24 /usr/bin/python2-config -> python2.7-config
-rwxr-xr-x 1 root root 7216 Aug  7  2019 /usr/bin/python2.7
-rwxr-xr-x 1 root root 1835 Aug  7  2019 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root   32 Feb  9 20:13 /usr/bin/python3 -> /usr/local/python3/bin/python3.7
[root@yz_server_001]# python
Python 3.7.6 (default, Feb  9 2020, 20:11:28)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

可能存在的问题

  • make没有报错,但是在编译的时候报 ModuleNotFoundError: No module named ‘_ctypes’ 错误
    解决方法:
yum -y install libffi-devel  tk-devel

然后重新执行make install

pip3 install package的过程如出现这个错误,也可以用这个方法解决。

你可能感兴趣的:(Linux下源码安装Python3.7.6)