Although Ubunto/Debian machines come already set up with Python 2.7.12, we might need different versions of python for experimentation. To do that we will use pyenv and virtualwrapper.
pyenv is a software that allows to switch between multiple versions of python. It is advantageous if you want to have a per-project version of Python, allows the creation and management of virtual-environments.
To install the dependencies for pyenv run the following code:
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
Install pyenv:
curl https://pyenv.run | bash
As recommended by the installation output on your terminal, add to the ~/.bashrc (or ~/.zshrc or /etc/bash.bashrc) the following lines to load pyenv automatically:
export PATH="/$yourpath$/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Change
If ~/.bashrc does not exist, create it, add the above lines and and load it into your shell using:
source ~/.bashrc
Restart your shell to load the new paths:
exec $SHELL
In pyenv the list of available python versions from 3.7 to 3.9 can be retrieved as follows:
pyenv install -l | grep " 3\.[789]"
Installing python 3.7.6:
pyenv install 3.7.6
In pyenv we distinguish between local and global interpreters.
A local interpreter will be available only on the folder where it is required and can be specified as follows:
pyenv local 3.7.6
While the global interpreter will be available through the entire system:
pyenv global 3.7.6
python -V
Virtual wrappers allows us to create multiple virtual environments with different package versions installations for the same (or different)version of python. Therefore it enables us to run multiple projects with different specifications on the same machine.
Substitute the PYENV_ROOT in the following code with the path where you installed pyenv (check your .bashrc file):
git clone https://github.com/pyenv/pyenv-virtualenv.git $PYENV_ROOT/plugins/pyenv-virtualenv
Add it to the .bashrc:
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
Some useful commands:
# create a virtualenv
mkvirtualenv my_environment
# deactivate virtualenv
deactivate
# list environments
workon
# work on virtualenv
workon my_environment
# delete virtualenv
rmvirtualenv my_environment