How To Install Python 3.9 on Ubuntu 20.04

install python 3.9 ubuntu 20.04

Python is a general-purpose, high-level, interpreted language with easy syntax and dynamic semantics. It was developed by Guido Van Rossum in 1989. It is used by Software Engineers, Data Analysts, Accountants, Mathematicians, Scientists and Kids because it is a very beginner-friendly and multi-purpose programming language. With Python, you can solve complex problems in less time with fewer lines of code.

Python offers a rich set of features including, Open-source, Simplicity, Portability, Embeddable, Extensible, Interpreted, Huge Libraries, Object-Oriented and many more. Currently, it is used by many big companies including, Google, Dropbox, Netflix, Nasa and BitTorrent.

At the time of writing this tutorial, the Python 3.9 has been released with several improvements and security updates. This release has many new features including, assignment expression, support for IANA time zone, new dict operators and many more.

In this tutorial, we will learn you how to install Python 3.9 on Ubuntu 20.04.

Prerequisites

  • A system running Ubuntu 20.04.
  • A root password is set up in your system.

Step 1 – Install Required Dependencies

Before starting, it is recommended to update the APT packages cache to the latest version. You can update them by running the following command:

1
apt-get update -y

Next, you will need to install some packages required to install Python 3.9. Run the following command to install all dependencies:

1
apt-get install software-properties-common apt-transport-https gnupg2 -y

Once all the dependencies are installed, you can proceed to install Python 3.9.

Step 2 – Install Python 3.9

By default, Python version 3.9 is not available in the Ubuntu standard repository. So you will need to add the dead snakes PPA to your system.

Run the following command to add the dead snakes PPA to your system:

1
add-apt-repository ppa:deadsnakes/ppa

Once the repository is added, update the repository cache and install the Python 3.9 with the following command:

1
2
apt-get update -y
apt-get install python3.9 -y

Once Python 3.9 has been installed, you can verify the installed version of Python using the command below:

1
python3.9 --version

You should see the Python version in the following output:

1
Python 3.9.1

Step 3 – Set the Default Python Version

If an older version of Python3.8 is already installed in your system then you will need to set your default Python version to Python 3.9.

First, check the default Python version using the following command:

1
python3 --version

You should see your default Python version in the following output:

1
Python 3.8.2

The above output indicates that you have multiple version of Python in your system.

You can also check all Python versions in your system using the following command:

1
ls /usr/bin/python*

You should see all Python versions in the following output:

1
/usr/bin/python3  /usr/bin/python3.8  /usr/bin/python3.9

Next, you will need to update your update-alternatives before setting the default Python version. You can update it with the following command:

1
2
update-alternatives --install /usr/bin/python3 python /usr/bin/python3.9 1
update-alternatives --install /usr/bin/python3 python /usr/bin/python3.8 2

Next, verify whether both versions are updated or not with the following command:

1
update-alternatives --list python

You should get the following output:

1
2
/usr/bin/python3.8
/usr/bin/python3.9

Next, set your default Python version to 3.9 using the following command:

1
update-alternatives --config python

You will be asked to set the default Python version as shown below:

1
2
3
4
5
6
7
  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.8   2         auto mode
  1            /usr/bin/python3.8   2         manual mode
  2            /usr/bin/python3.9   1         manual mode
 
Press  to keep the current choice[*], or type selection number: 2

Type 2 and press Enter to set the default Python version. You should get the following output:

1
update-alternatives: using /usr/bin/python3.9 to provide /usr/bin/python (python) in manual mode

Now, you can verify your default Python version using the following command:

1
python3 --version

You should get the following output:

1
Python 3.9.1

Step 4 – Install Pip and Virtualenv Tool

Pip is a package manager for Python used for installing and managing additional libraries and dependencies required for your development projects.

You can install the pip by running the following command:

1
apt-get install python3-pip -y

You can now use pip command to install your required Python library as shown below:

1
pip3 install package-name

Venv is a Python module used for creating lightweight “virtual environments”. It gives you an isolated space on your system for Python projects. It helps you to create a Python project with its own set of dependencies that won’t disrupt any of your other projects.

You can install the Venv Python module with the following command:

1
apt-get install python3.9-venv -y

Once you are done, you can proceed to set up a Python virtual environment.

Step 5 – Setting Up a Virtual Environment

Python virtual environment is a simple directory tree where you can create your Python project and install necessary dependencies for your project.

First, change the directory where you want to store your Python 3 virtual environments.

1
cd /opt

Next, run the following command to create a virtual environment named my-project:

1
python3 -m venv my-project

Next, activate your virtual environment with the following command:

1
source my-project/bin/activate

You should get the following output:

1
(my-project) root@ubuntu2004:/opt#

Now, you can install and upgrade your desired modules using the pip command.

Let’s install the Requests module using the pip command:

1
pip3 install requests

Next, verify the installation using the following command:

1
python3 -c "import requests"

If the installation was successful. You will not get any errors.

You can now deactivate from the Python virtual environment with the following command:

1
(my-project) root@ubuntu2004:/opt# deactivate

Conclusion

In the above post, you learned how to install Python 3.9 on Ubuntu 20.04 server. You also learned how to set the default Python version and create and use Python virtual environments. You can now start creating your first Python project.

You should also read the following article

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva