Skip to content

Instantly share code, notes, and snippets.

@chewwt
Last active October 2, 2022 05:25
Show Gist options
  • Save chewwt/ce7ee4ece7b02acabed83f9a776ec62d to your computer and use it in GitHub Desktop.
Save chewwt/ce7ee4ece7b02acabed83f9a776ec62d to your computer and use it in GitHub Desktop.
python3 venv setup

Setting up virtual environments with different python versions (Latest)

Tested with Ubuntu 22.04

0. If there is no python2 on system

Link command python to python3, or pyenv will not be able to detect system version.

sudo ln -s /usr/bin/python3 /usr/bin/python

1. Install pyenv

pyenv manages the different python versions

# dependencies (https://github.com/pyenv/pyenv/wiki#suggested-build-environment)
sudo apt-get update; sudo apt-get install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

# pyenv installer
curl https://pyenv.run | bash  
pyenv update

Put at the bottom of ~/.zshrc

# pyenv (must put at the end)
export PATH="/home/ruth/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"

2. Install desired python version

pyenv install 3.8.14

If for graph-tools or third party tools that require support for shared libraries:

env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.8.14

3. Install some global python libs

On system python

sudo apt install python3-pip python3-venv

4. Switch to desired python version

You can check available python versions with pyenv versions.

pyenv global 3.8.14

5. Make virtual environemnt with venv

python -m venv ~/.venvs/py_onto_36

6. Setup virtual environment wrapper

Make sure you are in system python

pyenv global system
pip3 install virtualenvwrapper

Add the following in ~/.zshrc or ~/.bashrc, where WORKON_HOME is the virtual environments home and VIRTUALENVWRAPPER_PYTHON is the system python

# venv
export WORKON_HOME=$HOME/.venvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/$(system python version)
source ~/.local/bin/virtualenvwrapper.sh

7. Update pyenv

Update to get the latest pythons

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