Ubuntu 16.04 by default comes with two versions of python, Python 2.7.X and Python 3.5.X. If you want to check current version of python installed on your Ubuntu please use following commands
$ python --version
Python 2.7.12
$ python3 --version
Python 3.5.2
There are two methods to install python 3.6 on ubuntu, one using ppa and second compiling source code on your machine. We will use the second method.
Step#1 Install build dependencies
$sudo apt install build-essential checkinstall
$sudo apt install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
Step#2 Download python source code from python.org
$wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
Step#3 Extract the downloaded file
$tar xvf Python-3.6.4.tar.xz
This will create a folder Python-3.6.4/
Step#4 Configure and build environment to install
Now cd into newly created python folder and use ‘./configure’ command to do configuration.
$ cd Python-3.6.4/
$ ./configure
You will see messages like this
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
.
.
.
.
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Modules/Setup.config
config.status: creating Misc/python.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup
creating Modules/Setup.local
creating Makefile
If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimisation
Ubuntu 16.04 be default uses Python 3.5 version to make sure latest version of python does not disturb existing installation, use following command.
$sudo make altinstall
On successful install you will see following message
.
.
.
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-9.0.1 setuptools-28.8.0
To verify if python version installed correctly, use following command
$ python3.6 --version
Python 3.6.4