Install new Python version, then use it in venv
First download and install the Python version you want, then create a virtual environment that uses this newly installed Python version.
The recommended way by python.org
The recommended way of managing virtual environments since Python 3.5 is with the venv
module within the Python Standard Library itself. The method 1 in your case.
Source: https://docs.python.org/3/library/venv.html#creating-virtual-environments
That is not the same as virtualenv
, which is a third party package, outside the Python Standard Library.
Source: https://pypi.org/project/virtualenv/
Install another version of Python
For example, in Ubuntu 20.04, to install Python 3.7:
# Update package lists
me@mydevice:~$ sudo apt update
# Add the deadsnakes repository
me@mydevice:~$ sudo add-apt-repository ppa:deadsnakes/ppa
# Install Python 3.7
me@mydevice:~$ sudo apt install python3.7
Install the venv package and create a venv virtual environment
# Install the venv package for Python 3.7
me@mydevice:~$ sudo apt install python3.7-venv
# Make a folder for venv virtual environments
me@mydevice:~$ mkdir ~/.venvs
# Create a new venv virtual environment with Python 3.7 in it
me@mydevice:~$ python3.7 -m venv ~/.venvs/my-venv-name
# Activate the new venv
me@mydevice:~$ source ~/.venvs/my-venv-name/bin/activate
(my-venv-name) me@mydevice:~$
Check Python version within the venv virtual environment
# Check the Python version inside the venv:
(my-venv-name) me@mydevice:~$ python -V
Python 3.7.12
Deactivate the virtual environment
(my-venv-name) me@mydevice:~$ deactivate
me@mydevice:~$
Check Python version outside any virtual environments
# Check Python version:
me@mydevice:~$ python -V
Python 3.8.10
Do not set the new Python as system default
The Linux system needs its original Python version to be default for its own functionality. The system Python, in the case of Ubuntu 20.04 - Python 3.8, should be left as default, otherwise the system might become instable.
More info here: https://unix.stackexchange.com/questions/652299/changing-pythons-default-version-breaks-ubuntu-20-04
Install more Python versions
To install more Python versions, just change the version number from 3.7 to which ever version you choose, that is available from the deadsnakes repository.