下载:https://www.python.org/downloads/
安装依赖包:apt install -y wget build-essential libreadline-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev
编译安装python:
https://www.jb51.net/article/152486.htm
注意先将上面的依赖包安装好,否则后面会遇到一些错误,例如
ModuleNotFoundError: No module named ‘_sqlite3’
ModuleNotFoundError: No module named ‘_ctypes’
$ python3 -V
Python 3.8.3
$ pip3 -V
pip 20.1.1 from /usr/local/python3/lib/python3.8/site-packages/pip (python 3.8)
https://bbs.hassbian.com/thread-27-1-1.html
#首先改一下sudoer设置,省得sudo时总要求输密码
sudo nano /etc/sudoers
#在最下面(看好,是此文件的最下方,否则会被下面的设置所覆盖而无效!)添加以下内容(jones改为你的用户名):
jones ALL=NOPASSWD: ALL
#可选项,如果在安装ubuntu时没有更改时区的,使用下面的代码更改时区
sudo dpkg-reconfigure tzdata
#更新源信息,安装更新
sudo apt-get update && sudo apt-get upgrade -y
#做一些清理工作
sudo apt-get autoclean
sudo apt-get clean
#直接安装python3-pip
sudo apt-get install python3-pip
#安装Python3虚拟环境
sudo apt-get install python3-venv
#添加一个名为homeassistant的用户
sudo useradd -rm homeassistant
#转到/srv目录,建立homeassistant文件夹
cd /srv
sudo mkdir homeassistant
#更改此文件夹的所有者和所属组
sudo chown homeassistant:homeassistant homeassistant
#更换用户
sudo su -s /bin/bash homeassistant
#切换目录,创建并进入虚拟环境
cd /srv/homeassistant
python3 -m venv homeassistant_venv
source /srv/homeassistant/homeassistant_venv/bin/activate
#虚拟环境下更新pip, setuptools(此步非常重要,否则安装HASS时会报错)
pip install --upgrade pip
#安装依赖netdisco和warrant(此步非常重要,否则安装完HASS会报discovery等错误)
pip install netdisco
pip install warrant
#正式安装HomeAssistant,速度会非常快
pip install homeassistant
#安装完毕,退出虚拟环境
exit
#设置开机启动,建立service文件,将#中间的部分拷入,按ctrl + x, y 退出。
sudo nano /etc/systemd/system/[email protected]
#########################################################################
[Unit]
Description=Home Assistant
After=network.target
[Service]
Type=simple
User=homeassistant
Environment=PATH=“ V I R T U A L E N V / b i n : VIRTUAL_ENV/bin: VIRTUALENV/bin:PATH”
ExecStart=/srv/homeassistant/homeassistant_venv/bin/hass -c “/home/homeassistant/.homeassistant”
[Install]
WantedBy=multi-user.target
#########################################################################
#更新系统设置
sudo systemctl daemon-reload
#设置HomeAssistant开机启动
sudo systemctl enable [email protected]
#启动HomeAssistant
sudo systemctl start [email protected]
#重新启动HomeAssistant
sudo systemctl restart [email protected]
#查看HomeAssistant状态
sudo systemctl status [email protected]
不使用service,直接执行hass的话
/srv/homeassistant/homeassistant_venv/bin/hass -v -c “/home/homeassistant/.homeassistant”
需要等待一段时间,看到log显示启动完成
2020-06-27 18:53:10 INFO (MainThread) [homeassistant.core] Timer:starting
$netstat -atnp| grep LISTEN
tcp 0 0 0.0.0.0:8123 0.0.0.0:* LISTEN 48703/python3