How to fix pyspice OSError: cannot load library 'libngspice.so'

Problem:

When trying to run a PySpice program, you see an error message such as

OSError: cannot load library 'libngspice.so': libngspice.so: cannot open shared object file: No such file or directory.  Additionally, ctypes.util.find_library() did not manage to locate a library called 'libngspice.so'

Solution

Install libngspice, often called libngspice0.

On Ubuntu, install it using

sudo apt -y install libngspice0-dev

You need to install the -dev library since libngspice0 only contains libngspice.so.0 whereas the -dev library contains libngspice.so which is required by pyspice.

Solution (with KiCad installed on Linux)

KiCad automatically installs libngspice-kicad which provides libngspice.so.0 but collides with libngspice0 and libngspice0-dev. To fix this, you can create a symlink:

sudo ln -sf /usr/lib/x86_64-linux-gnu/libngspice.so.0 /usr/lib/x86_64-linux-gnu/libngspice.so

After that, restart your Python script, which should work now.