Software & AppsOperating SystemLinux

How To Install Python 3.9.5 on Ubuntu 20.04

Ubuntu 20

In this comprehensive guide, we will walk through the process of installing Python 3.9.5 on Ubuntu 20.04. Python is a versatile and widely-used programming language, and keeping it up-to-date is essential for running newer applications.

Prerequisites

Before we start, ensure that you have a running Ubuntu 20.04 system and have sudo privileges.

Step 1: Update Your System

The first step is to update your system’s package list. This ensures that you have the latest updates and security patches. You can do this by running the following command:

sudo apt-get update && sudo apt-get upgrade

The sudo command allows you to run programs with the security privileges of another user (by default, as the superuser). apt-get update updates the list of available packages and their versions, but it does not install or upgrade any packages. apt-get upgrade actually installs newer versions of the packages you have.

Step 2: Add the Deadsnakes PPA

Ubuntu 20.04 comes with Python 3.8 installed by default. To install Python 3.9, we will use the deadsnakes PPA. A PPA, or Personal Package Archive, is a collection of software not included in Ubuntu by default. You can add the PPA repository by running the following command:

sudo add-apt-repository ppa:deadsnakes/ppa

Step 3: Install Python 3.9

After adding the PPA, update your package list again:

sudo apt-get update

Now, you can install Python 3.9 by running:

sudo apt-get install python3.9

The install command is used to install a package, and python3.9 is the package we want to install.

Step 4: Verify the Installation

After the installation is complete, you can verify the installed version by running:

python3.9 -V

This should display the version as Python 3.9.5.

Important Note

Changing the default version of python3 to python3.9 is not recommended, as it may break your Ubuntu installation. Instead, you can manually call python3.9 when you want to use it.

Creating a Virtual Environment with Python 3.9

Creating an isolated environment allows you to work with Python 3.9 without affecting the system-wide Python installation. Here’s how to create a virtual environment with Python 3.9:

Step 1: Install the python3-venv Package

First, install the python3-venv package by running:

sudo apt-get install python3-venv

Step 2: Create a Directory for Your Virtual Environment

Next, create a directory for your virtual environment and navigate to it:

mkdir my_env && cd my_env

Step 3: Create a New Python 3.9 Virtual Environment

Now, create a new Python 3.9 virtual environment inside the directory:

python3.9 -m venv env

Step 4: Activate the Virtual Environment

Activate the virtual environment:

source env/bin/activate

Your shell prompt should now show (env).

Step 5: Check the Python Version

You can now use Python 3.9 within this virtual environment. To check the version, run:

python3 -V

It should display Python 3.9.5.

Step 6: Deactivate the Virtual Environment

When you’re done working with the virtual environment, you can deactivate it by running:

deactivate

This will return you to your normal shell.

Conclusion

In this guide, we walked through the process of installing Python 3.9.5 on Ubuntu 20.04 and creating a virtual environment. By following these steps, you can ensure that you are working with the latest version of Python and that your system remains stable and secure. Happy coding!

Can I install Python 3.9.5 on Ubuntu 20.04 without updating my system?

No, it is recommended to update your system before installing Python 3.9.5 to ensure that you have the latest updates and security patches. You can update your system by running sudo apt-get update && sudo apt-get upgrade command.

What is a PPA?

PPA stands for Personal Package Archive. It is a collection of software not included in Ubuntu by default. The deadsnakes PPA is used to install Python 3.9 on Ubuntu 20.04.

Can I change the default version of `python3` to `python3.9`?

It is not recommended to change the default version of python3 to python3.9 as it may break your Ubuntu installation. Instead, you can manually call python3.9 when you want to use it.

How can I create a virtual environment with Python 3.9?

To create a virtual environment with Python 3.9, you need to install the python3-venv package using sudo apt-get install python3-venv command. Then, create a directory for your virtual environment, navigate to it, and create a new Python 3.9 virtual environment using python3.9 -m venv env command. Finally, activate the virtual environment using source env/bin/activate command.

How do I deactivate a virtual environment?

To deactivate a virtual environment, simply run the deactivate command. This will return you to your normal shell.

Leave a Comment

Your email address will not be published. Required fields are marked *