Skip to content

Instantly share code, notes, and snippets.

@ranabhat
Created November 6, 2020 10:26
Show Gist options
  • Save ranabhat/d0dde0f0db55234100bf028938bcd222 to your computer and use it in GitHub Desktop.
Save ranabhat/d0dde0f0db55234100bf028938bcd222 to your computer and use it in GitHub Desktop.
Quick Guide to install Pyenv **tool for managing multiple Python versions**

Pyenv Installation

Ubuntu Installation

  1. Install build dependencies 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
  2. Install pyenv curl https://pyenv.run | bash
  3. Add following lines to ~/.bashrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="/home/pi/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
  1. If you are combining pyenv and pipenv, then set the environment variable PIPENV_PYTHON to $PYENV_ROOT/shims/python. This will make Pipenv use pyenv’s active Python version to create virtual environments by default.
  2. Add PIPENV_PYTHON="$PYENV_ROOT/shims/python" to ~/.bashrc. And reload your shell exec "$SHELL"
  3. Check available python versions pyenv install --list | grep " 3\.[678]". To see all pyenv install --list
  4. Install the version you need, for instance pyenv install -v 3.7.2
  5. To see where the python versions are installed enter ls ~/.pyenv/versions/
  6. Remove, if you need, by entering rm -rf ~/.pyenv/versions/3.7.2 or pyenv uninstall 3.7.2
  7. Check what versions of Python you have available, pyenv versions (* indicates the system Python version)
* system (set by /home/pi/.pyenv/version)
  3.7.2
  1. To confirm use which python
  2. To use version 3.7.2, then you can use the global command pyenv global 3.7.2
  3. Check python version now pyenv version
 system 
  3.7.2 (set by /home/pi/.pyenv/version)
  1. To go back to the system version of Python enter pyenv global system and check version to confirm.

MacOS Installation

  1. Install build dependencies brew install openssl readline sqlite3 xz zlib
  2. Install pyenv curl https://pyenv.run | bash
  3. Add following lines to ~/.bashrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="/home/pi/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
  1. If you are combining pyenv and pipenv, then set the environment variable PIPENV_PYTHON to $PYENV_ROOT/shims/python. This will make Pipenv use pyenv’s active Python version to create virtual environments by default.
  2. Add PIPENV_PYTHON="$PYENV_ROOT/shims/python" to ~/.bashrc. And reload your shell exec "$SHELL"
  3. Check available python versions pyenv install --list | grep " 3\.[678]". To see all pyenv install --list
  4. Install the version you need, for instance pyenv install -v 3.7.2
  5. To see where the python versions are installed enter ls ~/.pyenv/versions/
  6. Remove, if you need, by entering rm -rf ~/.pyenv/versions/3.7.2 or pyenv uninstall 3.7.2
  7. Check what versions of Python you have available, pyenv versions (* indicates the system Python version)
* system (set by /home/pi/.pyenv/version)
  3.7.2
  1. To confirm use which python
  2. To use version 3.7.2, then you can use the global command pyenv global 3.7.2
  3. Check python version now pyenv version
 system 
  3.7.2 (set by /home/pi/.pyenv/version)
  1. To go back to the system version of Python enter pyenv global system and check version to confirm.

Pro Tip To check the version of Python installed is working properly is to run the built-in test suite.

$ pyenv global 3.8-dev
$ python -m test
----->will verify your installation<---------

For further detail info go to Intro-to-pyenv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment