Using Pyenv and Virtualenv for managing multiple Python versions and environments

Firstly, install pyenv using these commands:

sudo apt install git
sudo apt install curl
curl <https://pyenv.run> | bash

Next, you need to configure pyenv in your Bash environment. Append the pyenv configuration to your ~/.bashrc file:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc

If you have ~/.profile, ~/.bash_profile, or ~/.bash_login files, also add the pyenv configuration there by following the instructions here. If not, add it to ~/.profile using the same commands as above.

Afterward, restart your terminal session or run source ~/.bashrc to apply the changes.

To create a new virtual environment, first, install the desired Python version. Then, use the pyenv virtualenv command with the Python version and the name of the virtual environment. Here's an example creating an environment named MidiEnv with Python 3.6.15:

pyenv install --list # list all possible versions of python that are available for installation
pyenv install 3.6.15 # select your desired version
pyenv virtualenv 3.6.15 MidiEnv # create the virtual env

Finally, activate the virtual environment using the pyenv activate command, like so:

pyenv activate MidiEnv 
pyenv deactivate # to exit the environment