Linux Python3安装(服务器架设篇)

很多同学使用Python的时候会发现他们Linux上的自带的Python基本都是Python2.X版本,鉴于Python2.X在不久就会停止维护,而Python3又一般没有在Linux中内置,所以自己学会安装Python3.X必不可少。Cent os7下默认系统自带Python2.7的版本,这个版本被系统很多程序所依赖,所以不建议删除,如果使用最新的Python3那么我们需要自己手动编译Python3源代码安装,并让Python3和Python2共存。

安装Python3

环境 版本
RHEL/CentOS 7.x
Python 3.6.x

依赖包

1
2
3
4
5
6
7
#先升级系统已安装的软件
[root@thinkcent ext_files]# sudo yum update -y
#安装Python3的依赖软件包
[root@thinkcent ext_files]# yum install zlib zlib-devel bzip2-devel openssl openssl-devel libffi-devel ncurses-devel xz-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel expat-devel

#安装development tools,可以不安装,执行前两个指令后其实大部分情况下就可以了,如果编译过程保持提升环境依赖问题就可以再执行这条命令尝试解决
[root@thinkcent ext_files]# sudo yum groupinstall "development tools"

编译安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#解压
[thinktik@thinkcent install]$ tar -zxvf Python-3.6.6.tgz

[thinktik@thinkcent install]$ ls
java8 nodejs8 Python-3.6.6.tgz
nexus-3.10.0-04-unix.tar.gz python3 redis-4.0.9.tar.gz
nginx-1.12.2.tar.gz Python-3.6.6 zookeeper-3.4.10.tar.gz


[thinktik@thinkcent install]$ mv Python-3.6.6 Python36
[thinktik@thinkcent install]$ ls
java8 nginx-1.12.2.tar.gz python3 Python-3.6.6.tgz zookeeper-3.4.10.tar.gz
nexus-3.10.0-04-unix.tar.gz nodejs8 Python36 redis-4.0.9.tar.gz
# 进入目录
[thinktik@thinkcent install]$ cd Python36/
[thinktik@thinkcent Python36]$ ls
aclocal.m4 configure.ac install-sh Makefile.pre.in Parser pyconfig.h.in Tools
config.guess Doc Lib Misc PC Python
config.sub Grammar LICENSE Modules PCbuild README.rst
configure Include Mac Objects Programs setup.py
# 配置,制定安装目录为/home/thinktik/install/python3,大家可以按自己要求修改
[thinktik@thinkcent Python36]$ ./configure --prefix=/home/thinktik/install/python3 --enable-optimizations

#配置文件设置完成,如下面的日志,接下来就可以编译了,编译会花一段时间,等待就好
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
config.status: pyconfig.h is unchanged
creating Modules/Setup
creating Modules/Setup.local
creating Makefile
[thinktik@thinkcent Python36]$ make

# make完成,如下面的日志,接下来就可以安装了,下面这句话只是说modules会被编译安装的文件替代,没什么问题,我特意记录下来给大家参考
The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
atexit pwd time
running build_scripts
copying and adjusting /home/thinktik/install/Python36/Tools/scripts/pydoc3 -> build/scripts-3.6
copying and adjusting /home/thinktik/install/Python36/Tools/scripts/idle3 -> build/scripts-3.6
copying and adjusting /home/thinktik/install/Python36/Tools/scripts/2to3 -> build/scripts-3.6
copying and adjusting /home/thinktik/install/Python36/Tools/scripts/pyvenv -> build/scripts-3.6
changing mode of build/scripts-3.6/pydoc3 from 664 to 775
changing mode of build/scripts-3.6/idle3 from 664 to 775
changing mode of build/scripts-3.6/2to3 from 664 to 775
changing mode of build/scripts-3.6/pyvenv from 664 to 775
renaming build/scripts-3.6/pydoc3 to build/scripts-3.6/pydoc3.6
renaming build/scripts-3.6/idle3 to build/scripts-3.6/idle3.6
renaming build/scripts-3.6/2to3 to build/scripts-3.6/2to3-3.6
renaming build/scripts-3.6/pyvenv to build/scripts-3.6/pyvenv-3.6
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -fprofile-use -fprofile-correction -I. -I./Include -DPy_BUILD_CORE -o Programs/_testembed.o ./Programs/_testembed.c
gcc -pthread -Xlinker -export-dynamic -o Programs/_testembed Programs/_testembed.o libpython3.6m.a -lpthread -ldl -lutil -lm
make[2]: Leaving directory `/home/thinktik/install/Python36'
make[1]: Leaving directory `/home/thinktik/install/Python36'
make profile-removal
make[1]: Entering directory `/home/thinktik/install/Python36'
find . -name '*.gc??' -exec rm -f {} ';'
find . -name '*.profclang?' -exec rm -f {} ';'
find . -name '*.dyn' -exec rm -f {} ';'
rm -f /home/thinktik/install/Python36/coverage.info
rm -rf /home/thinktik/install/Python36/lcov-report
make[1]: Leaving directory `/home/thinktik/install/Python36'

# 开始make install
[thinktik@thinkcent Python36]$ make install

# 日志显示编译成功
Looking in links: /tmp/tmp_wbmtfs_
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-10.0.1 setuptools-39.0.1

[thinktik@thinkcent install]$ ls
java8 nginx-1.12.2.tar.gz python3 Python-3.6.6.tgz zookeeper-3.4.10.tar.gz
nexus-3.10.0-04-unix.tar.gz nodejs8 Python36 redis-4.0.9.tar.gz

# 我们看到我们指定的目录下已经有了安装后的二进制文件
[thinktik@thinkcent install]$ cd python3/
[thinktik@thinkcent python3]$ ls
bin include lib share

配置环境变量

1
2
3
4
5
6
7
8
# 修改文件
[thinktik@thinkcent python3]$ vim ~/.bash_profile
# 立即生效
[thinktik@thinkcent python3]$ source ~/.bash_profile
# 成功后看到出现了python3.x命令
[thinktik@thinkcent python3]$ python
python python2.7 python3.6 python3.6m python3-config
python2 python3 python3.6-config python3.6m-config

附上配置文件的详情:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

JAVA_HOME=/home/thinktik/install/java8
NODE_HOME=/home/thinktik/install/nodejs8
PYTHON_HOME=/home/thinktik/install/python3

export PATH=$PATH:$PYTHON_HOME/bin
export PATH=$PATH:$NODE_HOME/bin
export PATH=$PATH:$JAVA_HOME/bin

export PATH