Create a virtual environment with an upgraded python version

·

1 min read

Here is a short guide on how to create a virtual environment with an upgraded python version than what is installed on the system.

We are going to use Ubuntu 20.04 LTS. Ubuntu 20.04 by default comes with Python3.8. We are going to create the virtual environment using Python3.9. These steps should be similar for any other Linux / Python versions as well.

# check the system's python version.
python3 --version
# outputs 
Python 3.8.10
# update the package repository list
sudo apt update
# install the prerequisite for adding a private package repository 
sudo apt install software-properties-common -y
# add the deadsneak private package repository.
sudo add-apt-repositry ppa:deadsnakes/ppa
# install python3.9 and python3.9-venv
sudo apt install python3.9 python3.9-venv
# change to the project directory
cd /home/project
# use python3.9 to create a virtual environment (Example name .venv)
python3.9 -m venv .venv
# activate the virtual environment
source .venv/bin/activate
# check the virtual environment's python version.
python3 --version
# outputs
Python 3.9.10