markosamuli.pyenv

markosamuli.pyenv

Ansible Quality Score Ansible Role GitHub release License

Branch Pipeline
master master

This is an Ansible role that helps install pyenv and pyenv-virtualenv on Ubuntu or macOS development machines.

You can also choose to install pyenv-virtualenvwrapper to manage different environments.

Note: Do not use this role on production servers as it is designed to install pyenv only in the user’s home directory.

Install from Homebrew on macOS

To install pyenv and its plugins on macOS, the recommended way is using Homebrew.

The role does not migrate existing Homebrew installations to Git-based installations. It will detect any existing installations and continue using the previous method.

If you want to switch to this role, make sure to back up your existing ~/.pyenv directory and delete it before running the role.

Install from Homebrew on Linux

This role contains experimental support for installing pyenv and its plugins using Homebrew on Linux.

It does not install Homebrew itself; it assumes that Homebrew is already installed in the standard /home/linuxbrew/.linuxbrew location.

There can be issues when installing Python versions with pyenv on Linux using Homebrew.

  • There are known issues with the readline extension not being compiled with pip installs via Linuxbrew on Ubuntu 16 #1479

Installed Python versions

This role installs the Python versions specified in the pyenv_python_versions variable.

To set the global version, use the pyenv_global variable.

pyenv_global: "{{ pyenv_python37_version }} system"

The default configuration installs the latest Python 2 and Python 3 versions along with the system version.

Changes to shell config files

This role creates a configuration file in ~/.pyenv/.pyenvrc, which is loaded in .bashrc and .zshrc files.

Code completion is enabled by default.

If you manage your shell scripts (like .dotfiles) or use a specific framework, set pyenv_init_shell to false and update these files manually for cleanliness.

Example .bashrc configuration:

if [ -e "$HOME/.pyenv/.pyenvrc" ]; then
  source $HOME/.pyenv/.pyenvrc
  if [ -e "$HOME/.pyenv/completions/pyenv.bash" ]; then
    source $HOME/.pyenv/completions/pyenv.bash
  elif [ -e "/usr/local/opt/pyenv/completions/pyenv.bash" ]; then
    source /usr/local/opt/pyenv/completions/pyenv.bash
  fi
fi

Example .zshrc configuration:

if [ -e "$HOME/.pyenv/.pyenvrc" ]; then
  source $HOME/.pyenv/.pyenvrc
  if [ -e "$HOME/.pyenv/completions/pyenv.zsh" ]; then
    source $HOME/.pyenv/completions/pyenv.zsh
  elif [ -e "/usr/local/opt/pyenv/completions/pyenv.zsh" ]; then
    source /usr/local/opt/pyenv/completions/pyenv.zsh
  fi
fi

Role Variables

The path to the ~/.pyenv directory is determined using environment variables:

pyenv_home: "{{ ansible_env.HOME }}"
pyenv_root: "{{ ansible_env.HOME }}/.pyenv"

To update .bashrc and .zshrc files in the user's home directory:

pyenv_init_shell: true

Versions to install:

pyenv_version: "v1.2.13"
pyenv_virtualenv_version: "v1.1.5"
pyenv_virtualenvwrapper_version: "v20140609"

Latest versions of Python 3.7 and 3.8:

pyenv_python37_version: "3.7.6"
pyenv_python38_version: "3.8.1"

By default, Python 2 and 3 versions are installed:

pyenv_python_versions:
  - "{{ pyenv_python37_version }}"
  - "{{ pyenv_python38_version }}"

Set the global version to Python 3.7 with a fallback to the system version:

pyenv_global: "{{ pyenv_python37_version }} system"

To install the virtualenvwrapper plugin:

pyenv_virtualenvwrapper: false
pyenv_virtualenvwrapper_home: "{{ ansible_env.HOME }}/.virtualenvs"

To install using Homebrew on macOS:

pyenv_install_from_package_manager: true

To detect existing installation and use that:

pyenv_detect_existing_install: true

To install using Homebrew on Linux:

pyenv_homebrew_on_linux: true

Example Playbook

- hosts: localhost
  connection: local
  become: false
  roles:
    - role: markosamuli.pyenv

Updating versions

You can run the following scripts to fetch the latest releases from GitHub and update them in the role defaults.

Update pyenv release:

./update-release pyenv

Update pyenv-virtualenv release:

./update-release pyenv-virtualenv

Update default Python 3.7 version:

./update-python python37

Update default Python 3.8 version:

./update-python python38

To update all versions:

make update

Coding style

To install pre-commit hooks and check the coding style:

make lint

Run tests

To run tests on Ubuntu and Debian using Docker:

make test

Acknowledgements

The use of the .pyenvrc file and parts of the installation process for Python versions are based on avanov.pyenv.

Development

To install development dependencies in a local virtual environment:

make setup

To install pre-commit hooks:

make install-git-hooks

Changes

Check CHANGELOG.md for changes.

License

This project is licensed under the MIT license.

Author Information

You can find the author on GitHub: @markosamuli.

Informazioni sul progetto

Installs pyenv and pyenv-virtualenv

Installa
ansible-galaxy install markosamuli.pyenv
Licenza
mit
Download
15.8k
Proprietario
Random hacker. Engineering at @echo-health