Home Assistant and Python 3.12 upgrade
I use Armbian Bullseye on RockPro64. The step may be a bit different on another platforms.
I get inspiration here Upgrading python virtual environment and forum post.
Python
Let start with required packages under root account:
apt install pkg-config build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev libxml2-dev libxslt1-dev
And continue under root account to get, configure, make and install Python:
mkdir Python
wget https://www.python.org/ftp/python/3.12.2/Python-3.12.2.tgz
tar xvfJ Python-3.12.2.tar.xz
./configure --prefix=/usr/local --enable-optimizations --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make -j 4
make altinstall
Now, let's configure latest installed Python version to default in the system:
update-alternatives --install /usr/bin/python python /usr/local/bin/python3.12 4
update-alternatives --config python
update-alternatives --install /usr/bin/pip pip /usr/local/bin/pip3.12 3
update-alternatives --config pip
update-alternatives --install /usr/bin/pip3 pip3 /usr/local/bin/pip3.12 3
update-alternatives --config pip3
And last step for Python, upgrade pip:
pip install --user --upgrade pip
Home Assistant
Under root account stop Home Assistant application:
systemctl stop homeassistant
Switch to homeassistant account and save required packages:
sudo -u homeassistant -H -s
cd /opt/homeassistant
source bin/activate
pip3.10 freeze --local > requirements.txt
deactivate
Again under root account backup current HA version and prepare dir for new one:
cd /opt
mv homeassistant homeassistant.311
mkdir homeassistant
chown homeassistant:homeassistant homeassistant
And go back to homeassistant account and finally prepare and install homeassistant:
cd /opt/homeassistant
python3.12 -m venv .
source bin/activate
cp ../homeassistant.311/requirements.txt .
python -m pip install wheel
pip install --upgrade setuptools
pip install --upgrade homeassistant
Note: Some packages installation could fail due to old version pinned in requirements.txt. In my case the solution was to upgrade all failed packages with command pip install --upgrade lxml
and change the coresponding version in requirements.txt
Under root account start HA:
systemctl start homeassistant
Revert unsuccessful upgrade
Under root account
cd /opt
rm -rf homeassistant
mv homeassistant.311 homeassistant
update-alternatives --config python # select old version
update-alternatives --config pip # select old version
update-alternatives --config pip3 # select old version
systemctl start homeassistant@hass.service