Upgrade Python on Debian

Published on: Fri, Oct 28, 2016 at 3:53 pm EST
Debian Linux Guides Programming Python System Admin

As you may know, Debian repositories update very slowly. At the time of writing, Python release versions are at 2.7.12 and 3.5.2, but in Debian 8 repositories you can find only 2.7.9 and 3.4.2. On Debian 7, versions in the repository are even older (2.7.3 and 3.2.3), both shipped in 2012.

This tutorial teaches you how to build Python from source and install the latest version. I assume you are using Debian 7 or 8. We will be obtaining the source code from the official Python FTP.

First, let's check your currently installed version of Python. Run this command on your terminal:

python --version

You will see something like this:

Python 2.7.9

Commands below must be performed by the root user.

Obtain Python source code

The following commands will load an archive with Python sources for version 2.7.12 to the home directory, unpack it, and switch to the directory with unpacked files:

cd /home
wget https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tar.xz
tar xf Python-2.7.12.tar.xz
cd ./Python-2.7.12

Pay attention to the name of the folder with unpacked files. It will be the same as the archive name (without the archive extension).

Compile Python

Now we can build the updated Python binaries:

./configure
make
make install

Switch to the new version

If you check the Python version now, you still see the same old one. So we need to point the default Python interpreter path to the new python binary, which was installed to /usr/local/bin/python.

update-alternatives --install /usr/bin/python python /usr/local/bin/python 10

Now you will see the installed version:

python --version
Python 2.7.12

Differences for Python 3

Python 3 can be updated in the same fashion, with one simple difference - you need to specify python3 instead of python. Note that Python 3 doesn't come pre-installed on Debian 7.

Check version:

python3 --version

Switch to new version:

update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3 10

When updating Python 3, remember to choose a corresponding archive from the Python FTP site.

Want to contribute ?

You could earn up to $300 by adding new articles