How to Setup Python 3 Virtual Environment on Ubuntu 20.04

Python’s venv module is a virtual environment is a Python environment such that the Python interpreter, libraries, and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed on your operating system, for example, those that are installed on your Ubuntu operating system to avoid clashing and disturbing your production environments.

In the following tutorial, you will learn how to install Python 3 and PIP 3 on your Ubuntu 20.04 operating system, along with setting up a programming environment via the command line.


  • Recommended OS: Ubuntu 20.04 – optional (Ubuntu 21.04)
  • User account: A user account with sudo or root access.
  • Required Packages: Python 3 and PIP 3

Install Dependencies

For the tutorial and creating a Python 3 environment, you will need to install the following dependencies in your Ubuntu terminal (CTRL+ALT+T):

sudo apt install -y build-essential libssl-dev libffi-dev

Install Python 3.8 (Ubuntu Default) or Python 3.9

By default, Python 3.8 comes in Ubuntu 20.04 repository. To install python 3.9, run the following command:

sudo apt install python3.9

Examples of dependencies that will also be installed:

How to Setup Python 3 Environment (VENV) on Ubuntu 20.04

Type Y, then press enter key to proceed with the installation.

Confirm the installation with version and build command:

python3.9 --version

Example output:

Python 3.9.5

If you would like a newer version of Python 3.9, install the PPA ppa:deadsnakes/ppa:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update

Now, if you have existing Python 3, you can upgrade, or if installing fresh, just run the install command:

To install:

sudo apt install python3.9 -y

To upgrade existing:

sudo apt upgrade python3.9 -y

Once complete, check the new build compared to that of the Ubuntu default repository:

python3.9 --version

Example output:


Suppose you would like the latest Python 3.10; please see our guide on installing Python 3.10 on Ubuntu 20.04. Note, this should only be used for experienced developers needing the absolute latest as 3.10 at writing is still in BETA.

Optional – python3-dev

Python-dev is the package that contains the header files for the Python C API, which is used by lxml because it includes Python C extensions for high performance. python-dev contains everything needed to compile python extension modules

Python 3.8 users:

sudo apt install python3-dev

Python 3.9 users:

sudo apt install python3.9-dev

Install PIP 3 on Ubuntu 20.04

When working with Python 3, you will need a way to manage software packages. A great tool to use is Pip that can install and manage packages. An in-depth guide on installing Pip on Ubuntu 20.04 can be found at How to Install PIP2 and PIP 3 on Ubuntu 20.04.

To install Pip for Python 3.8, which comes as default on Ubuntu 20.04, use the following command:

sudo apt install pip3

Example of dependencies that will be installed:

How to Setup Python 3 Environment (VENV) on Ubuntu 20.04

Type Y, then press enter key to proceed with the installation.

Verify Pip installation with the following command:

pip3 --version

Example output:

pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

To install PIP for Python 3.9 or higher, use the following curl command:

curl -sSL -o

Upon execution, will install pip, setuptools, and wheel in the current Python environment.

Verify the installation and Pip for Python 3.9 is correct:

python3.9 --version

Example output:

pip 21.2.4 from /home/joshua/.local/lib/python3.9/site-packages/pip (python 3.9)

Setup Python 3 Environment on Ubuntu 20.04

The first thing you will need to set up a python 3 environment is installing the package python3-env.

To do this, use the following command for Python 3.8:

sudo apt install python3-venv

For Python 3.9 users:

sudo apt install python3.9-venv

Next, make a new directory to host our python environment using the mkdir command:

sudo mkdir myproject
cd myproject

Note, the myproject directory name can be replaced with anything you like.

Inside the directory, run the following command to create the environment:

Python 3.8 users:

python3 -m venv myapplication

Python 3.9 users:

python3.9 -m venv myapplication

Once inside the environment you just created, the command setups the directory, which can verify the directory with the ls command:

ls myapplication

Example output:

joshua@ubuntu:~/myproject$ ls myapplication/
bin  include  lib  lib64  pyvenv.cfg

To activate the environment or, in easy terms, login to the environment, use the following command:

source myapplication/bin/activate

After you activate your environment, you will notice that your command console will be prefixed with the environment name, which is named (myapplication) as per our tutorial example.

An example of this is below:

(myapplication) joshua@ubuntu:~/myproject$ 

Create Test Program (Hello World)

To test your virtual environment, a quick way is to create the famous Hello World example program. Doing this will make sure everything is working and get you familiar with working in a python environment.

In your environment, create and open your future python script:

sudo nano

In the file, copy and paste the following code:

print("Hello, World!")

Save the file (CTRL+O), then exit (CTRL+X).

Now run the python test file by using the following command:

Python 3.8 users:


Python 3.9 users:


If everything worked correctly, you should get the following output:

Hello, World!

When you are finished in the my_env environment, to exit type:


Comments and Conclusion

In the tutorial, you have learned how to install a virtual environment with Python 3. Overall, Python virtual environments help decouple and isolate Python and associated pip packages, allowing end-users to install and manage their own set of packages independent of those provided by the system. This is very handy when developing a keeping system files separately.


Like to get automatic updates? Follow us on one of our social media accounts!