# Installation of pyenv and virtualwrapper on Ubunto/Debian 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 [pyenv](https://github.com/pyenv/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. ### Installing dependencies 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 ``` ### Installing pyenv 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 $yourpath$ to the path where pyenv is installed on your machine. 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 ``` ### Install python with pyenv 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 ``` ## Virtualwrapper 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. ### Installation 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 ``` ### Useful commands 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 ``` #### References: * https://gist.github.com/SebastiaAgramunt/5185ccf8637e69f611bd1217a98289b2 * https://github.com/pyenv/pyenv#installation * https://github.com/pyenv/pyenv/wiki/Common-build-problems