Program pyenv together with virtualenvwrapper is possibly the coolest, most practical and easiest way of installing and dealing with different versions of python and its configurations on the same machine.

Installation of pyenv

Note Here we assume here that you are using Ubuntu with bash. Therefore default shell configuration file is ~/.bashrc. On the other hand, if you are using zshell then you can use ~/.zshrc.

Mac Users If you are using Mac you need to check which shell you are using. You can do this by running

echo $0

If you see bash then your configuration file is most likely to be ~/.bash_profile. On the other hand, if it’s zsh you can use ~/.zshrc.

Ubuntu

sudo apt install make build-essential zlib1g-dev libffi-dev libssl-dev \
    libbz2-dev libreadline-dev libsqlite3-dev

CentOS

yum install -y  gcc gcc-c++ make git patch openssl-devel zlib-devel readline-devel\
    sqlite-devel bzip2-devel libffi-devel

Mac only

brew install zlib

Mac/Ubuntu

git clone https://github.com/pyenv/pyenv.git ~/.pyenv

Then in ~/.bashrc add the following lines:

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

Pyenv usage

List available python versions:

pyenv install -l

Install python version

pyenv install 3.7.0

Mac

If you get into problems, on mac see: https://github.com/pyenv/pyenv/wiki/Common-build-problems

You may also try

brew install 'openssl@1.1'
SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk\
    MACOSX_DEPLOYMENT_TARGET=10.15 pyenv install 3.7.4

Setting up global python

After installing at least one version of python we can set it as global by

pyenv global 3.7.0

and also can be added as default in .bashrc:

pyenv global 3.7.0

List installed python’s versions

pyenv versions

Installation of pyenv-virtualenvwrapper

https://github.com/pyenv/pyenv-virtualenvwrapper

Fist, we need to make sure we have set up a global python (see above).

git clone https://github.com/pyenv/pyenv-virtualenvwrapper.git\
    $(pyenv root)/plugins/pyenv-virtualenvwrapper

Now you should be able to execute:

pyenv virtualenvwrapper

This will make sure that packages like virtualenv are installed on your system.

In order to have access to commands of virtualenvwrapper every time we run sell, we could add this lines to .bashrc:

pyenv virtualenvwrapper

(make sure that you have pyenv global 3.7.0 there already. )

Update of pyenv-virtualenvwrapper

cd $(pyenv root)/plugins/pyenv-virtualenvwrapper
git pull

Working with virtualenvwrapper

Now using pyenv we can choose previously installed version of python.

pyenv shell 3.7.0

Then we can create virtualenv by simply calling:

mkvirtualenv py3.7tf

This will create a new python’s installation in .virtualenvs directory.

In order to switch to this version you can call

workon py3.7tf

Now you can install packages with pip, for example:

pip install tensorflow

This package will be install only for py3.7tf environment. You can list installed packages calling

lssitepackages

Deactivating

you can deactivate virtualenv by simply calling

deactivate

Updated: 2019-10-18