We recommend to use the Python 3 that is available via Red Hat Software Collections. Software collections are part of your RHEL subscription and have their own release cadence so they can be updated more frequently and independent of RHEL OS releases. These are very secure and fully supported by Red Hat. Software collections is now up to Python 3.8. What's great about software collections is that Python 3.8 installations will be exactly the same across your servers - whether you install some today or in 3 months. A problem with other alternatives (e.g. EPEL) is that the package set may be one thing today but be different in 3 months. Also, software collections allow you to install 2 different Pythons in parallel without contention. A lot of RHEL 7 was built on Python 2.7 and installing Python 3 without software collections can potentially (and commonly) break the OS.
To install python 3 on RHEL 7, use these instructions. Installation requires that you need to enable the correct repos, etc. The first time through may be a bit slow, but people have found it to be routine afterwards.
These same software collections are also delivered as Linux containers and can be found in our catalog.
Answer from Mike Guerette on Stack Overflowpython - what is the formal latest python3 installtion on rhel 7.X servers - Stack Overflow
How to change python default version from 2.7 to 3.7 on RHEL 7 - Unix & Linux Stack Exchange
rhel - How to install python3 with all it is tools on Redhat RHEL7 offline machine - Unix & Linux Stack Exchange
python3 - need a help to find python 3.7 rpm for RHEL 8.4 - Unix & Linux Stack Exchange
Videos
RHEL 7 and its derivatives depend on Python 2 at a very deep level. If you outright replace Python 2 with 3, you'll break several of the OS's core tools.
Even if that were not the case, your question is based on an incorrect premise, being that completely replacing Python 2 with Python 3 is a good idea in the first place. Instead, install both side-by-side.
How? It starts with the fact that one should always call Python 3 as python3, since that insulates you from the major version compatibility problem. Scripts that assume Python 2 will call it as python in shebang lines and such, so there is not in fact a conflict between the old version and your newer version's python3 binary and everything that depends on it, as long as your binary Python packages are built properly.
That โifโ can bite you: some packagers have created Python 3 packages with a /usr/bin/python or similar, which creates a conflict. These packages are ignoring the standard advice, which allows both to be installed in parallel.
As for getting a non-conflicting Python 3 package for RHEL 7, thatโs well covered in another answer on Stack Overflow.
I would recommend the alternatives solution
My commands wold be.
#!/bin/bash
alternatives --list | grep -i python
alternatives --install /usr/bin/python python /usr/bin/python2.7 1
alternatives --install /usr/bin/python python /usr/bin/python3.6 2
alternatives --install /usr/bin/python python /usr/local/bin/python3.7 3
alternatives --config python
Installing from RPM is generally better, because:
- you can install and uninstall (properly) python3.
- the installation time is way faster. If you work in a cloud environment with multiple VMs, compiling python3 on each VMs is not acceptable.
Solution 1: Red Hat & EPEL repositories
Red Hat has added through the EPEL repository:
- Python 3.4 for CentOS 6
- Python 3.6 for CentOS 7
[EPEL] How to install Python 3.4 on CentOS 6
sudo yum install -y epel-release
sudo yum install -y python34
# Install pip3
sudo yum install -y python34-setuptools # install easy_install-3.4
sudo easy_install-3.4 pip
You can create your virtualenv using pyvenv:
pyvenv /tmp/foo
[EPEL] How to install Python 3.6 on CentOS 7
With CentOS7, pip3.6 is provided as a package :)
sudo yum install -y epel-release
sudo yum install -y python36 python36-pip
You can create your virtualenv using pyvenv:
python3.6 -m venv /tmp/foo
If you use the pyvenv script, you'll get a WARNING:
$ pyvenv-3.6 /tmp/foo
WARNING: the pyenv script is deprecated in favour of `python3.6 -m venv`
Solution 2: IUS Community repositories
The IUS Community provides some up-to-date packages for RHEL & CentOS. The guys behind are from Rackspace, so I think that they are quite trustworthy...
https://ius.io/
Check the right repo for you here:
https://ius.io/setup
[IUS] How to install Python 3.6 on CentOS 6
sudo yum install -y https://repo.ius.io/ius-release-el6.rpm
sudo yum install -y python36u python36u-pip
You can create your virtualenv using pyvenv:
python3.6 -m venv /tmp/foo
[IUS] How to install Python 3.6 on CentOS 7
sudo yum install -y https://repo.ius.io/ius-release-el7.rpm
sudo yum install -y python36u python36u-pip
You can create your virtualenv using pyvenv:
python3.6 -m venv /tmp/foo
It is easy to install python manually (i.e. build from source):
Download (there may be newer releases on Python.org):
$ wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tar.xzUnzip
$ tar xf Python-3.* $ cd Python-3.*Prepare compilation
$ ./configureBuild
$ makeInstall
$ make installOR if you don't want to overwrite the
pythonexecutable (safer, at least on some distrosyumneedspythonto be 2.x, such as for RHEL6) - you can installpython3.*as a concurrent instance to the system default with analtinstall:$ make altinstall
Now if you want an alternative installation directory, you can pass --prefix to the configurecommand.
Example: for 'installing' Python in /opt/local, just add --prefix=/opt/local.
After the make install step: In order to use your new Python installation, it could be, that you still have to add the [prefix]/bin to the $PATH and [prefix]/lib to the $LD_LIBRARY_PATH (depending of the --prefix you passed)