How to Install Python 3.12 (Beta so far) in Ubuntu 22.04 | 20.04 | 23.04

For developers who want to prepare their project for the latest Python releases, here’s how to install Python 3.12 in all current Ubuntu releases.

Python 3.12 now is in Beta development stage. It features more flexible f-string parsing, new type annotation syntax for generic classes, support for the Linux perf profiler, and many performance improvements, but removed the distutils package and wstr from Unicode. See more about Python 3.12.

How to Install Python 3.12

Python is easy to install in Ubuntu by either using the popular Deadsnakes PPA or building from the source. Choose either one that you prefer.

Option 1: Install Python 3.12 from PPA

For Ubuntu 22.04, Ubuntu 20.04, and their derivatives such as Linux Mint 21, the Deadsnakes PPA has made the packages for all supported CPU architecture types: amd64, arm64/armhf, ppc64el, and s390x.

1. First, press Ctrl+Alt+T on keyboard to open terminal. Then paste the command below and hit run to add PPA:

sudo add-apt-repository ppa:deadsnakes/ppa

Type user password (no asterisk feedback) when it asks and hit Enter to continue.

2. Ubuntu 20.04+ automatically refresh package cache while adding PPA. However, Linux Mint user may need to do this job manually by running command:

sudo apt update

3. Finally, run command to install Python 3.12:

sudo apt install python3.12

Option 2: Compile and install Python 3.12 from source

Don’t trust third-party repositories or you’re running Ubuntu 23.04 or Ubuntu 18.04? It’s easy to build Python from the source tarball.

1. First download the source tarball from its ftp download page:

Python FTP Download Page

2. Then open ‘Downloads’ folder, extract the source tarball, finally right-click on source folder and select “Open in Terminal”.

3. When terminal opens, run the commands below one by one to configure and build Python:

./configure --enable-optimizations
sudo make -j4 && sudo make altinstall

NOTE: You have to first install all build dependency libraries before running last 2 commands. See this tutorial for details.

Verify:

Once installed Python 3.12, verify by running command:

python3.12 --version && pip3.12 --version

Set Python 3.12 as default

It’s NOT recommended to set non-preinstalled Python package as default for Python3, since it will break some core applications.

However, you may try to set python3.12 as python by running the commands below one by one:

  • First, run command to find out where Python 3.12 executable is installed to:
    whereis python3.12

    It’s either /usr/bin/python3.12 or /usr/local/bin/python3.12.

  • Then, add Python 3.12 as an alternative link to python (replace /usr/local/bin/python3.12 according last command output).
    sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.12 1
  • Finally, run command to set default for /usr/bin/python executable if more then one available:
    sudo update-alternatives --config python

Uninstall Python 3.12:

If you installed Python 3.12 using the PPA repository, simply open terminal and run command to remove it:

sudo apt remove --autoremove python3.12

For the PPA, run command to remove it:

sudo add-apt-repository --remove ppa:deadsnakes/ppa

If you built the Python 3.12 from source tarball, then there’s no uninstaller script to automate the job.

However, you may manually remove the installed files by running commands:

cd /usr/local/bin && sudo rm python3.12* pip3.12 idle3.12 pydoc3.12 2to3-3.12
cd /usr/local/lib && sudo rm -R python3.12 pkgconfig libpython3.12.a

Original Article