CentOS7.3 compile and install Python3.6.2,
I am using CentOS7.3 to install Python3.6.2
1. Check whether Python has been installed.
Centos7 is installed with Python2.7.5 by default, because some commands use it, such as yum, which uses python2.7.5.
Run the python-V command to check whether Python is installed:
# python -VPython 2.7.5
Check the location of the Python executable file:
# which python/usr/bin/python
Go to the bin directory:
# Cd/usr/bin/# ls-al python * lrwxrwxrwx. 1 root 34 August 25 08:18 python-> python2lrwxrwxrwx. 1 root 9 August 19 21:32 python2-> python2.7-rwxr-xr-x. 1 root 7136 November 6 2016 python2.7
You can see that the executable file python points to python2, and python2 points to python2.7, that is, the pre-installed Python2.7.
To install other Python versions, run the mv python. bak command to back up the python file.
# Mv python. bak # ls-al python * lrwxrwxrwx. 1 root 9 August 19 21:32 python2-> python2.7-rwxr-xr-x. 1 root 7136 November 6 2016 python2.7lrwxrwxrwx. 1 root 7 August 19 21:32 python. bak-> python2
2. Modify the yum configuration file
Because yum uses CentOS7.3 to install Python 2.7.5 by default, replacing it with Python 3.6.2 will not work properly.
# yum repolist File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: ^SyntaxError: invalid syntax
Therefore, modify the yum configuration file and continue to use Python2.7.5.
Change the python dependency of the yum script
# Cd/usr/bin # ls-al yum *-rwxr-xr-x. 1 root 801 November 15 2016 yum-rwxr-xr-x. 1 root 9429 November 6 2016 yum-builddep-rwxr-xr-x. 1 root 8582 November 6 2016 yum-config-manager-rwxr-xr-x. 1 root 7609 November 6 2016 yum-debug-dump-rwxr-xr-x. 1 root 7903 November 6 2016 yum-debug-restore-rwxr-xr-x. 1 root 10999 November 6 2016 yumdownloader-rwxr-xr-x. 1 root 11031 November 6 2016 yum-groups-manager
Use vim to change the above file header #! /Usr/bin/python #! /Usr/bin/python2
Modify the gnome-tweak-tool configuration file
# vim /usr/bin/gnome-tweak-tool
Put the file header #! /Usr/bin/python #! /Usr/bin/python2
Modify the urlgrabber configuration file
# vim /usr/libexec/urlgrabber-ext-down
Put the file header #! /Usr/bin/python #! /Usr/bin/python2
3. Prepare the compiling environment
Install the development kit to compile the Python3.6.2 source code
# yum groupinstall 'Development Tools'
Dependencies that may be used when python3.6.2 is installed
Copy codeThe Code is as follows:
# Yum install-y ncurses-libs zlib-devel mysql-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
4. Install Python3.6.2
Get the latest version of Python3.6.2 https://www.python.org/downloads/source/ on the official website
Download the tgz. xz package. Python-3.6.2.tar.xz, This Is The Python source code.
Run the wget command to download it to the current folder:
# wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz
Unpack:
# xz -d Python-3.6.2.tar.xz
Decompress:
# tar -xvf Python-3.6.2.tar.xz
After decompression, the Python-3.6.2 folder appears.
# Lldrwxr-xr-x. 17 max 4096 August 25 01:22 Python-3.6.2-rw-rw-r --. 1 max 83169280 July 17 11:55 Python-3.6.2.tar
Go to the Python-3.6.2 folder and compile the source code.
# cd Python-3.6.2/# ./configure --prefix=/usr/local/python3.6 --enable-optimizations
First, explain the above parameters:
The -- prefix option is the installation path. If this option is not configured, the executable files are stored in/usr/local/bin by default, and the library files are stored in/usr/local/lib by default, the configuration file is stored in/usr/local/etc by default, and other resource files are stored in/usr/local/share, which is messy.
If -- prefix is configured, for example :. /configure -- prefix =/usr/local/python3.6. All resource files can be stored in the path of/usr/local/python3.6 without clutter. another benefit of using the -- prefix option is to uninstall or transplant the software. When a software installation is no longer needed, you only need to delete the installation directory to clean the software; to transplant the software, you only need to copy the entire directory to another machine of the same operating system. of course, to uninstall the program, you can also use make uninstall in the original make directory, provided that the make file has specified uninstall.
-- Enable-optimizations is an optimization option (LTO, PGO, etc.). After this flag is compiled, the performance will be optimized by about 10%, but this will significantly increase the compilation time, which has been a long time.
./After the configure command is executed, create a file Makefile for the following make command. After the make install command is executed, the program will be installed in the specified folder.
Then run the make command.
# make
Run the make install command.
# make install
After the installation is complete, go to the/usr/local/python3.6 directory. You can see the Python executable file directory bin and other related directories.
# Cd/usr/local/python3.6 # Total ll usage 4drwxr-xr-x. 2 root 4096 August 25 08:07 bindrwxr-xr-x. 3 root 24 Aug 17 08:07 includedrwxr-xr-x. 4 root 63 August 25 08:07 libdrwxr-xr-x. 3 root 17 08:07 share
Go to the bin directory and you can see a lot of related executable files. Currently, you only need to pay attention to python3.6 and pip3.6.
# Cd bin # ll total usage 22552lrwxrwxrwx. 1 root 8 August 25 08:07 2to3-> 2to3-3.6-rwxr-xr-x. 1 root 111 August 25 08:07 2to3-3.6-rwxr-xr-x. 1 root 252 August 25 08:07 easy_install-3.6lrwxrwxrwx. 1 root 7 August 25 08:07 idle3-> idle3.6-rwxr-xr-x. 1 root 109 August 25 08:07 idle3.6-rwxr-xr-x. 1 root 224 August 25 08:07 pip3-rwxr-xr-x. 1 root 224 August 25 08:07 pip3.6lrwxrwxrwx. 1 root 8 August 25 08:07 pydoc3-> pydoc3.6-rwxr-xr-x. 1 root 94 August 25 08:07 pydoc3.6lrwxrwxrwx. 1 root 9 August 25 08:07 python3-> python3.6-rwxr-xr-x. 2 root 11526224 August 25 08:06 python3.6lrwxrwxrwx. 1 root 17 Aug 17 08:07 python3.6-config-> python3.6m-config-rwxr-xr-x. 2 root 11526224 August 25 08:06 python3.6m-rwxr-xr-x. 1 root 3107 August 25 08:07 python3.6m-configlrwxrwxrwx. 1 root 16 August 25 08:07 python3-config-> python3.6-configlrwxrwxrwx. 1 root 10 August 25 08:07 pyvenv-> pyvenv-3.6-rwxr-xr-x. 1 root 451 August 25 08:07 pyvenv-3.6
5. Modify the soft link configuration Python3.6 and pip3.6 as the default values.
# ln -s /usr/local/python3.6/bin/python3.6 /usr/bin/python# ln -s /usr/local/python3.6/bin/pip3.6 /usr/bin/pip
Go to the/usr/bin directory to view the soft link configuration results:
# Cd/usr/bin # ls-al python * lrwxrwxrwx. 1 root 34 August 25 08:18 python->/usr/local/python3.6/bin/python3.6lrwxrwxrwx. 1 root 9 August 19 21:32 python2-> python2.7-rwxr-xr-x. 1 root 7136 November 6 2016 python2.7lrwxrwxrwx. 1 root 7 August 19 21:32 python. bak-> python2 # ls-al pip * lrwxrwxrwx. 1 root 31 Aug 17 08:21 pip->/usr/local/python3.6/bin/pip3.6
View the default Python and pip versions of the system:
# python -VPython 3.6.2# python2 -VPython 2.7.5# pip -Vpip 9.0.1 from /usr/local/python3.6/lib/python3.6/site-packages (python 3.6)
Currently, the default python version is Python 3.6.2, while Python 2.7.5 installed by CentOS7.3 can be called using python2. pip is also in the latest version.
Here, CentOS7.3 compilation and installation of Python3.6.2 is complete!
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.