Compiling Python 3.8.3 on Ubuntu 18.04

How to compile Python 3.8.3 on a Ubuntu 18.04 system and set up a virtual environment with venv.

Instructions

Download the XZ compressed source tarball (the download link is from the Python 3.8.3 release page).

Unpack the tarball with tar -xf Python-3.8.3.tar.xz and cd into the Python-3.8.3 directory.

Read ./configure to figure out what features you want and run ./configure with appropriate parameters, then make and make altinstall. This is what I settled on:

./configure --enable-optimizations --with-lto --with-system-expat --with-ensurepip=yes --enable-loadable-sqlite-extensions --enable-ipv6 --prefix=/home/thomas/prg/python/3.8.3
make
make altinstall

Because of the --prefix parameter, the path to my freshly built Python binary is

~/prg/python/3.8.3/bin/python3.8

I like to run Python in a virtual environment using venv and a nice alias:

~/prg/python/3.8.3/bin/python3.8 -m venv ~/.venv/p383
alias p3='source ~/.venv/p383/bin/activate'

Might as well upgrade pip:

p3 # the alias defined above
pip install --upgrade pip

Troubleshooting “The necessary bits to build these optional modules were not found”

The first time I ran ./configure and make I got the message:

The necessary bits to build these optional modules were not found:
_dbm                  _gdbm                 _sqlite3
_uuid                 readline

Apparently I was missing a couple of packages. I added the following:

sudo apt install libreadline-dev uuid-dev libsqlite3-dev libgdbm-dev

And with that, make could build all optional modules except _dbm.

Loose ends

I’m left with a couple of loose ends:

  • I couldn’t build the _dbm module.
  • I tried calling ./configure with --with-system-ffi but I got a warning that it wouldn’t work on my system so I removed it.
  • I tried calling ./configure with --enable-shared but this caused make to fail with a weird tokenization error.

I’ll dig into this next time I compile Python, if I remember.

References

Python 3.8.3 Documentation - Using Python on Unix platforms