pyenv is useful for managing Python versions, especially when you don’t have root access.

Go to a local directory, like $HOME, and in a bash shell do:

curl -L | bash
pyenv update

Follow the instructions at the end of the installer regarding updating your ~/.bashrc script. You may need to restart the shell (or source ~/.bashrc) before proceeding. To install a specific Python version, e.g. 2.7.11, do:

pyenv install 2.7.11

Sometimes it is useful to pass some Python configuration settings, for example to build with shared libraries and UCS4 encoding do:

PYTHON_CONFIGURE_OPTS="--enable-shared --enable-unicode=ucs4" pyenv install 2.7.11

To pass specific compiler and linker options (e.g. because a dependency is in a strange location), do:

PYTHON_CONFIGURE_OPTS="--enable-shared --enable-unicode=ucs4" LDFLAGS="-L$DIRECTORY_WITH_LIBRARY" CPPFLAGS="-I$DIRECTORY_WITH_HEADERS" pyenv install 2.7.11 --verbose

where --verbose is helpful to see if the dependency is found correctly. To set this version as the global one to be used by pyenv do:

pyenv global 2.7.11

With pyenv-virtualenv you can create a named virtual environment from the global version. Do:

pyenv virtualenv venv2711

It is useful to then add pyenv activate venv2711 to ~/.bashrc to activate this venv when opening a shell.

Note: pyenv can fail silently when building Python and then fall back to system Python. It is good to check the Python version being used after installing, e.g. by launching Python.


comments powered by Disqus