Sodium is not loading on XAMPP PHP 7.2
If you are running a Windows environment with XAMPP and PHP 7.2, encountering missing sodium functions despite seeing Argon2i work correctly points to a specific configuration quirk rather than a broken core extension. The official documentation often states that the Sodium extension is part of the core and requires no installation. While this holds true for many modern Linux distributions where the module is compiled directly into the binary, Windows buildsâespecially those bundled with XAMPPâoperate on a different principle: they rely on external .dll files to extend functionality.
The symptom you are describing is deceptive. Checking get_loaded_extensions() returns an empty list for Sodium, yet Argon2i constants are defined and password_hash() successfully generates hashes using the Argon2i algorithm. This discrepancy is the critical clue. The Sodium extension provides a suite of cryptographic primitives including random_bytes(), crypto_secretbox(), and high-level hashing functions like SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13. If you rely on these specific functions for application security, their absence means your code will throw fatal errors when attempting to execute them.
The root cause lies in the architecture of PHP on Windows. Unlike Unix-like systems where extensions are often compiled statically or loaded automatically by default configurations, XAMPP defaults to a minimal set of enabled extensions to ensure stability and reduce memory footprint. The extension=php_sodium.dll line you found in your php.ini is the key. In a Linux environment, this line might be ignored if the extension is built-in, but on Windows, it is mandatory for the DLL to load.
However, simply uncommenting that line is not always sufficient. You must verify the physical presence of the corresponding .dll file within the XAMPP installation directory. The php_sodium.dll file usually resides in the ext\php_sodium-7.2.x folder. If this file is missing from your XAMPP distribution, or if the version of the DLL does not match your exact PHP 7.2 build (e.g., mismatched MSVC runtime versions), PHP will fail to load it silently during initialization unless specific error reporting settings are enabled.
A common pitfall in this scenario is assuming that because Argon2i works, the underlying cryptographic library is fully functional. While Argon2i relies on similar low-level C libraries (like libsodium or OpenSSL wrappers), the PHP wrapper itself might be loaded while the broader Sodium API remains disabled if the extension was only partially compiled or if specific function groups were stripped during the build process. Additionally, check your php.ini for any conflicting settings that might override the load order or disable specific function groups within the extension, though this is rare in standard distributions.
To resolve this without manually compiling extensions from sourceâa difficult task on Windowsâyou should verify that the XAMPP package you installed includes the Sodium support files. If it does not, upgrading to a version of XAMPP that explicitly bundles the Sodium extension or switching to a PHP distribution known for including all core extensions by default (such as MAMP Pro or specific Composer-managed setups) is often the most pragmatic solution. Alternatively, if you are comfortable with CMake, compiling the extension from source against your specific PHP installation ensures binary compatibility.
For developers using frameworks like Laravel, missing the Sodium extension can break features that rely on random_bytes() for token generation or specific hashing algorithms. If you are deploying to a production environment where XAMPP is not available (as it is rarely recommended for production), ensuring the php_sodium.dll is present and correctly configured in your php.ini is a critical step in your security hardening checklist. Always verify the presence of the DLL file on disk before assuming PHP will find it automatically, as Windows does not perform implicit path resolution for extension loading like some Linux distributions do.