Linux上安装python3

笔者所用的Linux是CentOS7系统,其他的系统类比即可,比如在ubuntu下把yum换成apt-get即可.
CentOS7 默认已经安装了Python2,查看系统版本:

[root@harvey ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core) 

默认已安装的Python版本如下

[root@harvey ~]# python --version
Python 2.7.5

由于某些原因我们需要在服务器上安装python3,但是一些重要的系统命令是基于python2的,所以我们不能把python2卸载,以下介绍如何在centos上安装python3。6。

方法是基于编译python3源码安装。

下载python3.6包

wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz

安装依赖环境

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

创建安装目录

python2的安装目录位于/usr/bin/python,于是我们在此创建一个python3的目录:

mkdir -p /usr/local/python3

解压编译安装python3

tar -xzf Python-3.6.5.tgz
cd Python-3.6.5
./configure --prefix=/usr/local/python3
make && make install

如果缺少什么基础类库需要自行安装,比如gcc库啥的,这个时候yum install gcc即可。

编译安装同时安装了setuptools和pip,编译完成末尾提示如下:

Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-9.0.3 setuptools-39.0.1

配置环境变量

编辑profile文件

vim /etc/profile

底部增加如下配置

export PYTHON=/usr/local/python3
export PATH=$PATH:$PYTHON/bin

使配置立即生效

source /etc/profile

验证安装结果

[root@VM_51_22_centos Python-3.6.5]# python --version
Python 2.7.5
[root@VM_51_22_centos Python-3.6.5]# python3 --version
Python 3.6.5
[root@VM_51_22_centos Python-3.6.5]# pip3 -V
pip 9.0.3 from /usr/local/python3/lib/python3.6/site-packages (python 3.6)

安装python3库

用pip3即可

pip3 install flask

检验

[root@VM_51_22_centos Python-3.6.5]# python3
Python 3.6.5 (default, May  1 2018, 20:37:37) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask

参考:
https://blog.csdn.net/HG_Harvey/article/details/79889476
https://gaojie.me/post/97.html

1 + 2 =
快来做第一个评论的人吧~