Tuesday, January 26, 2016

How to Build and Install the Latest Version Python on Ubuntu

Ubuntu 14.04 LTS comes with Python 2.7.6 and 3.4.0 pre-installed. However, sometimes you may wish to install the most recent version, say 2.7.11 as of now along with the default system python. Unfortunately, Ubuntu's repository does not have the most recent version, so you will need to manually build Python from source files. This post will show you how to do so. Note that in this example, I will be installing 2.7.11, but you may want to use a different version as desired.

Before we do anything, let us examine where the system default Python is located and what version it is

$ which python

The which  command shows the file which will be executed upon running the command $ python in shell. This is the default Python file pre-installed on the system. To check its version, run

$ python --version
Python 2.7.6

So, the default version that comes with Ubuntu 14.04 LTS is indeed 2.7.6 and is located in the /usr/bin directory.

OK, let us now start the installation process for the latest version 2.7.11. First, download the Python source archive file
$ wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgz

Extract the files
$ tar -xf Python-2.7.11.tgz

Enter the directory
$ cd Python-2.7.11

Before we configure, we must install quite a few packages necessary to build Python successfully. Although not all of the packages below are required, I recommend getting them because they will be needed for installing packages later on, such as numpy, scipy, and matplotlib, which I will cover in the next post.
$ sudo apt-get install -y build-essential gcc-multilib g++-multilib libffi-dev libffi6 libffi6-dbg python-crypto python-mox3 python-pil python-ply libssl-dev zlib1g-dev libbz2-dev libexpat1-dev libbluetooth-dev libgdbm-dev dpkg-dev quilt autotools-dev libreadline-dev libtinfo-dev libncursesw5-dev tk-dev blt-dev libssl-dev zlib1g-dev libbz2-dev libexpat1-dev libbluetooth-dev libsqlite3-dev libgpm2 mime-support netbase net-tools bzip2 libblas-dev liblapack-dev gfortran

Now, configure
$ ./configure

Then build
$ sudo make install

It will take some time to build and install. When complete, we verify whether Python is now updated to 2.7.11 version
$ python --version
Python 2.7.11

Great. So, the question is, did we just overwrite the system's default python? The answer is no; we actually installed the new version on the system side by side from the old one, so the old version is still intact. How do we figure? Simply run

$ which python

The output shows that the command $ python will now execute /usr/local/bin/python, which is different from the old one /usr/bin/python. To verify this, see if we can still run the old one

$ /usr/bin/python --version
Python 2.7.6

So, the old one is still there; if we need to run the old one, we just need to execute it by the full path. This is because the default $PATH environment looks something like this

$ echo $PATH

The directory /usr/local/bin, where version 2.7.11 is installed, precedes the directory /usr/bin, where version 2.7.6 is installed. Thus, when we run $ python command, it will execute the Python file in /usr/local/bin, thus running the 2.7.11 version.

No comments:

Post a Comment