Install Python 3.6 on Ubuntu 14.04 and 16.04 LTS

A regular Ubuntu release comes up with 9 months of support, except the LTS (Long Term Support) versions.  Ubuntu 14.04 and 16.04 being the LTS are still widely in use at production level. Being a Python developer the first thing I need to on having a fresh Ubuntu 14.04, or 16.04 machine is update Python. Ubuntu 14.04 has Python 3.4 and 16.04 comes with Python 3.5. This blog post is about installing Python 3.6 on your Ubuntu 14.04, or 16.04 LTS.


Step 0 – Install the pre-requisites (optional)

The following step are optional, as to cater post-installation issues i.e. “zipimport.ZipImportError: can’t decompress data; zlib not available” for the missing zlib-dev, and also preparing environment for Python development.
$ sudo apt-get update
$ sudo apt-get install build-essential libpq-dev libssl-dev openssl libffi-dev zlib1g-dev
$ sudo apt-get install python3-pip python3-dev

Latest Ubuntu 17.10 already comes with Python 3.6 installed, can verify by executing python3.

For Ubuntu 16.10 and 17.04

Python 3.6 is available for install via the main repository, all you have to do is
$ sudo apt-get update
$ sudo apt-get install python3.6

For Ubuntu 14.04 and 16.04 LTS

Python 3.6 is not available for direct install, you have to either add a repository and install, or simply download and install. You can verify if Python 3.6 is available for direct install
$ apt-cache search python3.6

Method 1 – using PPA

PPA (Personal Package Archives) allows contributor to upload and share Ubuntu source packages to be built and published at an apt repository by Launchpad. There has been always an open debate regarding the PPA red flags while using a PPA, main important point to consider is, who made the PPA. For Python 3.6 J Fernyhough’s PPA, is widely used and reliable.
$ sudo add-apt-repository ppa:jonathonf/python-3.6
$ sudo apt-get update
$ sudo apt-get install python3.6

Method 2 – Download and configure

$ wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
$ tar -xvf Python-3.6.3.tgz
$ cd Python-3.6.3
$ sudo ./configure --enable-optimizations
If above configure ends up with no errors, execute the following commands to complete the installation of Python 3.6
$ sudo make -j8
$ sudo make install

Verification

Execute the python3.6
nahmed@localhost:~$ python3.6
Python 3.6.3 (default, Oct  6 2017, 08:44:35)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Creating virtualenv using Python 3.6 (optional)

Given that you have more than one Python 3 versions installed, and need to use Python 3.6 for your project. Best practice is to create a virtual environment using Python 3.6

Install virtualenv

$ sudo pip3 install virtualenv

Create virtualenv

$ virtualenv -p /usr/bin/python3.6 ~/virtualenvs/venv_devopspy
$ source virtualenvs/venv_devopspy/bin/activate
(venv_devopspy) nahmed@localhost:~$

Leave a Reply

Your email address will not be published. Required fields are marked *