Skip to content

Instantly share code, notes, and snippets.

@michaelkarrer81
Last active April 28, 2024 06:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save michaelkarrer81/1d52e7e467844fe55f35391ac3477931 to your computer and use it in GitHub Desktop.
Save michaelkarrer81/1d52e7e467844fe55f35391ac3477931 to your computer and use it in GitHub Desktop.
[Install python versions] Install multiple python versions in linux with pyenv #pyenv #python #linux

Important links

Install pyenv for the user root

sudo su
curl https://pyenv.run | bash

# Load pyenv automatically by adding
# the following to ~/.bashrc:
export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Install build tools to be able to build python

sudo apt 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 git manpages-dev

# In some cases you may need to install libreadline-dev again
sudo apt install libreadline-dev

# When building python 2
sudo apt-get install python-tk
sudo apt install python3-pkgconfig

Install a python version for the current user

# Install the needed python version first for pyenv (will build python)
pyenv install -v 2.7.18

# Create a new virtual environment with pyenv
pyenv virtualenv 2.7.18 myvenv
pyenv activate myvenv

# Attention: sometimes this will not work! check with which python2 and which pip
#            if it dies not work you need to manualy activate the venv:
source /root/.pyenv/versions/2.7.18/envs/myenv/bin/activate

# Deactivate the virtualenv again
pyenv deavtivate

# This will create a new python env at
# /root/.pyenv/versions/2.7.18/envs/

# HINT: pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.

Install a python version for all user with pyenv tool python-build

/root/.pyenv/plugins/python-build/bin/python-build 2.7.10 /usr/local/python-2.7.10/

# Create a virtual env without pyenv
virtualenv -p /usr/local/python-2.7.10/bin/python /opt/online/venv_python-2.7.10

# Activate the venv
source /opt/online/venv_python-2.7.10/bin/activate

# Upgrade pip and setuptools in the venv
pip install pip --upgrade
pip install setuptools --upgrade

# Install python libs by requirements.txt
pip install -r /root/requirements.txt

# Deactivate the venv
deactivate

Python venv tipps for odoo 8 and python 2.7

# missing headers
sudo apt install libgeoip-dev libxml2-dev libxslt1-dev libsasl2-dev python-dev libldap2-dev libssl-dev libxmlsec1-dev

# Downgrade pip
pip install pip==19.0.3

# Solve marker.h missing
easy_install distribute
pip install --upgrade distribute

# Install without cache dir
pip install --no-cache-dir -r odoo_pip_requirements_python2.7.16_mac.txt

Handy pyenv and related commands

which python
python -V
pip -V
pyenv which python
pyenv versions

# Install
pyenv install --list
pyenv install 2.7.10
ls ~/.pyenv/versions/

# Uninstall
pyenv uninstall 2.7.15 	# ist the same as: rm -rf ~/.pyenv/versions/2.7.15

# Activate for current user
pyenv local 2.7.15
pyenv shell 2.7.15

# Go back to the system python
pyenv local system
pyenv shell system
@agatho-daemon
Copy link

Are you using this setup in production? On daily bases?
The restrictions on pip package installation after 3.12 seems to push users towards your approach without the option of breaking system packages.
It would be a nice and clean way to use Pyenv to install packages that require root privileges.

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