Choose a different version or distribution
Before we begin talking about how to install Python 3.9 on Ubuntu 20.04, let's briefly understand – What is Python 3.9?
Python 3.9 is the latest version of the popular programming language Python. Released in October 2020, it brings several new features and improvements. Python 3.9 enhances developer productivity and code readability with its optimized syntax and upgraded tools. Key highlights include improved dictionary merging, precise types for function annotations, and enhanced error messages.
With its focus on simplicity and efficiency, Python 3.9 offers developers a powerful and user-friendly environment for building a wide range of applications. Stay up-to-date with Python 3.9 to leverage its advancements and make your coding experience smoother and more enjoyable.
In this tutorial, you will install Python 3.9 on Ubuntu 20.04. We will also address a few FAQs on how to install Python 3.9 on Ubuntu 20.04.
Advantages of Python 3.9
- Enhanced Developer Productivity: Python 3.9 offers optimized syntax and upgraded tools, making coding faster and more efficient.
- Improved Type Hinting: Precise types for function annotations enhance code reliability and readability.
- Better Dictionary Merging: Python 3.9 simplifies the merging of dictionaries, saving time and effort.
- Enhanced Error Messages: Clearer error messages aid in debugging and troubleshooting, improving code quality.
- Wide Range of Applications: Python 3.9 is versatile, allowing developers to build diverse applications with ease.
Installing Python 3.9 on Ubuntu with Apt
Python 3.9 installation on Ubuntu with apt is a simple process that takes only a few minutes to complete.
1) Install the prerequisites and update the package list:
sudo apt update
sudo apt install software-properties-common
2) Add the deadsnakes PPA to the sources list of your system:
sudo add-apt-repository ppa:deadsnakes/ppa
3) To proceed, press Enter
when prompted. Once the repository is enabled, run the following command to install Python 3.9:
sudo apt install python3.9
4) Verify that the installation was successful by typing:
python3.9 --version
Output
Python 3.9.1+
That’s it. Python 3.9 is installed on your Ubuntu system, and you can start using it.
Installing Python 3.9 on Ubuntu from Source
When you compile Python from source, you can install the most recent Python version and customize the build options. You will, however, be unable to maintain your Python installation using the apt
package manager.
The steps below describe how to compile Python 3.9 from source:
1) Install the prerequisites required to build Python:
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
2) Use wget to download the source code for the most recent release from the Python download page:
wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tgz
3) Once the download is complete, extract the gzipped archive:
tar -xf Python-3.9.1.tgz
4) Switch to the Python source directory and run the configure
script, which performs a number of checks to ensure that all of your system's dependencies are present:
cd Python-3.9.1
./configure --enable-optimizations
The --enable-optimizations
flag allows the Python binary to be optimized by running multiple tests. This slows down the building process.
5) Begin the Python 3.9 build process by typing:
make -j 12
To reduce build time, change the -j
to the number of cores in your processor. By typing nproc
, you can find the number.
6) When the build is finished, run the Python binaries by typing:
sudo make altinstall
Because later it will overwrite the default system python3 binary, we're using altinstall
instead of install
.
That's all. Python 3.9 is now installed and ready for use. To confirm, type:
python3.9 --version
The output should show the Python version:
Output
Python 3.9.1
FAQs to Install Python 3.9 on Ubuntu 20.04
How do I install Python 3.9 using the deadsnakes
PPA?
After adding the PPA, run the command: sudo apt install python3.9
.
Can I have both Python 3.8 and Python 3.9 installed on Ubuntu 20.04?
Yes, Ubuntu allows you to have multiple Python versions installed. Use the specific version's command, such as python3.8
or python3.9
, to run the desired interpreter.
How do I set Python 3.9 as the default version on Ubuntu 20.04?
You can set Python 3.9 as the default version by using the update-alternatives
command. Run: sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1
Can I still use pip
with Python 3.9 on Ubuntu 20.04?
Yes, pip
comes pre-installed with Python 3.9. You can use pip3
command to install Python packages.
How do I uninstall Python 3.9 from Ubuntu 20.04?
To uninstall Python 3.9, run: sudo apt remove python3.9
Conclusion
Python 3.9 is not included in the default Ubuntu 20.04 repositories. We demonstrated how to install Python 3.9 on your Ubuntu 20.04 machine. You can now begin working on your Python 3.9 project.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.