In an era where Python 3 dominates the development landscape, certain legacy applications still require Python 2.7. However, newer distributions of Linux, such as CentOS/RHEL 9/8 and Fedora, come with Python 3 installed by default. This guide provides a step-by-step approach to installing Python 2.7 alongside the system’s default Python installation, ensuring your legacy applications continue to run smoothly without disrupting the system’s Python 3 environment.
Preparing Your System
Before proceeding with the installation, ensure your system is up to date. Open a terminal and execute the following command:
sudo dnf update
This command ensures all your system’s packages are updated to their latest versions.
Step 1: Install Development Tools
Python 2.7 requires certain development tools and libraries to compile from source. Install them by running:
sudo dnf groupinstall "Development Tools"
sudo dnf install openssl-devel bzip2-devel libffi-devel
Step 2: Download Python 2.7
Next, download the Python 2.7 source code from the official Python website. You can do this by navigating to https://www.python.org/downloads/source/ and downloading the latest Python 2.7.x version, or by using wget with the appropriate link. As of writing this guide, Python 2.7.18 is the latest version. You can download it using:
wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tar.xz
Extract the downloaded file:
tar -xf Python-2.7.18.tar.xz
Step 3: Compile Python Source
Navigate to the directory containing the extracted Python source code:
cd Python-2.7.18
Prepare the Python 2.7 source for compilation:
./configure --enable-optimizations --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
Compile and install Python 2.7:
make
sudo make altinstall
Using make altinstall instead of make install prevents replacing the default python command with Python 2.7. This way, Python 3 remains the default Python interpreter on your system.
Step 4: Verify the Installation
After the installation process is complete, verify it by checking the Python version:
python2.7 --version
You should see output similar to Python 2.7.18, indicating that Python 2.7 has been successfully installed.
Step 5: Setting up a Virtual Environment (Optional)
To isolate your Python 2.7 environment, it’s recommended to use virtualenv. First, install pip for Python 2.7:
wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
sudo python2.7 get-pip.py
Then, install virtualenv:
sudo pip2.7 install virtualenv
Create a virtual environment for your project:
virtualenv -p /usr/local/bin/python2.7 venv
Activate the virtual environment:
source venv/bin/activate
Now, you're ready to work on your Python 2.7 project within an isolated environment.
Conclusion
Installing Python 2.7 on CentOS/RHEL 9/8 and Fedora systems allows you to run legacy Python applications without interfering with the system's default Python 3 environment. By following this guide, you can ensure compatibility with older software while benefiting from the security and features of modern Linux distributions. Remember to consider migrating your Python 2.7 applications to Python 3 to take advantage of the latest improvements and support.
48 Comments
Seems like you’re missing “yum install make” as dependency too 🙂
(at least i got an error in a centos 7 container)
Please note that the command
curl “https://bootstrap.pypa.io/get-pip.py” -o “get-pip.py”
does not work anymore. User gets error
curl: (35) Peer reports incompatible or unsupported protocol version.
I think this error is given by the fact that the site “https://bootstrap.pypa.io/get-pip.py” does not accept TLS version older than 1.2.
Workaround was to give the command on a different machine that had TLS1.2 and copy the downloaded get-pip.py file from that machine to the CentOS machine on location /usr/src/Python-2.7.16
after this step, give the last command from the webBlog.
python2.7 get-pip.py
Please beware, this doesn’t work on Centos 7, make alt install will break default python 2.7.5 installation.
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won’t btained after that date. A future version of pip will drop support for Python 2.7.
WHAT? when i type python -V im still on 2.6 then it say that
Hello, and how can I uninstall this? Since I can only use it using python2.7
I want to uninstall it
>>> import boto
>>> boto.version
Traceback (most recent call last):
File “”, line 1, in
AttributeError: ‘module’ object has no attribute ‘version’
>>>
how to fix this error
Hi Suchita, Try with capital V in version:
>>> import boto
>>> boto.Version
an error in the tutorial.
search for: tar xzf Python-2.7.14.tgz
replace by: tar xzf Python-2.7.15.tgz
Thanks V. Article has been updated for the latest version installation of Python 2.7.
Im getting this error, is there any way to bypass certificate checking?
[root@localhost Python-2.7.15]# python2.7 get-pip.py
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won’t be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting pip
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:618)’),)’: /simple/pip/
Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:618)’),)’: /simple/pip/
Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:618)’),)’: /simple/pip/
Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:618)’),)’: /simple/pip/
Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:618)’),)’: /simple/pip/
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=’pypi.org’, port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:618)’),)) – skipping
Could not find a version that satisfies the requirement pip (from versions: )
No matching distribution found for pip
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=’pypi.org’, port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:618)’),)) – skipping
Works well with RHEL7. I was having issues installing Python 2.7.14 onto a RHEL7 system that had Python 2.7.5 as it’s native install.
Your concise instructions helped show how “make altinstall” was the correct solution to avoid overwriting the native Python 2.7.5 install.
Two additions:
1. altinstall role is not properly described. Altinstall means “don’t overwrite $PREFIX/bin/python symlink”; in our case system-default Python is installed under /usr/bin/python (PREFIX=/usr), while we install 2.7.14 Python under /usr/local/bin/python (PREFIX=/usr/local).
So if we use `make install` then we will create `/usr/local/bin/python`, without overwriting `/usr/bin/python`. And `/usr/local/bin` has higher priority than `/usr/bin` resulting in new Python version being used for `python` command, and this may break some system-wide stuff. Although scripts with hard-coded `#!/usr/bin/python` in shebang will still use old python version.
2. No need to download `get-pip.py` script because Python 2.7.14 has built-in `ensurepip` module. It is enough to enter `python2.7 -m ensurepip` to install pip.
Hi Rahul ,
do u have any idea how to install 32 bit python on 64 bit rhel 6.8 .
All worked and many thanks for publishing this!
Any way I can make a binary i.e. rpm with this and then move it to prod? I don’t want to compile stuff on prod servers.
python2.7 is not a valid command
/usr/local/bin/python2.7 –version ,This path need to be added to PATH
/usr/local/bin/python2.7 –version
what is the latest python version which supports RHEL 6.8 ?
I am getting error in the last command python2.7 get-pip.py
The following is the error msg
[uexcel@localhost Python-2.7.13]$ python2.7 get-pip.py
Collecting pip
Using cached pip-9.0.1-py2.py3-none-any.whl
Collecting wheel
Using cached wheel-0.29.0-py2.py3-none-any.whl
Installing collected packages: pip, wheel
Exception:
Traceback (most recent call last):
File “/tmp/tmpGCg2kV/pip.zip/pip/basecommand.py”, line 215, in main
status = self.run(options, args)
File “/tmp/tmpGCg2kV/pip.zip/pip/commands/install.py”, line 342, in run
prefix=options.prefix_path,
File “/tmp/tmpGCg2kV/pip.zip/pip/req/req_set.py”, line 784, in install
**kwargs
File “/tmp/tmpGCg2kV/pip.zip/pip/req/req_install.py”, line 851, in install
self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
File “/tmp/tmpGCg2kV/pip.zip/pip/req/req_install.py”, line 1064, in move_wheel_files
isolated=self.isolated,
File “/tmp/tmpGCg2kV/pip.zip/pip/wheel.py”, line 345, in move_wheel_files
clobber(source, lib_dir, True)
File “/tmp/tmpGCg2kV/pip.zip/pip/wheel.py”, line 316, in clobber
ensure_dir(destdir)
File “/tmp/tmpGCg2kV/pip.zip/pip/utils/__init__.py”, line 83, in ensure_dir
os.makedirs(path)
File “/usr/lib64/python2.7/os.py”, line 157, in makedirs
mkdir(name, mode)
OSError: [Errno 13] Permission denied: ‘/usr/lib/python2.7/site-packages/pip’
[uexcel@localhost Python-2.7.13]$
Use –user along with command
python2.7 get-pip.py –user
What about pip for this version of python?
I Eugene, I have added the last step to installed PIP for the installed Python version.
I installed the version 2.7.5 but i don’t find the file python2.7. I d’ont understand.
cmd type = python
start your programing
These steps are simple and easy to replicate across hosts. Worked like a charm. Thanks.
python2.7 -V didn’t work. says cannot find it.
I can run “python” from “/usr/src/Python2.7.13/” and I get the version 2.7.3 but running python2.7 does nothing.
What went wrong? Do I need to run install-sh after doing the “make altinstall” command?
Please help. I’m not familiar with Oracle Linux, I’m used to Ubuntu Server and I’m floundering a bit here.
Excellent
thx drom Russia
Your article helps me to install…
After the installation we are getting the below error when we try to run a command,
[root@earth home]# vdetect –reseller=xyz
AH00112: Warning: DocumentRoot [/home/*/public_html/web1] does not exist
AH00112: Warning: DocumentRoot [/home/*/public_html/krk.*.com] does not exist
AH00112: Warning: DocumentRoot [/home/*/public_html/home/cistrons/public_html/demo] does not exist
AH00112: Warning: DocumentRoot [/home/*/public_html/web1] does not exist
AH00112: Warning: DocumentRoot [/home/*/public_html/home/innosysc/public_html/demo] does not exist
AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/apache2/conf/httpd.conf:107656
AH00112: Warning: DocumentRoot [/home/*/public_html/paypal-singin] does not exist
AH00112: Warning: DocumentRoot [/home/*/public_html/paypal-singin] does not exist
Traceback (most recent call last):
File “/root/bin/vdetect”, line 618, in
signatures = ApplicationSignature.get_xml_signatures_from_url(opts.sigloc, opts.useragent)
File “/root/bin/vdetect”, line 218, in get_xml_signatures_from_url
return cls.get_xml_signatures_from_string(urllib2.urlopen(req).read())
File “/usr/local/lib/python2.7/urllib2.py”, line 154, in urlopen
return opener.open(url, data, timeout)
File “/usr/local/lib/python2.7/urllib2.py”, line 429, in open
response = self._open(req, data)
File “/usr/local/lib/python2.7/urllib2.py”, line 447, in _open
‘_open’, req)
File “/usr/local/lib/python2.7/urllib2.py”, line 407, in _call_chain
result = func(*args)
File “/usr/local/lib/python2.7/urllib2.py”, line 1228, in http_open
return self.do_open(httplib.HTTPConnection, req)
File “/usr/local/lib/python2.7/urllib2.py”, line 1198, in do_open
raise URLError(err)
urllib2.URLError:
[root@earth home]#
It about Windows 7, It doesn’t have python 2.7 and I want to install python 2.7.13 first so how would I do this using batch file? Assume all setups are placed on common location.
Works. Thank you
make altinstall command is failing with the following message:
make: *** No rule to make target ‘altinstall’. Stop
Please install gcc and try again
Installed like a charm on an IBM Mainframe. Thanks!
Hi,
This works perfectly.
Could you show how to install pyodbc to this python2.7 altinstall dir?
By default running the pyodbc rpm, it gets install to the python 2.6 dir.
Please advise. thanks.
works gr8
Worked as a charm for me on Centos 6.4. Thank you very for the article.
Thanks.
Just a heads up, if you are on centos/rhel 7, where python2.7.5 is the default, the altinstall option for any other python2.7 version (2.7.11 for instance) will break your installation as it writes to the same /usr/lib/python2.7 directory and destroys the yum modules. I am sure there is a way around this, but I just encountered this issue and it took rebuilding yum from a few RPMs just to get my system back to normal.
How did you do this? I did the same thing and would like to get back to the original situation?
Thanks!
How did you fix this? Is this why pip will not install?
# curl “https://bootstrap.pypa.io/get-pip.py” -o “get-pip.py”
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1558k 100 1558k 0 0 1172k 0 0:00:01 0:00:01 –:–:– 1174k
# python2.7 get-pip.py
Traceback (most recent call last):
File “get-pip.py”, line 20061, in
main()
File “get-pip.py”, line 194, in main
bootstrap(tmpdir=tmpdir)
File “get-pip.py”, line 82, in bootstrap
import pip
zipimport.ZipImportError: can’t decompress data; zlib not available
[root@TOPLLHinf01 src]#
RUN “yum install gcc openssl-devel bzip2-devel” first! then download and install python
Great article! Easy to read and does what it supposed to do. I tried it on Centos6.6
More quickly
yum install centos-release-SCL
yum install python27
scl enable python27 bash
Thank YOu!
Thanks, how to install pip?
thanks, great work!
just a question from a novice, how do i now -use- this altinstall instead of the standard version
e.g. when running plexconnect.py?
Hello Rahul,
I have installed Python 2.7 on centos 5 from source code. Python 2.4 is already there in centos 5. Could you please help me to uninstall Python 2.7 so that I can reinstall it with yum.
Thanks in advance
Worked perfectly and put into simple steps. Very good for someone new to linux. Thanks!