WHM-cPanel Server uses Python for Mailman, yum etc. This means cPanel relies on the older Python 2.x installations for its working and replacing that may cause serious issues.
CentOS/RHEL 6 and 7 ships with Python 2.x. The latest version of Python available in CentOS/RHEL 6 is 2.6.6 and CentOS/RHEL 7 is 2.7.5. However the latest version of Python available is 3.x.x and most of the tools used now requires higher Python 3.x.x versions.
In this tutorial, we will see how to install Python 3.x.x version in a cPanel server without disturbing the base python installation.

Install Prerequisites

Make sure the system is up to date.
# yum update
This version of python is installed from source. So we need to install compilers and related development tools.
# yum groupinstall -y "development tools"
After installing the development tools, install the libraries that are essential for the full functioning of Python. Some of the packages may be already available. To avoid compilation and runtime errors, follow the below steps to install the required packages.
# yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel expat-devel

Download Python

Go to https://www.python.org/ftp/python/ and choose the version of python as per your requirement and find the tar.gz file. We will choose Python version 3.6.3 in this example.
# cd /usr/local/src
# wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
Extract the TAR file
# tar xzf Python-3.6.3.tgz
# cd Python-3.6.3

Compile Python

In this example, we will install python to “/usr/local/python3.6”. So this should be passed to the configure command with --prefix=/usr/local/python3.6
Passing --enable-shared flag to the configure command will enable python to be compiled as a shared library. This would be an advantage if multiple python processes are running for different dependent tools.
The LDFLAGS="-Wl,-rpath /usr/local/lib" flag will help the executable to find its shared library.
Compile python using the following command.
# ./configure --prefix=/usr/local/python3.6 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
Wait for the process to complete and issue the make command.
# make
The make command should complete without errors, if everything you do is correct. Be sure that you use make altinstall rather than ‘make install’. The ‘make install’ command will mess up with the already existing python. So install using the command,

Updated:  The following issue was reported in our comments section. Special thanks to Markos Giannopoulos.
So in case you get an error during ‘make’ which says,
No module named ‘_ctypes’
the solution is to install libffi devel package. Run,
yum -y install libffi-devel
Then run ‘make’ again. Verify it completes without errors.
Next, be sure that you use ‘make altinstall’ rather than ‘make install’. The ‘make install’ command will mess up with the already existing python. So install using the command,
 

# make altinstall
This will install python to the said directory, /usr/local/python3.6

Add Path

Add the lib folder to the library path and update our global PATH so that python3.6 is available globally upon login.
Add the following text to /etc/profile.d/python3.6.sh with a text editor.
PATH=$PATH:/usr/local/python3.6/bin
export PATH

Similarly, create a file /etc/ld.so.conf.d/opt-python3.6.conf and add the following content to it.
/usr/local/python3.6/lib
Update the current session and the servers library cache by issuing the following commands in shell,
# ldconfig
# source /etc/profile.d/python3.6.sh
This will complete the python 3.x.x setup on a WHM/cPanel server without changing the base installation. The python installed just now can be called by typing in python3.6 in shell. The base python can be used by just python command.
The pip which is essential to install python packages is also installed along with this.

Thanks for dropping by. Ready for the next blog?
https://dev.sysally.com/blog/howto-high-availability-docker-swarm

Get new insights right to your inbox

How can our experts help you?

Schedule your consultation

You may also like

  • By admin
  • in DevOps

DevOps as a Service: All You Should Know

  • Aug 9, 2022 .
  • 9 min min
Read More
  • By admin
  • in Containerization

Containerization VS Virtualization: Understanding the Differences

  • Aug 4, 2022 .
  • 8 min min
Read More
  • By admin
  • in DevOps

Best DevOps Tools You Must Try in 2022

  • Jul 26, 2022 .
  • 12 min min
Read More

Be in the know

Techno tips served hot! Subscribe now and stay atop.