Today, I was trying to install an application on my CentOS 7.4 system which required Python >= 2.7.10, but there are Python 2.7.5 installed, which we can’t remove as other applications depend on it. This tutorial will help you to install Python 2.7.18 without removing older versions.
1. Prerequisites
Firstly make sure that you have GCC package installed on your system. Use the following command to install GCC if you don’t have it installed.
yum install gcc openssl-devel bzip2-devel # On CentOS systems dnf install gcc openssl-devel bzip2-devel # On Fedora systems
2. Download Python 2.7
Download Python using following command from python official site. You can also download the latest version in place of specified below.
cd /usr/src wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgz
Extract downloaded archive using tar command.
tar xzf Python-2.7.18.tgz
3. Install Python 2.7
Now run the following commands to compile Python 2.7 and install on your system using altinstall
.
cd Python-2.7.18 ./configure --enable-optimizations make altinstall
4. Check Python Version
Check the latest version installed of python using below command. During this installation, the latest Python binary was installed on path /usr/local/bin/python2.7. The existing binary was located under /usr/bin.
/usr/local/bin/python2.7 -V Python 2.7.18
5. Install PIP
PIP is a useful utility to install and manage Python modules. Let’s install the PIP for the installed Python version.
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" python2.7 get-pip.py
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?
[[email protected] 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
[[email protected] 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’
[[email protected] 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,
[[email protected] 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:
[[email protected] 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
[[email protected] 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!