我们之前介绍了在 Linux系统上的软件安装,有三种方式:源代码包编译安装,npm/dpkg软件包安装,yum/apt前端工具安装。

这里,我采取的是源码包安装。

1,准备工作

1.1 安装依赖

yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y

1.2 下载源码安装包

到 Python官方网站 下载相关版本的源码包:

1,准备工作-LMLPHP

我这里直接用wget获取到本地

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

解压

tar -xvf Python-3.6.0.tar.xz

2 安装

2.1 阅读文档

进到解压后的目录中,查看 README 文件,其中有一段

This is Python version 3.6.0
============================

Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
2012, 2013, 2014, 2015, 2016 Python Software Foundation.  All rights reserved.

Python 3.x is a new version of the language, which is incompatible with the
2.x line of releases.  The language is mostly the same, but many details,
especially how built-in objects like dictionaries and strings work,
have changed considerably, and a lot of deprecated features have finally
been removed.


Build Instructions
------------------

On Unix, Linux, BSD, OSX, and Cygwin:

    ./configure
    make
    make test
    sudo make install

This will install Python as python3.

You can pass many options to the configure script; run "./configure --help" to
find out more.  On OSX and Cygwin, the executable is called python.exe;
elsewhere it's just python.

可以看到,这里已经对在 Linux 系统安装给出了步骤,我们按照步骤来就好了。

2.2 ./configure 配置

输出

checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking for python3.6... python3.6
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... linux
checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... gcc
.
.
.
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Modules/Setup.config
config.status: creating Misc/python.pc
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


If you want a release build with all optimizations active (LTO, PGO, etc),
please run ./configure --enable-optimizations

可以看到,这个命令主要是做了一些检查,最后生成了几个安装所需要的文件,以及 下一步编译所需要的 Makefile 文件。

也可以在 ./c 后面加上 --prefix=/some/path/python36/ 的参数,表示安装路径。我这里没加,就在当前目录安装。

2.3 make 编译

输出

/usr/bin/make64 MAC=64
gcc -pthread -c -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes    -std=c99 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers   -I. -I./Include    -DPy_BUILD_CORE \
		-DABIFLAGS='"m"' \
		-DMULTIARCH=\"x86_64-linux-gnu\" \
		-o Python/sysmodule.o ./Python/sysmodule.c
.
.
.
running build_scripts
copying and adjusting /home/work/Python-3.6.0/Tools/scripts/pydoc3 -> build/scripts-3.6
copying and adjusting /home/work/Python-3.6.0/Tools/scripts/idle3 -> build/scripts-3.6
copying and adjusting /home/work/Python-3.6.0/Tools/scripts/2to3 -> build/scripts-3.6
copying and adjusting /home/work/Python-3.6.0/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   -Xlinker -export-dynamic -o Programs/_testembed Programs/_testembed.o libpython3.6m.a -lpthread -ldl  -lutil -lrt   -lm
# Substitution happens here, as the completely-expanded BINDIR
# is not available in configure
sed -e "s,@EXENAME@,/usr/local/bin/python3.6m," < ./Misc/python-config.in >python-config.py
# Replace makefile compat. variable references with shell script compat. ones;  ->
LC_ALL=C sed -e 's,\$(\([A-Za-z0-9_]*\)),\$\{\1\},g' < Misc/python-config.sh >python-config
# On Darwin, always use the python version of the script, the shell
# version doesn't use the compiler customizations that are provided
# in python (_osx_support.py).
if test `uname -s` = Darwin; then \
		cp python-config.py python-config; \
	fi

没有报错。就算编译好了。

2.4 make install

有时候要加上sudo,输出

