Add /snap/bin to PATH used by systemd

34,166

Solution 1

According to the systemd documentation, its PATH is set on compilation (see section "Command lines"):

If the command is not a full (absolute) path, it will be resolved to a full path using a fixed search path determinted at compilation time. Searched directories include /usr/local/bin/, /usr/bin/, /bin/ on systems using split /usr/bin/ and /bin/ directories, and their sbin/ counterparts on systems using split bin/ and sbin/. It is thus safe to use just the executable name in case of executables located in any of the "standard" directories, and an absolute path must be used in other cases. Using an absolute path is recommended to avoid ambiguity. Hint: this search path may be queried using systemd-path search-binaries-default.

The command to query the path on my Ubuntu 18.04 was sudo systemd-path search-binaries (on Arch, it was systemd-path search-binaries-default):

$ sudo systemd-path search-binaries
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

So, you have the following options:

The good: Edit the unit file so that it uses absolute paths. Assuming you have access to it, this is by far the best solution. It makes the file conform to the specification, it allows you to copy it to other machines, it even silences the warning messages.

The bad: Recompile systemd from source, and change the path. This is time consuming, complicated and an all around bad idea unless you really know what you're doing. Even if you do, this seems like a bad solution. You're not going to be able to recompile systemd every time you setup a new machine.

The ugly: If you really can't fix the unit file, you can always create a symlink in /usr/bin pointing to docker

sudo ln -s /snap/bin/docker /usr/bin/docker

Solution 2

This is the way to fix this.

To check the value of the variable PATH :

echo $PATH

To add /snap/bin

export PATH="$PATH:/snap/bin"

If this does not work, there is a file called /etc/environment

Share:
34,166

Related videos on Youtube

rgov
Author by

rgov

Updated on September 18, 2022

Comments

  • rgov
    rgov almost 2 years

    I installed Docker via snap while setting up Ubuntu Server 18.10.

    If I have a systemd unit file that references the docker command, I get this error:

    Executable "docker" not found in path "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
    

    I'm not sure where it is getting this search path. It does not match what is found in /etc/environment.

    Without modifying the unit file, can I globally change the search path used by systemd to include /snap/bin?


    On an Ubuntu 18.04 system, just using docker without the full path resulted in the error Executable path is not absolute. I ideally want the same service file to work with both snap's Docker and apt's docker-ce packages.

  • rgov
    rgov over 5 years
    Thanks. Editing the unit file isn't ideal because I want to distribute my unit file without caring how the system installed Docker (apt or snap, or whatever future package system is dreamt up). But it seems far less intrusive than asking end users to re-compile systemd!
  • rgov
    rgov over 5 years
    Actually, systemd-path search-binaries reveals /snap/bin is in that list. It does not match the error from systemd.
  • terdon
    terdon over 5 years
    @rgov I have never touched systemd's unit files, so I don't know if this is possible, but perhaps you could do something like if $snapDocker; else $aptDocker? But if you're getting the error even if the dir is in the PATH then it sounds like a proper bug and worth reporting.
  • terdon
    terdon almost 5 years
    Please read the question more carefully. The OP knows about /etc/environment and the question specifically states this must be fixed without editing the unit file.
  • Bhikkhu Subhuti
    Bhikkhu Subhuti almost 5 years
    I’ll probably make a question and answer with this answer. That was how I got here. I actually had a problem with a different district but we all use Ubuntu for help...:)
  • terdon
    terdon almost 5 years
    Yes, please ask a new question. This answer is about adding snap to a regular user's path, it won't help in the situation the OP describes. It might be a fine answer for another question though!