Categories
python ubuntu

How to build python 3 from source on Ubuntu 22.04?

  1. Install pre-requisites

    sudo apt install build-essential libssl-dev libffi-dev software-properties-common libbz2-dev libncurses-dev libncursesw5-dev libgdbm-dev liblzma-dev libsqlite3-dev tk-dev libgdbm-compat-dev libreadline-dev
  2. Download the python version you want from https://www.python.org/downloads/source/

    cd /opt
    sudo wget https://www.python.org/ftp/python/3.9.16/Python-3.9.16.tar.xz
  3. Extract the downloaded source

    sudo tar -xvf Python-3.9.16.tar.xz
  4. Read the info

    cd Python-3.9.16/
    cat README.rst
  5. Compile and Install python

    ./configure
    make
  6. When running make you may get a message similar to the below:

Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2                  _curses               _curses_panel      
_dbm                  _gdbm                 _lzma              
_sqlite3              _tkinter              _uuid              
readline              zlib                                     
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
Ideally these modules are installed with apt:

    sudo apt libbz2-dev libncurses-dev libncursesw5-dev libgdbm-dev liblzma-dev libsqlite3-dev tk-dev libgdbm-compat-dev libreadline-dev
  1. Install python into the path:

    make test # optional
    sudo make install
  2. This installs the python binary to /usr/local/bin/python3

Sources