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