Sometimes it’s require to install python2.7 on Ubuntu 10.04 which has python2.6 pre-installed, because some of the libraries are not compatible with python2.6 and they can work well if system has python2.7. If you try to replace 2.6 with 2.7 some of the system script might break. To avoid such scenario you generally have both python versions. This post will help you to walk you through steps to install version 2.7.

First pip should be removed because default pip is 2.6 compatible and always install libraries which are compatible with 2.6.

apt-get remove python-pip

Once it is removed then execute the following commands in the same sequence as these are listed below.

[sudo] add-apt-repository ppa:fkrull/deadsnakes 
[sudo] apt-get update
[sudo] easy_install pip
[sudo] pip install -U pip
[sudo] apt-cache search python  

Where [sudo] is optional. If you get any permission related error, then use sudo. Once apt-cache search python is executed then look for python2.7 entries. In case you don’t find the entries that means you haven’t executed apt-get update command. Finally, run the following command to install python2.7.

sudo apt-get install python2.7

Once it is installed, you can verify by checking the /usr/bin/ directory.

ls -al /usr/bin/pytho*

lrwxrwxrwx 1 root root       9 Jan 11 15:55 /usr/bin/python -> python2.6
lrwxrwxrwx 1 root root      16 Apr 17 18:17 /usr/bin/python-config -> python2.6-config
lrwxrwxrwx 1 root root       9 Jan 11 15:55 /usr/bin/python2 -> python2.6
-rwxr-xr-x 1 root root 2617584 Oct  1  2012 /usr/bin/python2.6
-rwxr-xr-x 1 root root    1452 Oct  1  2012 /usr/bin/python2.6-config
-rwxr-xr-x 1 root root 2925496 Apr 25 05:46 /usr/bin/python2.7

The above output indicates that python2.6 and python2.7 are coexisting in the system. But you have to make sure that by default python is still pointing to python2.6. Execute the following command and check the result. You should get the similar output as shown below.

python --version
Python 2.6.5

Now it’s time to check the python2.7 version.

/usr/bin/python2.7 
Python 2.7.4 (default, Apr 25 2013, 12:40:53) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

You are almost done. But if you script files have the following shebang. It may not work, because it is still pointing the default python version which is 2.6.

#!/usr/bin/env python   

You can fix the script by changing the shebang to python2.7 specific. This method works but not suggested.

#!/usr/bin/env python2.7

If you port this script to other system which has higher version of python then it may not work. So it’s better to stick with the standard version of shebang and use virtual environment. Virutal environment helps you to maintain different python environments for development. Each virtual environement has its own specific version library install, so you don’t have to worry about the version compatibility. It is just like having different virtual machines on a single physical machine.
you can install virtualenvwrapper to create virtual environment.

sudo pip install virtualenvwrapper

Once installed, execute the following command to generate virtual environment which will use python2.7.

mkvirtualenv --python=python2.7 yourvirtualenv
(yourvirtualenv)/>python --version
Python 2.7.4

If virtual environment is activated, its name will appear at the beginning of the command prompt (as shown in the highlighted line). To use the virtual environment you can use workon yourvirtualenv whereas to exit from virtual environment you can use deactivate


Hope this blog helped you in some way. If you like this blog then please share it. You can also leave your comment below.

, ,
Trackback

no comment untill now

Add your comment now