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 and pkg-config packages for Ubuntu:

apt-get install libsystemd-dev pkg-config

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
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)"'.

Error: FileNotFoundError: [Errno 2] No such file or directory: 'pkg-config' §

The following error likely means you need to install pkg-config.

$ poetry add systemd-python
Using version ^235 for systemd-python

Updating dependencies
Resolving dependencies... (7.1s)

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

  Traceback (most recent call last):
    File "/home/vagrant/.local/share/pipx/venvs/poetry/lib/python3.10/site-packages/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
      main()
    File "/home/vagrant/.local/share/pipx/venvs/poetry/lib/python3.10/site-packages/pyproject_hooks/_in_process/_in_process.py", line 335, in main
      json_out['return_val'] = hook(**hook_input['kwargs'])
    File "/home/vagrant/.local/share/pipx/venvs/poetry/lib/python3.10/site-packages/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
      return hook(config_settings)
    File "/tmp/tmpxz3s6ldw/.venv/lib/python3.10/site-packages/setuptools/build_meta.py", line 325, in get_requires_for_build_wheel
      return self._get_build_requires(config_settings, requirements=['wheel'])
    File "/tmp/tmpxz3s6ldw/.venv/lib/python3.10/site-packages/setuptools/build_meta.py", line 295, in _get_build_requires
      self.run_setup()
    File "/tmp/tmpxz3s6ldw/.venv/lib/python3.10/site-packages/setuptools/build_meta.py", line 487, in run_setup
      super().run_setup(setup_script=setup_script)
    File "/tmp/tmpxz3s6ldw/.venv/lib/python3.10/site-packages/setuptools/build_meta.py", line 311, in run_setup
      exec(code, locals())
    File "<string>", line 55, in <module>
    File "<string>", line 40, in lib
    File "<string>", line 18, in pkgconfig
    File "<string>", line 6, in call
    File "/usr/lib/python3.10/subprocess.py", line 971, in __init__
      self._execute_child(args, executable, preexec_fn, close_fds,
    File "/usr/lib/python3.10/subprocess.py", line 1863, in _execute_child
      raise child_exception_type(errno_num, err_msg, err_filename)
  FileNotFoundError: [Errno 2] No such file or directory: 'pkg-config'


  at ~/.local/share/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)"'.