Linux安装Python3与Python2共存

linux自带了Python2而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到。

输入python命令,查看可以得知是Python2.7.5版本

[root@GuangBo ~]# python
Python 2.7.5 (default, Jun 20 2019, 20:27:34) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

输入which python可以查看python所在目录

[root@GuangBo ~]# which python
/usr/bin/python

下面开始安装Python3

首先安装依赖包

yum -y groupinstall "Development tools"
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

创建Python目录

mkdir python3
cd python3/

下载Python3.7.4

wget  https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz

TP:如果速度不够快,可以直接去官网下载,利用WinSCP等软件传到服务器上指定位置,我的存放目录是/home/<个人目录>/python3

然后解压压缩包,进入该目录,安装Python3

tar -zxvf Python-3.7.4
cd Python-3.7.4/
./configure --prefix=/home/GuangBo/python3
make && make install

出错了~

ModuleNotFoundError: No module named '_ctypes'
make: *** [install] 错误 1

3.7版本需要一个新的包libffi-devel,安装此包之后再次进行编译安装即可。

yum install libffi-devel -y
make install

安装成功。创建软链

# ln -s /home/GuangBo/python3/bin/python3 /usr/bin/python3
# ln -s /home/GuangBo/python3/bin/pip3 /usr/bin/pip3

到此结束!~

    原文作者:GuangBo
    原文地址: https://segmentfault.com/a/1190000019880623
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