Installing Python 3.7

Dragon lady by Firkin

“Here be dragons, they ate the yaks”

When we refer to “Python” in this guide, we will be referring to any version of Python equal to or greater than version Python 3.7. But we will try to be specific.

Note

You will not be required to install Python on your host operating system (the one installed on your laptop) as you will be provided with a Ubuntu 18.04 Long-Term Support (LTS) “Bionic Beaver” virtual machine provided as an OVA with PyCharm Community Edition. This section has been included for completeness.

Warning

The Python Software Foundation plan to continue to provide bug-fix releases for 3.6.x though to at least the end of 2018 and security fixes through 2021.

Warning

Python 2.7 will not be maintained past January 1, 2020.

Installation on Windows

Installation on Windows 7+

If you are running Windows 7+ we are going to install Python via Chocolatey, a community system package manager for Windows similar to Homebrew on OS X.

The instructions on how to install Chocolatey can be found on the Chocolatey website.

1. To install the latest version of Python run choco install python as Chocolatey pushes Python3 as the default version of Python.

Chocolatey will automatically add Python to your PATH so you don’t need to worry about configuring that. In addition, pip and setuptools are both installed by default.

Installation on pre-Windows 7

If you are using a version of Windows prior to Windows 7 you will need to install Python from source, by following the instructions below:

  1. Visit python.org downloads and download the latest version. At the time of writing (3 April 2019) this was Python 3.7.3 <https://www.python.org/downloads/release/python-373/>.

  2. To change install location, click on Customize installation, then Next and enter C:\python37 (or another appropriate location) as the install location.

  3. If you did not check the Add Python 3.7 PATH option earlier, check Add Python to environment variables. This does the same thing as Add Python 3.7 to PATH on the first install screen.

  4. You can choose to install Launcher for all users or not, either option is fine, as it simply allows you to switch between different versions of Python installed.

If you set the PATH variable properly, you will be able to run the interpreter from the command line.

  1. To open the terminal in Windows, click the start button and click Run. In the dialog box, type cmd and press [ENTER] key.

  2. Once the terminal is open type python and you will be dropped into a Python interpreter.

Installation on GNU/Linux

There is a very good chance your Linux distribution has Python installed, however, in most cases it probably won’t be the latest versions, and in many cases it will be Python 2.7 instead of Python 3.x.

Attention

If your system comes with Python 2.x.x installed by default do not under any circumstances uninstall this version. In many cases, a lot of packages still depend on these versions of Python and uninstalling it may leave you with a very minimal install of your operating system.

To find out what version(s) you have, open a terminal window and try the following commands:

  • python --version

  • python2 --version

  • python3 --version

One or more of these commands should respond with the version, as shown in the example below:

% python --version
Python 2.7.13

If the version is Python 2.x.x or Python 3.x.x is not installed or is not Python 3.7.x at the latest, then you will want to install the latest version. How you do this will depend on the flavour of Linux you are currently using. We will try to summarise the major distributions for you.

Ubuntu

Depending on which version of Ubuntu you are running, the Python installation instructions will vary. You can determine the version of Ubuntu you are running by running the following command:

% lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.4 LTS
Release:        16.04
Codename:       xenial

Ubuntu 17.10/Ubuntu 18.04+

Comes with Python 3.7 by default, you can invoke it by opening a terminal window and typing the following:

% python3

Ubuntu < 17.10/18.04+

Does not come with Python 3.7 by default, but is available for download (and installation) via apt. You can install it but opening a terminal window and following the instructions below:

Step One: Download Source Code

% mkdir /usr/src
% cd /usr/src
% wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz

Step Two: Prepare Your System

You will first want to make sure your package manager is up to date, on Ubuntu we do this by running the following commands:

$ sudo apt-get update
$ sudo apt-get upgrade

Once this step successfully completes, we will need to make sure we have all the tools and libraries we need to build Python.

$ sudo apt-get install build-essential checkinstall
$ sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev \
libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev

Step Three: Build Python

Once you have successfully installed all the required packages, you can unpack the source into a directory. Without getting too much into Linux directory internals I’d suggest unpacking the directory into /usr/local/bin the commands you will want to run in a terminal windows is shown in the code block below. However, before we blindly run into that screaming about Pythons and other slithery delights, let’s walk-through what’s going on on lines, 3, 4 and 5.

1
2
3
4
5
6
7
$ sudo tar xzf Python-3.7.3.tgz --directory /usr/local/bin
$ sudo cd /usr/local/bin/Python-3.7.1
$ sudo ./configure --enable-optimizations --with-ensurepip=install
$ sudo make -j 8
$ sudo make altinstall
% cd ..
% cd ~

Line 3

% sudo ./configure --enable-optimizations --with-ensurepip=install

The configure script is responsible for getting the software ready to build on your system. It makes sure dependencies are met and ensures the software is installed just the way you want. It’s sort of like adding walnuts into the banana bread batter just before you put it in the oven.

If you are curious to see what a configure file looks like you can check out the Python one on GitHub - there is absolutely no reason you need to do this and frankly this is the authors first time looking at the file as well.

Tip

There are many (like lots) of options available when building Python if you are ever curious about these you can run ./configure --help

Line 4

$ sudo make -j 8

We then build the Python programs using make. The -j flag tells make to split the build into parallel steps to help speed up the time it takes to compile - however, keep in mind that even with parallel builds this can still take a few minutes.

Line 5

$ sudo make altinstall

