Introduction

How to install HASS Core on a Raspberry PI 4 using Pyenv and Virtualenv

Create User

useradd -m -s /bin/bash homeassistant

Setup Pyenv

Preparation

yay -S pyenv pyenv-virtualenv
sudo -u homeassistant -i

Adjust your ~/.bashrc to enable Pyenv:

#
# ~/.bashrc
#

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

alias ls='ls --color=auto'
PS1='[\u@\h \W]\$ '

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"

Reload your Shell to reload ~/.bashrc:

. ~/.bashrc

Build Python

Install several packages to enable binary modules:

yay -S libpng libjpeg bzip2 expat gdbm libffi libnsl libxcrypt openssl zlib

Build Python 3.10.6

pyenv install 3.10.6 -vvvvv

If it was successful, you should this version when you run:

pyenv versions
* system
  3.10.6

Create Virtualenv named ha using Pyenv-Virtualenv

pyenv virtualenv 3.10.6 ha
pyenv versions

... should print this:

* system
  3.10.6
  3.10.6/envs/ha
  ha

Install HA

Create a root folder for HA:

mkdir ~/ha
cd ~/ha

Create a .python-version file to auto load the Virtualenv when you enter ~/ha

pyenv local ha

Database

Install Mariadb

yay -S mariadb mariadb-clients mariadb-libs
systemctl enable mariadb
systemctl start mariadb
mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

Add database for HA

  • Username: homeassistant
  • Password: homeassistant
  • DB name: homeassistant
mysql -uroot -p
CREATE DATABASE homeassistant DEFAULT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci';
GRANT ALL PRIVILEGES ON homeassistant.* TO 'homeassistant'@'localhost' IDENTIFIED BY 'homeassistant';
FLUSH PRIVILEGES;

We need to install a Python MySQL client inside the Virtualenv

pip install mysqlclient

For later, use the following config section for your configuration.yaml:

recorder:
  db_url: mysql://homeassistant:homeassistant@127.0.0.1:3306/homeassistant?charset=utf8

Install Home Assistant

pip install --upgrade homeassistant

Create a systemd unit to start HA:

Create /etc/systemd/system/ha.service with the following content:

[Unit]
Description=Home Assistant
After=network-online.target

[Service]
Type=simple
User=root
WorkingDirectory=/home/homeassistant/ha
ExecStart=/home/homeassistant/.pyenv/versions/ha/bin/hass -c=./homeassistant/

[Install]
WantedBy=multi-user.target
systemctl enable ha
systemctl start ha

Try!

Open http://$hostname:8123 to see if it's working

Backups

For backups read this blog post.