How to Compile & Install Python 3.11/3.10 from Source in Ubuntu

Last updated: October 3, 2023

This simple tutorial shows how to compile and install Python 3.11, Python 3.10, or other certain Python version in Ubuntu.

For Ubuntu 18.04, Ubuntu 20.04 and Ubuntu 22.04 LTS, there’s well trusted “deadsnakes” team PPA that maintains all the Python packages. For non-LTS releases, you may build the programming language package manually from the source tarball.

NOTE: This tutorial is tested and works in Ubuntu 20.04, Ubuntu 22.04, and Ubuntu 23.04 in my case.

1. Preparation:

Before getting started, you need to install some essential packages for building the computer language package.

Open terminal either by pressing Ctrl+Alt+T on keyboard or by searching from start menu. When it opens, run the command below to install the dependencies:

sudo apt install wget build-essential libreadline-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev

2. Download Python Tarball:

You can simply download the latest tarball from the Python website via the link below:

In case you’re running Ubuntu without desktop environment, download the source package by running the wget command in terminal:

wget -c https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tar.xz

For other Python version, go and download via web browser in this page. And, change the version number in command accordingly.

And uncompress the tarball either by right-click and selecting ‘Extract Here‘ in file manager, or by running command in terminal:

tar -Jxf Python-3.11.0.tar.xz

3. Configure the source:

Depends on how you grab the source tarball, either right-click on it in file manager, extract, and open the source folder in terminal via context menu option:

Open Python 3.11 source folder in terminal

If you just did the tar command above, then you may navigate to source folder via:

cd Python-3.11.0/

The command varies depends on which Python version you downloaded.

When you’re in source directory, run command to configure the source with expensive, stable optimizations (PGO, etc.):

./configure --enable-optimizations

4. Build and install Python:

Finally compile and install it into “/usr/local/bin” by running command:

sudo make -j4 && sudo make altinstall

Here -j4 will start 4 threads to speed up the job. Depends on your CPU, you may increase the number or just skip it. And, when make command done, it will ask you to type password for running make install with sudo privilege.

5. Verify

If everything’s done successfully without error, you may verify by running command:

python3.11 --version
python3.11 -m pip --version

It should output the language version as well as the pip package manager version for you

5. Make Python 3.11 default

IMPORTANT: Change default Python3 in Ubuntu MAY cause issues!!! Do it at your own risk.

After installation, you may set it as default by creating symbolic links to /usr/bin/python3:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.10 110
sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.11 100

In the first command change system default python3.10 to python3.8 if you’re on Ubuntu 20.04. Finally, run command below at any time to select which Python to use as default:

sudo update-alternatives --config python3

Uninstall Python 3.11:

Until you removed the source directory, you may first either navigate to that folder in terminal or right-click and select open folder in terminal, finally run make uninstall command to remove Python 3.11:

sudo make uninstall

If you’ve already removed the source, or the previous command does not work for you, try removing all the installed libraries manually via:

cd /usr/local/bin && rm -f sudo rm -f 2to3 2to3-3.11 idle3 idle3.11 pip pip3 pip3.10 pip3.11 pydoc3 pydoc3.11 python3 python3.11 python3.11-config python3-config

Twitter

I'm a freelance blogger who started using Ubuntu in 2007 and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please comment to let me know if the tutorial is outdated! And, notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me via ubuntuhandbook1@gmail.com Buy me a coffee: Buymeacoffee | https://ko-fi.com/ubuntuhandbook1 |

12 responses to How to Compile & Install Python 3.11/3.10 from Source in Ubuntu

  1. Awsome!
    To the point with no extras.
    Thank you,

  2. Jerry Yang(Korea) October 17, 2021 at 7:17 am

    Thanks. perfect~!!

  3. Hey, so I’ve installed python but can’t seem to figure out how to launch the app.
    Any help would be appreciated!

  4. Perfect tutorial!! Keep up the great work!!

  5. how to uninstall?

  6. A Random Try Hard Linux User December 9, 2021 at 5:22 am

    If I use the second method in uninstalling Python 3.10.0, that’s it, right?

  7. Thanks for the steps. It really helped

  8. I used this instruction to install Python 3.10.4 (of course, I had to replace .0 with .4) and it worked great. Thank you!

  9. Hello, thanks for the tutorial, I was using python 3.10.6 and updated to ubuntu 23.04 which installed 3.11 and things didn’t work out for me. Thanks to you I could easily make an alternative install, and launch what I wanted with 3.10, but I now get this error
    ” import lzma
    File “/usr/local/lib/python3.10/lzma.py”, line 27, in
    from _lzma import *
    ModuleNotFoundError: No module named ‘_lzma'”

    I also tried another version just to see but have the same problem with 3.10.12 for exemple

    What can cause it and how do I help it?

  10. Nice, danke für deine job.

  11. liblzma-dev should be added to the apt-get install command