How To

Install Python 3.7 on Ubuntu 18.04: apt and Source Build Methods

Python 3.7 was a significant release for the language. It introduced data classes, a decorator that automatically generates common methods like __init____repr__, and __eq__ from the fields you define, removing a lot of repetitive code. It also added postponed evaluation of type annotations, context variables through the contextvars module, and more predictable module attribute access.

Ubuntu 18.04 ships with Python 3.6 by default. Python 3.7 is not in the standard repositories, so you need to add a third-party PPA or compile it from source. This guide covers both options. The same steps work on Ubuntu 16.04 and other Ubuntu-based distributions.

<strong>Prerequisite:</strong>&nbsp;You need sudo access.

If you just need Python 3.7 up and running quickly and are comfortable with third-party repositories, use Method 1. It installs in a few minutes and receives automatic security updates through the PPA. If you are on a controlled server without PPA access, Method 2 gives you a fully independent build.

Method 1: Install Python 3.7 on Ubuntu Using apt

The deadsnakes PPA maintains current Python packages for Ubuntu and is the simplest way to install Python 3.7 without any compilation.

Update the package list and install the prerequisites:

bashsudo apt updatesudo apt install software-properties-common

Add the deadsnakes PPA:

bashsudo add-apt-repository ppa:deadsnakes/ppa

Press Enter when prompted to confirm.

Install Python 3.7:

bashsudo apt install python3.7

Verify the installation:

bashpython3.7 --version

Output:

Python 3.7.3

Python 3.7 is now available on your system alongside the default Python version. Run python3 --version to confirm your system Python is still intact.

Method 2: Build Python 3.7 from Source

Building from source gives you a self-contained Python 3.7 install with no dependency on any external PPA. This is the right choice for locked-down server environments or when you want full control over the build configuration.

Install the required build tools and development libraries:

bashsudo apt updatesudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev wget libbz2-dev

Download the Python 3.7 source code:

bashwget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz

Extract the archive:

bashtar -xf Python-3.7.4.tgz

Switch into the source directory and run the configure script. The --enable-optimizations flag runs a series of internal benchmark tests to fine-tune the Python binary. It slows the build but produces a faster interpreter:

bashcd Python-3.7.4./configure --enable-optimizations

Start the build. The -j flag sets the number of parallel compile jobs. Match it to your CPU core count for the fastest build time. To check how many cores your system has, run nproc:

bashmake -j 8

Install the binaries using altinstall:

bashsudo make altinstall

Always use altinstall instead of the standard make install. The standard command overwrites the python3 symlink on your system, which can break tools and packages that depend on the default Python version.

Verify the installation:

bashpython3.7 --version

Output:

Python 3.7.4

Once installed, Python 3.7 is accessible as python3.7 and sits alongside your system Python without replacing it. Install pip separately with python3.7 -m ensurepip if you need package management for this version.

Python 3.7 is now installed on your Ubuntu 18.04 machine. From here, set up pip for package management and create Python virtual environments to keep your project dependencies isolated. Leave a comment below if you run into any build or installation errors.

Cyber Defence

Recent Posts

Install Odoo 13 on Ubuntu 18.04: Python Venv and Nginx Guide

Odoo is a popular open-source suite of business applications. A single platform covers CRM, e-commerce, accounting,…

2 hours ago

Secure Apache with Let’s Encrypt on Ubuntu 18.04: Free SSL Guide

Let's Encrypt is a free, automated certificate authority run by the Internet Security Research Group…

2 hours ago

Install GCC on Ubuntu 18.04: C and C++ Compiler Setup Guide

GCC (GNU Compiler Collection) is a set of compilers and development libraries for C, C++, Fortran,…

2 hours ago

Install Python 3.8 on Ubuntu 18.04: apt and Source Build Methods

Python is one of the most widely used programming languages in the world. Its clean, readable…

2 hours ago

Install Tomcat 9 on Ubuntu 18.04: Systemd Service Setup Guide

Apache Tomcat is an open-source Java application server that implements the Java Servlet, JavaServer Pages, and…

1 day ago

Change Timezone on Ubuntu 18.04: Command Line and GUI Methods

Setting the correct timezone on your Ubuntu machine is more important than it sounds. Your…

1 day ago