Use apt-get commands

Hi, I’m new to Home Assistant, so please bear with me. I have HassOS running on my Raspberry Pi, and I am trying to install some integrations that are not available from the web UI, for example the Apple TV, or Bluetooth integrations. They require me to run some initial apt-get commands to install some packages, but the commands don’t work because apt-get is not installed. Is there a way I could get apt-get to work on HassOS, or is there another way I could install the packages?

1 Like

You cannot install packages for Home Assistant OS and you shouldn’t need to install any packages to use any of the built-in integrations. Have you tried to use the integrations without installing additional packages?

Actually, I haven’t. I’ll try that

Please report back with your results. If the Docs are misleading they should be updated. If your willing to do the updates see the link below with how. If you are not, post the results and myself or someone else can make the updates.

In fact you can install packages in home assistant OS. The package manager uses the command apk add xxxx

First create bash script and make executable and put it in a shell command

shell_command: 
setup: /config/scripts/setup.sh
# setup. sh

#!/bin/sh
apk update
apk add -u busybox
apk add busybox-extras
apk add expect
apk add sshpass

Then create an automation that runs your shell command during startup

- id: hass_1
  initial_state: 'true'
  alias: System Startup Notification
  trigger:
    platform: homeassistant
    event: start
  action:
  - service: shell_command.setup
6 Likes

That is not the OS, that is inside the homeassistant container

1 Like

True that’s where home assistant is running as docker on top of home assistant OS.

In this way the packages that are not by default avaliable can be installed and used with home assistant.

Every use case is different… in my case telnet client and expect for scripting is not avaliable by default hence the above proposed solution.

arent you missing the bash instruction here?

like : setup: bash /config/scripts/setup.sh

you could add the bash command indd but I works without here as well

if you make your scripts executable chmod +x setup. sh should work as well
it depends on your system

Yeah, it works, but I also need to load modules with modprobe, that doesn’t work :frowning:

hmm, tried it today, with automation, but then it fails

created an requirements.sh with this as content and did a chmod +x on it :slight_smile:

#!/bin/bash
sudo apk add can-utils

if i run it from putty, then it works, it installs that package

bash /config/python_scripts/requirements.sh

if i run it from the automation

- alias: Dobiss Server
  initial_state: 'on'
  trigger:
    - platform: homeassistant
      event: start
  action:
    - service: shell_command.requirements

shell_command:
requirements: bash /config/python_scripts/requirements.sh

then i receve error below

2021-02-17 09:35:51 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: bash /config/python_scripts/requirements.sh, return code: 127

if i remove the sudo from the .sh script, i get error 1 instead of error 127

any idea?

it has todo with environment, i know the yaml code is correct, because if i try something simple in the bash script, it creates the abc.txt file, so everything is fired

it has something todo with rights i think of installing an file , with maybe the user that runs teh script not able to install apk files? with or without sudo gives me another error 1 or 127

#!/bin/bash
apk add can-utils
#echo abc > abc.txt

return code: 1 

############

#!/bin/bash
sudo apk add can-utils
#echo abc > abc.txt

return code: 127

chmod a+x doesnt help either

Hi, not sure what the problem is, but you can try executing the command with exec myScript.sh to test. Follow it with echo $? to get the exit status. Exec will close your terminal session so maybe wrap it $(exec myScript.sh)

The Shebang(#!) must be on the first line of the file with no whitespace between the start of the file and the Shebang.

Try a space between the Shebang and the /bin/bash. Most of the time it doesn’t matter because it’s launched from a shell and processed by that shell also exec is usually smart enough. In this case it may be that the exec command processor is looking for the area between the first space and the end of the line. I’ve seen this happen on a Linux system before. Not HA, but others.

BTW. HA default is #!/usr/bin/with-contenv bashio. Try this one.

Here’s a list of valid Shebangs I found on the terminal addon. Run On Startup.d

I have solved it, all command were actually fine, but I opened an session in docker itself, to see what went wrong, because it worked in an ssh session from ssh addon … Seems ssh add-on is running alpine 3.13 and HassOs is running 3.12 and that apk is not part of 3.12 …
So I needed to modify script to download another apk version

Hmm I’m having a small issue: Where do you put the following:

shell_command: 
setup: /config/scripts/setup.sh

Does this go in /config/configuration.yaml or another location?

I put the following in the /config/configuration.yaml file:

shell_command:
  setup: /config/shell/setup.sh

My /config/automations

- id: '1635091517986'
  alias: startup
  description: Adds APK packages to HomeAssistant through apk package manager at boot
  trigger:
  - platform: homeassistant
    event: start
  action:
  - service: shell_command.setup
  initial_state: 'on'

Here is /config/shell/setup.sh:

#! /bin/sh

# apk_add_packages_at_boot.sh
# Script taken from: https://community.home-assistant.io/t/use-apt-get-commands/236873/4

apk update
apk add git-lfs

My log files however say this:

startup: Error executing script. Service not found for call_service at pos 1: Unable to find service shell_command.setup

try to do it manually first

3 Likes

@elRadix

OK - I tried manually. I could install the package. Being this is a docker image however, the added file isn’t going to stay after reboot.

In doing some reading – I think I’m approaching things a little wrong, With what you’re telling me to do I’m adding git-lfs package inside the docker container – homeassistant. I think however I want to add git lfs inside the Home Assistant OS – not the container. I can add this package inside the OS – however after a reboot it’s gone as well.

Based on the information I’m gathering, it looks like there is a great method to add permanent packages inside the homeassistant OS :frowning:

I have the script running at each reboot executing the shell command
you can also execute the the shell command manually in dev tools

automation:

- id: hass_1
  initial_state: true
  alias: System Startup Notification
  trigger:
    platform: homeassistant
    event: start
  action:
    - service: notify.telegram
      data:
        message: Home Assistant back online! at {{now().strftime("%H:%M:%S")}}
    - service: shell_command.setup

setup: !secret shell_command_setup
shell_command_setup: "/config/scripts/setup.sh"

setup. sh

#!/bin/sh
echo "Updating Image with Easyplus Tools"
apk update
apk add -u busybox
apk add busybox-extras
apk add expect
apk add sshpass
pip3 install pexpect
echo "Easyplus Tools Installed"
sh /config/scripts/ssh-login-alert-telegram/deploy.sh
1 Like

What files are you putting the following commands in:

setup: !secret shell_command_setup

shell_command_setup: "/config/scripts/setup.sh"

How do you add packages to the HassOS system? When I access it, I get “apk add…” Apk not found …

Is there also a way to make it permanent after a reboot?

I need to compile something, inside the HassOS itself, I need to add the linux-headers

1 Like