.
.
.
Compiling '/usr/local/lib/python3.6/site-packages/setuptools/monkey.py'...
Compiling '/usr/local/lib/python3.6/site-packages/setuptools/msvc.py'...
Compiling '/usr/local/lib/python3.6/site-packages/setuptools/namespaces.py'...
Compiling '/usr/local/lib/python3.6/site-packages/setuptools/package_index.py'...
Compiling '/usr/local/lib/python3.6/site-packages/setuptools/py26compat.py'...
.
.
.
Listing '/usr/local/lib/python3.6/site-packages/setuptools-28.8.0.dist-info'...
PYTHONPATH=/usr/local/lib/python3.6  \
		./python -E -m lib2to3.pgen2.driver /usr/local/lib/python3.6/lib2to3/Grammar.txt
Generating grammar tables from /usr/local/lib/python3.6/lib2to3/Grammar.txt
Writing grammar tables to /usr/local/lib/python3.6/lib2to3/Grammar3.6.0.final.0.pickle
PYTHONPATH=/usr/local/lib/python3.6  \
		./python -E -m lib2to3.pgen2.driver /usr/local/lib/python3.6/lib2to3/PatternGrammar.txt
Generating grammar tables from /usr/local/lib/python3.6/lib2to3/PatternGrammar.txt
Writing grammar tables to /usr/local/lib/python3.6/lib2to3/PatternGrammar3.6.0.final.0.pickle
/usr/bin/install -c -m 644 ./Include/abstract.h /usr/local/include/python3.6m
/usr/bin/install -c -m 644 ./Include/accu.h /usr/local/include/python3.6m
/usr/bin/install -c -m 644 ./Include/asdl.h /usr/local/include/python3.6m
.
.
.
./python -E ./setup.py install \
	   	--prefix=/usr/local \
		--install-scripts=/usr/local/bin \
		--install-platlib=/usr/local/lib/python3.6/lib-dynload \
		--root=/
running install
running build
running build_ext
INFO: Can't locate Tcl/Tk libs and/or headers
building '_sqlite3' extension
.
.
.
running build_scripts
copying and adjusting /home/work/Python-3.6.0/Tools/scripts/pydoc3 -> build/scripts-3.6
copying and adjusting /home/work/Python-3.6.0/Tools/scripts/idle3 -> build/scripts-3.6
.
.
.
(cd /usr/local/bin; ln -s 2to3-3.6 2to3)
rm -f /usr/local/bin/pyvenv
(cd /usr/local/bin; ln -s pyvenv-3.6 pyvenv)
if test "x" != "x" ; then \
		rm -f /usr/local/bin/python3-32; \
		(cd /usr/local/bin; ln -s python3.6-32 python3-32) \
	fi
rm -f /usr/local/share/man/man1/python3.1
(cd /usr/local/share/man/man1; ln -s python3.6.1 python3.1)
if test "xupgrade" != "xno"  ; then \
		case upgrade in \
			upgrade) ensurepip="--upgrade" ;; \
			install|*) ensurepip="" ;; \
		esac; \
		 ./python -E -m ensurepip \
			$ensurepip --root=/ ; \
	fi
Requirement already up-to-date: setuptools in /usr/local/lib/python3.6/site-packages
Requirement already up-to-date: pip in /usr/local/lib/python3.6/site-packages

没有报错。

3 检验

在命令行中输入

python

输出

Python 2.7.3 (default, Sep 21 2020, 16:53:02)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

为什么是2.7.3,我明明安装的是3.6.0版本。查了一下才知道

whereis python

python: /usr/bin/python /usr/bin/python2.6 /usr/bin/python2.6-config /usr/lib/python2.6 /usr/lib64/python2.6
/usr/local/bin/python3.6m /usr/local/bin/python3.6 /usr/local/bin/python3.6m-config /usr/local/bin/python3.6-config
/usr/local/lib/python3.6 /usr/include/python2.6 /opt/hosteye/bin/python2.7 /usr/share/man/man1/python.1.gz

再试一下

python3.6

Python 3.6.0 (default, Mar  5 2021, 17:28:05)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

至此,Python3.6就安装好了。

04-16 22:39