Install Python 3 in cPanel Server

This article offers a guide on how to install Python 3 on a cPanel server. As you may be aware, cPanel comes pre-installed with Python 2.X, which is utilised for Mailman and Yum. Consequently, upgrading the Python version directly could potentially disrupt the current cPanel configuration.

In this article, I will outline the process of installing Python 3 alongside the existing Python version without affecting the current setup.

In this article, I am describing how to install Python 3 alongside with the existing Python. If you do this yourself, on your own server or a client’s, you do so at your own risk.

yum update
yum groupinstall “development tools”
yum install tk-devel gdbm-devel db4-devel libpcap-devel xz-devel expat-devel readline-devel lib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel libffi-devel

Once the prerequisites installed, we can go ahead and install Python 3

Install Python 3 from Source

Download the latest version of Python from here: https://www.python.org/ftp/python/

cd /usr/local/src wget https://www.python.org/ftp/python/3.11.1/Python-3.11.1.tgz tar xzf Python-3.11.1.tgz cd Python-* ./configure –prefix=/usr/local/python3.11 –enable-shared LDFLAGS=”-Wl,-rpath /usr/local/lib” make make altinstall

Please make sure that you run make altinstall instead of make install. The make install command will break the existing Python installation.

To get Python 3.11 globally upon login, you need to update the library path like below:

Create a file python3.11.sh:

vim /etc/profile.d/python3.11.sh

Add the following into it:

PATH=$PATH:/usr/local/python3.11/bin
export PATH

Also, create another file opt-python3.11.conf

vim /etc/ld.so.conf.d/opt-python3.11.conf

Add the following into it:

/usr/local/python3.11/lib

Run the following commands to create the necessary links and cache to the most recent shared libraries found in the directories specified on the command line.

ldconfig
source /etc/profile.d/python3.11.sh

It will complete the Python 3 setup and you can verify the both Python installation using the following commands:

Base Python installation:

python -V

Example Output:

[root@grepit ~]# python -V
Python 2.7.5
[root@grepit ~]#

Python 3.11 installation:

python3.11 -V

Example Output:

[root@grepit ~]# python3.11 -V
Python 3.11.1
[root@grepit ~]#

That’s all folks.