Add /snap/bin to PATH used by systemd

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.


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

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