Sam Hooke

Installing systemd-python on Ubuntu

Following are steps for installing the official systemd Python package on Python using Poetry, on Ubuntu 22.04:

Steps §

Install dependencies §

Before installing the Python package, you must install the libsystemd-dev package for Ubuntu:

apt-get install libsystemd-dev

You’ll probably also need a compiler such as gcc.

Some documentation (such as the README.md) mentions needing to install libsystemd-journal-dev and various other libsystemd-* packages. However, as far as I can tell, these have all been combined into a single libsystemd-dev package. I can’t find a good reference for this, but the 2015 Debian mailing list mentions a bunch of these libsystemd-* packages as being either “deprecated” or a “transitional package”.

Install systemd-python §

Then you can add systemd-python to your Poetry project:

poetry add systemd-python

If all is right, it should build and install the dependency.

Troubleshooting §

Error: Cannot find libsystemd or libsystemd-journal §

The following error likely means you need to install libsystemd-dev.

$ poetry add systemd-python
Creating virtualenv use-systemd-Bm5-ZGXX-py3.10 in /home/sam/.cache/pypoetry/virtualenvs
Using version ^235 for systemd-python

Updating dependencies
Resolving dependencies... (8.8s)

Package operations: 1 install, 0 updates, 0 removals

 • Installing systemd-python (235): Failed

 ChefBuildError

 Backend subprocess exited when trying to invoke get_requires_for_build_wheel
 
 Cannot find libsystemd or libsystemd-journal:
 
 Package libsystemd was not found in the pkg-config search path.
 Perhaps you should add the directory containing `libsystemd.pc'
 to the PKG_CONFIG_PATH environment variable
 No package 'libsystemd' found
 
 Package libsystemd-journal was not found in the pkg-config search path.
 Perhaps you should add the directory containing `libsystemd-journal.pc'
 to the PKG_CONFIG_PATH environment variable
 No package 'libsystemd-journal' found
 
 

 at ~/.local/pipx/venvs/poetry/lib/python3.10/site-packages/poetry/installation/chef.py:164 in _prepare
     160│ 
     161│                 error = ChefBuildError("\n\n".join(message_parts))
     162│ 
     163│             if error is not None:
   → 164│                 raise error from None
     165│ 
     166│             return path
     167│ 
     168│     def _prepare_sdist(self, archive: Path, destination: Path | None = None) -> Path:

Note: This error originates from the build backend, and is likely not a problem with poetry but with systemd-python (235) not supporting PEP 517 builds. You can verify this by running 'pip wheel --no-cache-dir --use-pep517 "systemd-python (==235)"'.

These are rough notes that vary greatly in quality and length, but prove useful to me, and hopefully to you too!

← Previous: python-systemd != python-systemd
Next: Reading from and writing to journald in Python →