Upgrading HomeAssistant Core in a Python VE

Hi,

Installing or upgrading HomeAssistant to the latest & greatest in a Python Virtual Environment, may be tricky. So I thought I’d create a post after I’ve struggled through it enough times now.

Hopefully this helps other members.

In summary there is a couple of steps involved, but from a High-Level, this is what’s required:

  1. Ensure you have a supported Python version for the latest build. (Currently for HASS 2021.12.10, the earliest supported version of Python is 3.9.1)
  2. If required, Upgrade Python on your platform (this guide is for Debian)
  3. Backup your current Python packages
  4. Rebuild HomeAssistant to run in the new Python virtual environment
  5. Upgrade HomeAssistant after its running from the new Python environment
  6. Check for other breaking changes (For example the SQL Lite version).

So, in summary here is what I did to upgrade my HASS core deployment on Ubuntu 18.04 to the latest:

a) Log into the Shell.
b) Check Python Version:

python --version
python3 --version

c) If you have a Python version that is supported (3.9.1 at the time of writing this) or above then skip to step i) otherwise continue to upgrade Python as follow:

wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tgz
tar -xzf Python-3.9.1.tgz
cd Python-3.9.1
./configure
make -j 4
sudo make install

d) Freeze current Python packages:

cd /home/homeassistant/.homeassistant
sudo -u homeassistant -H -s
source /srv/homeassistant/bin/activate
pip3 freeze –local > requirements.txt

e) Backup your existing HASS folder (all in the name of Justin…Justin Case):

sudo systemctl stop [email protected]
sudo mv homeassistant homeassistantold
sudo mkdir homeassistant
sudo chown -R homeassistant:homeassistant homeassistant

f) Install HASS dependencies for the new Python Version:

cd /srv/homeassistant/lib
sudo -u homeassistant -H -s
cd /srv/homeassistant
sudo python3.9 -m venv .
source /srv/homeassistant/bin/activate
cd /home/homeassistant/.homeassistant
pip3 install -r requirements.txt

g) If you get an Error about not having access to some Python Libraries (e.g. ERROR: Could not install packages due to an Environment Error: [Errno 13] Permission denied: '/srv/homeassistant/lib/python3.9/site-packages/six.py’) Then do the following (which sets the correct permissions and re-runs the install):

cd /srv
sudo chown -R homeassistant:homeassistant homeassistant
cd /home/homeassistant/.homeassistant
pip3 install -r requirements.txt

h) Finalise / Activate the HASS Install on the new Python version:

pip3 install homeassistant
sudo systemctl start [email protected]

and monitor the HASS statup through:

sudo nano /home/homeassistant/.homeassistant/home-assistant.log

Recorder might start rebuilding DB’s at this point, and this may take some time, just monitor the progress and wait for HASS to become fully available before continuing to the next step.

i) Upgrade HASS to the Latest version:
At this point, HASS should be running on a supported version of Python, to upgrade HASS to the latest version do the following:
Start a New SSH session, and run:

source /srv/homeassistant/bin/activate
sudo systemctl stop [email protected]
pip3 install --upgrade homeassistant
sudo systemctl start [email protected]

Once all of this is done, HASS should be the latest version, Python should be a supported version, and all should be good.

Once you have reached this point, wait for any recorder/db checks & rebuilds, and wait until HomeAssistant becomes available again, you can monitor the progress under /home/homeassistant/.homeassistant/home-assistant.log:

sudo nano /home/homeassistant/.homeassistant/home-assistant.log

HOWEVER…

If you view the log and you see an Error that reads: "Version 3.22.0 of SQLite is not supported; minimum supported version is 3.31.0. " then an upgrade of SQL lite is required

To fix this, do the following:
j) Start a Fresh SSH session, & Install & configure SQL Lite by:

sudo systemctl stop [email protected]
wget https://sqlite.org/2021/sqlite-autoconf-3360000.tar.gz
tar -xvf sqlite-autoconf-3360000.tar.gz
./configure
make
sudo make install

Once the supported version of SQL lite is downloaded & installed, you will probably still have the error when trying to start Home Assistant, reason for this is because the SQL Lite install happens to the default location (/usr/local/lib/), and is not in a place where HomeAssistant expects it to be (/usr/lib/xxxx).

To correct this, you first have to determine your system type (For Example, a normal x86/x64 PC will be x86_64-linux-gnu, and a Raspberry PI it would be arm-linux-gnueabihf).
Once you know this you can copy the files from the default install to the folder HASS expects.

For a x86 or x64 Ubuntu system:

sudo cp /usr/local/lib/*sql* /usr/lib/x86_64-linux-gnu/

For a Raspberry Pi ARM system:

sudo cp /usr/local/lib/*sql* /usr/lib/arm-linux-gnueabihf/

Once copied across, we need to set the permissions on the above directories.
For a x86 or x64 Ubuntu system:

sudo chmod a+x /usr/lib/x86_64-linux-gnu/*sql*

For a Raspberry Pi ARM system:

sudo chmod a+x /usr/lib/arm-linux-gnueabihf/*sql*

Once this is done, you can Start the Hass Instance:

sudo systemctl start [email protected]

Once again, monitor the startup and you should be good to go, however recorded might take some time to rebuild the DB, depending on its size.

sudo nano /home/homeassistant/.homeassistant/home-assistant.log

Hopefully this helps!

1 Like