How to install the latest Python version on Debian separately or upgrade?

Ok after a lot of searching I decided to build Python from source, so I downloaded the compressed source tarball from the Python download page, now we need to install the build-essential package to be able to compile the source files:

apt-get install build-essential

also we need to install these development packages which are required for some Python modules:

libbz2-dev
libsqlite3-dev
libreadline-dev
zlib1g-dev
libncurses5-dev
libssl-dev
libgdbm-dev

next we extract the downloaded source file:

tar zxf Python-2.7.6.tgz

then we cd into the extracted directory:

cd Python-2.7.6

and follow the instructions on the Python docs:

./configure --prefix=/opt/python
make
make install # <- in the docs but don't execute: use checkinstall

I chose to install it under the directory /opt/python which I created before, also I used the checkinstall package to create a .deb file so I can uninstall easily later, so we install it first:

apt-get install checkinstall

and substitute the last line make install with:

checkinstall

now I have a new python installation in /opt/python/lib/python2.7 and the binary file is in /opt/python/bin/python2.7.

now I can run in the command line /opt/python/bin/python2.7 to use this Python version, also we can make a link to this executable so we don't type the full path, I named it python2.7.6.

ln -s /opt/python/bin/python2.7 /usr/local/bin/python2.7.6

also the directory /opt/python/bin will contain later other executables like pip and virtualenv or any other modules you install so we can add it to the PATH environment variable, edit ~/.profile and add this line:

PATH="$PATH:/opt/python/bin"

and run:

source ~/.profile

I appended the path to the end because it contains executable names like the ones in /usr/bin like python, python2 and python2.7 so we keep the higher priority for /usr/bin.


You probably are looking for virtualenv or pyenv or some other non-system-wide method to install Python. The method using APT (Advance Package Tool) and dpkg, ensures that all parts of the system are working in harmony, so you maybe want to install python in a separated path, hidden of all the other programs that you can call at will, which is the purpose of pyenv/virtualenv. This answers how to install the latest version of python without breaking the system.

BTW, you can check out the latest version of python that Debian in madison, while the latest version of python 2 at the date is the one you pointed out:

➜  ~  apt-cache policy python
python:
  Installed: 2.7.5-5
  Candidate: 2.7.5-5
  Version table:
 *** 2.7.5-5 0
        500 http://ftp.us.debian.org/debian/ testing/main i386 Packages
        100 /var/lib/dpkg/status

(pythonbrew is not longer maintained).