How to install Python 3.6?

You can install Python-3.6 on Debian 8 as follows:

wget https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz
tar xvf Python-3.6.9.tgz
cd Python-3.6.9
./configure --enable-optimizations --enable-shared
make -j8
sudo make altinstall
python3.6

It is recommended to use make altinstall according to the official website.

If you want pip to be included, you need to add --with-ensurepip=install to your configure call. For more details see ./configure --help.

Warning: make install can overwrite or masquerade the python binary. make altinstall is therefore recommended instead of make install since it only installs exec_prefix/bin/pythonversion.

Some packages need to be installed to avoid some known problems, see: Common build problems(updated)

Ubuntu/Debian:

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev

Alternative of libreadline-dev:

sudo apt install libedit-dev

Fedora/CentOS/RHEL(aws ec2):

sudo yum install zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel \
openssl-devel xz xz-devel libffi-devel

Alternative of openssl-devel:

sudo yum install compat-openssl10-devel --allowerasing

Update

You can download the latest python-x.y.z.tar.gz from here.

To set a default python version and easily switch between them , you need to update your update-alternatives with the multiple python version.

Let's say you have installed the python3.7 on debian stretch , use the command whereis python to locate the binary (*/bin/python). e,g:

/usr/local/bin/python3.7
/usr/bin/python2.7
/usr/bin/python3.5

Add the python versions:

update-alternatives --install /usr/bin/python python /usr/local/bin/python3.7 50
update-alternatives --install /usr/bin/python python /usr/bin/python2.7 40
update-alternatives --install /usr/bin/python python /usr/bin/python3.5 30

The python3.7 with the 50 priority is now your default python , the python -V will print:

Python 3.7.0b2

To switch between them, use:

update-alternatives --config python

Sample output:

There are 3 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                      Priority   Status
------------------------------------------------------------
* 0            /usr/local/bin/python3.7   50        auto mode
  1            /usr/bin/python2.7         40        manual mode
  2            /usr/bin/python3.5         30        manual mode
  3            /usr/local/bin/python3.7   50        manual mode

Press <enter> to keep the current choice[*], or type selection number: 

Editorial note:
Warning: this answer shows how to install Python from future releases of Debian. This will result in a system which mixes Debian releases, and will affect more than Python — in most cases, applying these instructions will pull in newer libraries too. The resulting setup won’t benefit from security updates with the same speed as might be expected, for packages which are updated. This is known as a FrankenDebian.

Consider the other answers to this question instead, in particular this one showing how to build from source, and this one showing how to use virtual environments.


Debian does not have Python 3.6 in its repositories, but testing has it.

$ sudo nano /etc/apt/sources.list
# add
deb http://ftp.de.debian.org/debian testing main
$ echo 'APT::Default-Release "stable";' | sudo tee -a /etc/apt/apt.conf.d/00local
$ sudo apt-get update
$ sudo apt-get -t testing install python3.6
$ python3.6 -V

You asked for:

the proper and officially accepted procedure

but I must point it out that this is not official solution because it uses testing repositories.


The official recommendation is "you don't actually need newer software"

Don't suffer from Shiny New Stuff Syndrome - DontBreakDebian | Debian Wiki

Most of the advice on that page is geared towards what to do if you want the software to be available system-wide, but I don't think that's necessary in this case.

If you fetch the python sources, build the 3.6 interpreter using --prefix to control where it ends up, and then use virtualenv with the --python option, then you can use python 3.6 without affecting anything outside your project.

The process might go something like this:

$ cd ~
$ mkdir pythonroot
$ mkdir opt
$ mkdir app
$ cd opt
$ wget <python tarball>
$ tar -xvf <python tarball>
$ cd python-3.6
$ ./configure --prefix="$HOME"/pythonroot
$ make
$ make install
$ cd ~
$ cd app
$ virtualenv venv --python ~/pythonroot/bin/python
$ . venv/bin/activate
[venv]$ which python
/home/<user>/pythonroot/bin/python

If you're going to do this, you may want to consider the --enable-optimizations flag in the Python configure script, which seems to enable some features like profile-guided optimization. It increases build times but seems to result in a faster interpreter by 10% or so according to some benchmarks.