Fatal error: Class ‘DOMDocument’ not found — How to Fix (php-xml)

Error: when installing or running a PHP module or script, the web server log shows Fatal error: Class ‘DOMDocument’ not found.
In short: PHP is missing the DOM extension (XML handling). On Debian and Ubuntu install the php-xml package, on CentOS php-xml via yum, on FreeBSD the php5-dom port. Restart the web server afterwards.
Fix for different systems
FreeBSD (my case, PHP 5): install the php5-dom module from ports or packages.
CentOS, RHEL, Fedora — as readers suggested:
yum install php-xml
Debian, Ubuntu:
apt install php-xml
(in older releases the package was php5-xml; for a specific PHP version, e.g. php7.4-xml).
After installing, restart Apache or PHP-FPM so the module is loaded.
How to check the module is installed
php -m | grep -i dom
If dom is in the output, the extension is enabled. Keep in mind that command-line PHP and the web server’s PHP can have different settings: if the module shows up in the console but the site still errors, check phpinfo() on the site itself.
If your site is on shared hosting
You can’t install packages on shared hosting — ask your host’s support to enable the PHP DOM / XML extension. Often you can enable it yourself in the control panel, in the PHP version settings.