Home Assistant System information
Version core-2023.10.1
Installation Type Home Assistant Core
Development false
Supervisor false
Docker false
User homeassistant20230903
Virtual Environment true
Python Version 3.11.5
Operating System Family Linux
Operating System Version 6.1.53-v7+
CPU Architecture armv7l
Timezone Asia/Shanghai
http://192.168.2.50:58123/config/repairs
Here is how to upgrade a Home Assistant instance to python 3.11.5 using python virtual environments. I encourage to take a full backup of your Home Assistant, configuration and database before starting. Anyway I have tried to do the upgrade in the least possible intrusive way, so we can rollback to our last state if something goes wrong.
With the announce of the 2023.8 version we have seen that python 3.10 will be deprecated, Home Assistant Core 2023.8 there is now a hard requirements for Python 3.11. (https://www.matterxiaomi.com/home-assistant-version)This procedure is for those that use Home Assistant core installed in a virtual environment and do not have python >= 3.9 provided by their distribution, Raspberry PI OS and Debian 11 Bullseye included. Those distributions provide python 3.9 at a maxim. This procedure has been tested with both. You could upgrade to python 3.11.5 as well only changing the sources to download.
Python 3.11.5 install
The idea is install our new python version using altinstall, so we maintain our main python version to not broke any dependencies on our system. All commands are meant to be executed as a regular user except those executed with sudo.
First we will install some required packages. For Debian use this command:
apt-get install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev libmysqlclient-dev libjpeg-dev libtiff5 libopenjp2-7
If you are on Raspberry PI OS, use this one:
apt-get install -y build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev libjpeg-dev default-libmysqlclient-dev libtiff5 libopenjp2-7
Then, download, compile and install python 3.11.5 from source.
#Download Python 3.11.5 source
sudo wget https://www.python.org/ftp/python/3.11.3/Python-3.11.5.tgz
#Unpack it
sudo tar -xzf Python-3.11.5.tgz
#Make & Install it
cd Python-3.11.5
./configure --enable-loadable-sqlite-extensions
make -j 4
sudo make install
New virtual environment
Next we will create a new Home Assistant virtual environment with the upgraded python 3.11.5 version we have just compiled from source. Then we will install home assistant and check its logs while it is booting, searching for errors. This assumes a standard HA installation with the virtual environment located in ‘/srv/homeassistant’ and configuration in the home of the homeassistant user, adapt this to your settings. Commented commands I used are below.
# stop home assistant and related services
sudo service hass stop
# cd into /srv directory
cd /srv/
# move your old virtualenv folder and create an empty new one
sudo mv homeassistant homeassistant20230903
sudo mkdir homeassistant
# assign necessary permissions
sudo chown homeassistant:homeassistant homeassistant
# become homeassistant user and cd into your new created directory
sudo -u homeassistant -H -s
cd /srv/homeassistant
# create your new virtual environmet in the current folder
# with the new python version you compiled before
python3.11 -m venv .
# activate the new viltual environment
source bin/activate
# install necesary python module and homeassistant
python3 -m pip install wheel
pip3 install homeassistant
# exit the virtual environment and
# start home assistant and check logs for errors
deactivate
sudo service hass start
Rollback
In case things go very bad and your Home Assistant become unstable or broken, you should go back to your old virtual environment. Find the commands below to rollback to your old setup.
# stop home assistant and related services
sudo service hass stop
# cd into /srv directory
cd /srv/
# move the failed venv and restore the old one
sudo mv homeassistant homeassistant.bad
sudo mv homeassistant20230903 homeassistant
# start home assistant
sudo service hass start
Conclusion
We have seen how to upgrade our Home Assistant virtual environment to python 3.11.5. Even though this is not a very difficult procedure, depending on your system and your Home Assistant installation you can face from none to several problems. I encourage you to do it and tell me how it was. I hope this post has been useful to you.