Then we can install our new version of Python, however, unlike building other programs we use altinstall instead of make install this is to ensure we do not overwrite the systems version of Python, which on Debian is catastrophic (the author has done this more than once).

Warning

Only use the altinstall target on make. Using the install target will overwrite the python binary. While it might seem like a good idea to try and upgrade the system version of Python to 3.7.1 there are significant parts of Ubuntu that rely on the pre-installed system version, which cannot be (easily) repaired later!

You can then invoke Python by opening a terminal window and typing the following:

% python3.7

Mint

Linux Mint and Ubuntu use the same package management system, you can follow the instructions above for Ubuntu < 17.10/18.04+ .

Debian

Depending on which version of Debian you are running, the Python installation instructions will vary. You can determine the version of Debian you are running by running the following command:

lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 9.5 (stretch)
Release:    9.5
Codename:   stretch

Debian 9 (stretch)

Configuring sudo

Typically (read: read all the time) sudo is not installed on Debian, so before we install Python we will want to install sudo.

Hint

sudo is short for “super user do!” and is pronounced like “sue dough”.

Note

sudo is considered safer than opening a session as a root user for a number of reasons, these include:

  • Nobody needs to know the root password

  • It’s easy to run only the commands that require special privileges via sudo, which reduces the damage that accidentally using the root account can cause

  • When a sudo command is executed the original username and command are logged

Now even though we just talked about why using sudo is better than logging into the root account, because our account currently has no permissions we have to log in as root to install sudo. Open a terminal window and typing the following:

$ su
$ apt-get install sudo

Option One: Configure sudo using adduser

This is the Debian suggested way. To add an existing user to the sudo group, open a terminal window and typing the following, remembering to change your-username to your actual username:

$ adduser your-username sudo
$ exit

Option Two: Configure sudo using visudo

This option will open the /etc/sudoers file for editing (in a safe fashion). To add yourself to the sudoers file, you will want to append the following text to the end of the file, remembering to change your-username to your actual username:

$ visudo
your_username ALL=(ALL) ALL

After being added to a new group you much log out and then log back in for the new group to take effect.

Hint

To learn more about sudo and how to debug common problems see the Debian Sudo page at https://wiki.debian.org/sudo

Installing Python 3.7.3

Step One: Download Source Code

% mkdir /usr/src
% cd /usr/src
% wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz

Step Two: Prepare Your System

You will first want to make sure your package manager is up to date, on Ubuntu we do this by running the following commands:

$ sudo apt-get update
$ sudo apt-get upgrade

Once this step successfully completes, we will need to make sure we have all the tools and libraries we need to build Python.

$ 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

Step Three: Build Python

Once you have successfully installed all the required packages, you can unpack the source into a directory. Without getting too much into Linux directory internals I’d suggest unpacking the directory into /usr/local/bin the commands you will want to run in a terminal window is shown in the code block below. However, before we blindly run into that screaming about Pythons and other slithery delights, let’s walk-through what’s going on on lines, 3, 4 and 5.

1
2
3
4
5
6
7
$ sudo tar xzf Python-3.7.3.tgz --directory /usr/local/bin
$ sudo cd /usr/local/bin/Python-3.7.1
$ sudo ./configure --enable-optimizations --with-ensurepip=install
$ sudo make -j 8
$ sudo make altinstall
% cd ..
% cd ~

Line 3

% sudo ./configure --enable-optimizations --with-ensurepip=install

The configure script is responsible for getting the software ready to build on your system. It makes sure dependencies are met and ensures the software is installed just the way you want. It’s sort of like adding walnuts into the banana bread batter just before you put it in the oven.

If you are curious to see what a configure file looks like you can check out the Python configuration file on GitHub - there is absolutely no reason you need to do this and frankly this is the authors first time looking at the file as well.

Tip

There are many (like lots) of options available when building Python if you are ever curious about these you can run ./configure --help

Line 4

$ sudo make -j 8

We then build the Python programs using make. The -j flag tells make to split the build into parallel steps to help speed up the time it takes to compile - however, keep in mind that even with parallel builds this can still take a few minutes.

Line 5

$ sudo make altinstall

Then we can install our new version of Python, however, unlike building other programs we use altinstall instead of make install this is to ensure we do not overwrite the systems version of Python, which on Debian is catastrophic (the author has done this more than once).

Warning

Only use the altinstall target on make. Using the install target will overwrite the python binary. While it might seem like a good idea to try and upgrade the system version of Python to 3.7.1 there are significant parts of Ubuntu that rely on the pre-installed system version, which cannot be (easily) repaired later!

You can then invoke Python by opening a terminal window and typing the following:

% python3.7

Installation on OSX

Before installing a newer version of Python you will need to install the GNU Compiler Collection (GCC). GCC can be installed by downloading and installing Xcode.

Caution

If you already have Xcode installed, do not install OSX-GCC-Installer. In combination, the software can cause issues that are difficult to diagnose.

Additionally, if you’ve been a long time OS X user, you will know while it comes with a number of UNIX like utilities it is missing one key feature - a package manager, so first up if you haven’t already, you’ll want to install Homebrew.

The instructions on how to install Homebrew can be found on the Homebrew website.

  1. To install the latest version of version of Python run brew install python3.

  2. To verify the installation worked, open a terminal by pressing [COMMAND + SPACE] keys (or manually open Spotlight search) and type Terminal and press [ENTER].

  3. Once the terminal is open type python3 and you will be dropped into a Python interpreter.

Hint

To install previous versions of Python 3.x.x you will need to visit the Official Python website and manually download and install them as Homebrew does not provide other major versions.