The default Python version under CentOS is 2.6 or 2.7, and now more and more modules support Python 3, so it is very necessary to install a Python 3 in CentOS. Instead of upgrading Python 2 to Python 3, we’re talking about installation, where Python 2 and Python 3 coexist. If you want to upgrade and replace Python 2 directly, i don’t recommend it. It will cause a lot of system problems. After all, many parts of CentOS still rely on Python 2.
1. Download The Latest Version Of Python3.
Download the latest version of Python3 from the python official website. After the download is complete, extract and execute below command.
$ wget --no-check-certificate https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz $ xz -d Python-3.7.2.tgz $ tar -xzvf Python-3.7.2.tgz
2. Pre-Build Configuration.
$ cd Python-3.7.2/ $ ./configure --prefix=/usr/local/python3.7 --enable-optimizations
- The –prefix option is used to set the local installation directory path. If this option is not configured, the executable files will be placed in /usr/local/bin by default after installation. The library files are placed by default in /usr/local/lib, The configuration file is placed by default in /usr/local/etc, other resource files are placed in /usr/local/share.
- If configured –prefix, for example
--prefix=/usr/local/python3.7, then all resource files can be placed in path /usr/local/python3.7 for easy administration.
- –enable-optimizations is an optimization option (LTO, PGO, etc.). If add this flag for compilation, the compile performance will has about 10% optimization, but this will significantly increase the compile time.
- When the ./configure command is finished, it will create a file Makefile for the make command to use. After make install command execute, it will install the program into the folder you specified.
3. Perform Compile And Install.
$ make && make install
When the installation is complete, enter the directory /usr/local/python3.7, and you will see the Python executable directory bin and other related directories.
4. Add Soft Links.
After above steps, you might not be able to run Python3 directly because your /usr/local/ path might not be in the system PATH environment variable value, but we can solve this by adding a soft link run below commands.
$ ln -s /usr/local/python3.7/bin/python3.7 /usr/bin/python3 $ ln -s /usr/local/python3.7/bin/python3.7 /usr/bin/python3.7
Now you can run python3.7 directly in terminal like below.
$ python3 Python 3.7.2 (default, Mar 3 2019, 13:15:50) [GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux Type "help", "copyright", "credits" or "license" for more information. >>>