一、环境部署-源码安装
1.安装依赖包
yum groupinstall "Development Tools" -y #开发工具包
yum -y install zlib-devel bzip2-devel openssl-devel sqlite-devel readline-devel libffi-devel
#groupinstall和install区别
yum install :安装单个软件
yum grouplist :查看这个软件的所有软件包
yum groupinstall :安装多个软件,安装这个软件的所有依赖的软件包,yum groupinstall安装的时候软件包名必须加双引号
2.下载python源码包
wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tar.xz
3.解压
[root@localhost ~]# tar -xf Python-3.7.6.tar.xz
[root@localhost ~]# ll
总用量 16852
drwxr-xr-x. 18 501 501 4096 12月 19 2019 Python-3.7.6
-rw-r--r--. 1 root root 17246360 12月 19 2019 Python-3.7.6.tar.xz
4.进入加压后的目录修改Modules文件夹下的Setup.dist文件
[root@localhost ~]# cd Python-3.7.6/
[root@localhost Python-3.7.6]# sed -ri 's/^#readline/readline/' Modules/Setup.dist
[root@localhost Python-3.7.6]# sed -ri 's/^#(SSL=)/\1/' Modules/Setup.dist
[root@localhost Python-3.7.6]# sed -ri 's/^#(_ssl)/\1/' Modules/Setup.dist
[root@localhost Python-3.7.6]# sed -ri 's/^#([\t]*-DUSE)/\1/' Modules/Setup.dist
[root@localhost Python-3.7.6]# sed -ri 's/^#([\t]*-L\$\(SSL\))/\1/' Modules/Setup.dist
[root@localhost Python-3.7.6]#
5.开始编译安装
[root@localhost Python-3.7.6]# ./configure --enable-shared
[root@localhost Python-3.7.6]# make -j 2 && make install
--enable-shared 指定安装共享库,共享库在使用其他需调用python的软件时会用到,比如使用mod_wgsi连接Apache与python时需要
#-j参数启动两个进程进行编译
二、配置环境
注意:这个时候启动python3是出错的,因为之前预编译阶段指定了共享库
[root@localhost Python-3.7.6]# python3
python3: error while loading shared libraries: libpython3.7m.so.1.0: cannot open shared object file: No such file or directory
[root@localhost Python-3.7.6]#
需要执行如下命令配置环境
[root@localhost Python-3.7.6]# cmdl='export LD_LIBRARY_PATH='
[root@localhost Python-3.7.6]# cmd2='$LD_LIBRARY_PATH:/usr/local/lib'
[root@localhost Python-3.7.6]# file="/etc/profile.d/python3_lib.sh"
[root@localhost Python-3.7.6]# echo "${cmd1}${cmd2}">$file
[root@localhost Python-3.7.6]# cat /etc/profile.d/python3_lib.sh
$LD_LIBRARY_PATH:/usr/local/lib
[root@localhost Python-3.7.6]#
[root@localhost Python-3.7.6]# path="/usr/local/lib"
[root@localhost Python-3.7.6]# file2="/etc/ld.so.conf.d/python3.conf"
[root@localhost Python-3.7.6]# echo ${path} > $file2
[root@localhost Python-3.7.6]# cat /etc/ld.so.conf.d/python3.conf
/usr/local/lib
[root@localhost Python-3.7.6]#
使配置文件生效
[root@localhost lib]# ldconfig
[root@localhost lib]# source /etc/profile #这一步我报错了,但不影响我启动python3
[root@localhost lib]# python3
Python 3.7.6 (default, May 5 2022, 18:08:12)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
#使用这种方式可以保留python2
[root@localhost lib]# python
Python 2.7.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@localhost lib]#