How to install Python 3.9 on Ubuntu 20.04 | 18.04

This brief tutorial shows students and new users how to install and configure Python 3.9 on Ubuntu 20.04 | 18.04.
Python, a general-purpose programming language which is versatile and popular can easily be installed on Ubuntu via multiple methods. Python 3.9 is the latest release of Python programming language.
This release brings many new features including, assignment expression — which assigns values to variables as part of a larger expression, position-only parameter, f-strings support and many more.
This post will also show you how to install Python via a third-party PPA which makes managing and updating to future versions easier from the PPA repository.
With Python, you can do almost anything like writing simple or advanced scripts, build and program robots and complicated machineries, develop websites and many more. Python also lets you work quickly and integrate systems more effectively.
When you’re ready to install Python, continue with the steps below:
There are many ways to install Python on Ubuntu. Below are two methods that show you how to manually install from source code and via third-party PPA repository.
Method 1: Manually Installing Python
Some users may want manually install the latest version of Python on Ubuntu by building from the source code.
To do that they will need to download the installer file and run the executable.
Before installing Python from its source code, you must first install some required packages that are needed to build Python from source.. To get these packages installed, run the commands below:
sudo apt update sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev
After installing the above packages, go and download the latest release’s source code from the Python download page using the following wget command.
cd /tmp wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz
At the time of writing this post, 3.9.0 is the latest Python version… If you find a later version on the site, you can download it instead…
After downloading the package, run the commands below extract the file and install.
tar -xf Python-3.9.0.tgz cd Python-3.9.0 ./configure --enable-optimizations
Next start the building process using the make command. Replace the #4 with the number of CPU cores on your system for faster build time.
My machine has 4 CPU cores, so I use the make command with -j 4 option.
make -j 4
sudo make altinstall
Do not use the standard make install as it will overwrite the default system with python3 binary.
After that, Python should be installed and ready to use.
To test if Python is installed and ready to use, run the commands below
python3.9 --version
You should see an output similar to the one below:
Python 3.9.0
That’s how you install Python from its source
Method 2: Installing Python via PPA
If you quickly want to get the latest version of Python installed on Ubuntu and get future updates automatically, then you can install it from the below third-party PPA repository.
To do that, run the commands below to add the PPA.
First install Ubuntu software properties package if it’s not already installed on your system.
sudo apt update sudo apt install software-properties-common
After that run the commands to add the PPA.
sudo add-apt-repository ppa:deadsnakes/ppa
Finally, run the commands below to install Python 3.9
sudo apt update sudo apt install python3.9
Conclusion:
This post showed you how to install Python 3.9 on Ubuntu 20.04 | 18.04. If you find any error above, please use the comment form below to report.
You may also like the post below: