Skip to content

Instantly share code, notes, and snippets.

@ddxv
Last active February 21, 2024 13:19
Show Gist options
  • Save ddxv/afc223b371e79b18fa42cbbab7867495 to your computer and use it in GitHub Desktop.
Save ddxv/afc223b371e79b18fa42cbbab7867495 to your computer and use it in GitHub Desktop.
Install Python3.12 on Ubuntu 20+ and 23.10

Prep - Linux (mainly for new servers)

You may need a C compiler if you haven’t yet downloaded: sudo apt update && sudo apt install build-essential

Ubuntu 22.04 / older

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev pkg-config make -y

Ubuntu 23.10

sudo apt install libpq-dev

Digital Ocean extras for 23.10

Note: pkg-list and zlib1g-dev seemed necessary to build, the others for modules like lzma, _ctypes (for pandas) etc sudo apt install pkg-list zlib1g-dev uuid-dev lzma-dev liblzma-dev libffi-dev

Installation - Linux

Download the latest version:

Choose Gzipped Tarball https://www.python.org/downloads/release/python-3120/

  1. tar -xvf UNZIPPEDPYTHONFILE you will now have a directory like Python3.12.xxx
  2. sudo mv Python3.12.0 /opt/ Move python installation directory to /opt/ to keep home clean
  3. cd ~/opt/Python3.12.0 into the directory and run each command
  4. ./configure --enable-optimizations
  5. make Builds Python, this step takes some time.
  6. sudo make altinstall altinstall skips creating the python link and the manual pages links, install will hide the system binaries and manual pages. This means that we will leave the system python installation untouched

Python is now installed.

Last important step is to configure the system links: sudo ldconfig /opt/Python3.12.0

Finally you can test:

python3.12

Or make a 3.12 virtual env: python3.12 -m venv ~/venv/my-new-env or local for a VS Code: python3.12 -m venv .virtualenv

Be careful which commands use sudo as running the ./configure or first make as sudo will cause Python to have files which require elevated permissions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment