If we install the Linux OS, then by default, python will be installed on the Linux system. But generally, the installed python version is lower than the current python version which has been released to the public.
Problem
How to install the latest version of python from the source?
Solution
To see what version of python is installed by default on a Linux system, type the command python and followed by pressing Tab 2x, it will show the version of python installed on a Linux system:
To see the latest python version, please look at this site, and until this writing, the last python version released was version 3.10.5, so we will install the version into our Linux system. Here are the steps to install the latest version of python:
1. Download packages
Previously we have to prepare the packages needed to install python:
CentOS
yum install -y wget gcc make zlib zlib-devel libffi-devel openssl-devel
Ubuntu/Debian
apt-get install make gcc libssl-dev zlib1g-dev zlib1g
OpenSUSE
zypper install -y perl gcc make zlib zlib-devel libffi-devel openssl-devel
2. Install the OpenSSL package
Since we want to install the latest version of python then we should also install the latest version of openssl package where we can see the latest version on this page. I chose openssl version 1.1.1o to install on my server by using the command:
wget --no-check-certificate https://www.openssl.org/source/openssl-1.1.1o.tar.gz
tar -zxvf openssl-1.1.1o.tar.gz
cd openssl-1.1.1o/
./config
make
make install
For CentOS, execute the below command:
export LD_LIBRARY_PATH = /usr/local/lib64:$LD_LIBRARY_PATH
After that, type the following command to see the location of openssl on the server:
whereis openssl
and make sure the /usr/include/openssl folder exists after you write the command.
3. Install python
After that, download the latest python by typing:
wget https://www.python.org/ftp/python/3.10.5/Python-3.10.5.tgz
tar -zxvf Python-3.10.5.tgz
cd Python-3.10.5
Then, we back up the Setup file because we will change the configuration of the file:
cp Modules/Setup Modules/Setup.ori
After that, we open the file and change the file from
# OPENSSL=/path/to/openssl/directory # _ssl _ssl.c \ # -I$(OPENSSL)/include -L$(OPENSSL)/lib \ # -lssl -lcrypto
to
OPENSSL=/usr/include/openssl _ssl _ssl.c \ -I$(OPENSSL)/include -L$(OPENSSL)/lib \ -lssl -lcrypto
then, execute the commands below to install the latest python:
./configure --enable-optimizations
make altinstall
After that we check whether the latest version of python is installed properly on Linux, by typing python and followed by pressing the tab key 2x, then the latest version of Python should be installed properly as shown below:
And the pip package or Python Package Index (PyPI) which is used to install python modules should already be installed in the Linux system. To find out, type the word pip and followed by tab 2x. To install a module in the latest version of python, use the following format:
pip3.10 install module_name
If we want to install fuzzywuzzy module, so we must type the command below:
pip3.10 install fuzzywuzzy
To check whether the module that we have installed using the latest version of python is properly installed in our Linux system, use the following command:
pip3.10 list
and you should see an image like the one below:
which means the module is installed properly in the last version of python.
Note
If you have an error like this when you using install pip to install a module:
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
To solve this error, you must install python with openssl option as in the commands above but previously the python-compatible openssl package must be installed on the server first.
References
tecadmin.net
techglimpse.com
bswen.com
serverfault.com
cyberithub.com
pythonpool.com
linuxize.com