Skip to content

Installing a new Python version for OpenPrescribing

David Evans edited this page Sep 27, 2024 · 2 revisions

Until we Dockerise the deployment (or upgrade the base OS) we have to install more recent Python versions manually.

I used pyenv to do this without messing with the system installation.

Preparation

You shouldn't need to do these steps again but I'm recording for posterity.

Install a whole bunch of build dependencies (this list was copied from an old Dockerfile which used pyenv)

sudo apt-get install --no-install-recommends \
  make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev \
  libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev \
  libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Get a copy of pyenv:

sudo mkdir /usr/local/pyenv
cd /usr/local/pyenv
sudo chown hello .
git clone https://github.com/pyenv/pyenv .
git checkout v1.2.21
sudo chown -R root .

Build a new Python version

# Setting PYENV_ROOT stops pyenv from putting it in the home directory
cd /usr/local/pyenv
sudo chown -R hello .
PYENV_ROOT="$PWD" bin/pyenv install 3.8.6
sudo chown -R root .

Update the version used by the app

Make a new virtualenv and install all requirements

cd /webapps/openprescribing
/usr/local/pyenv/versions/3.8.6/bin/python -m venv .venv38
source .venv38/bin/activate
pip install wheel
pip install -r requirements.txt
sudo chown -R bennettbot .venv38

Stop the app, switch the symlink, and restart

sudo systemctl stop app.openprescribing.*
unlink .venv
ln -s .venv38 .venv
sudo chown -h bennettbot .venv
# Start the socket and let the socket start the service otherwise
# it seems to cause problems
sudo systemctl start app.openprescribing.web.socket

Repeat the above for webapps/openprescribing_staging