Fatal error: Call to undefined function socket_create()

Fatal error: Call to undefined function socket_create()

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Fatal error: Call to undefined function socket_create()

The Fatal error: Call to undefined function socket_create() exception is a classic symptom of running PHP code in an environment that doesn't support low-level networking operations. While your syntax for creating a TCP socket appears correct, the underlying system library required to execute that function has simply not been compiled into your PHP interpreter or enabled via the php.ini configuration. When you attempt to run a WebSocket client script and encounter this error immediately upon execution, the application fails before it can even initialize the connection, resulting in the "Socket Status: 3 (Closed)" state you are seeing in your browser.

The root cause is almost always related to the SAPI (Server API) or the specific build flags used when compiling PHP. The socket_create function relies on the BSD sockets library. If this extension is missing, PHP treats these functions as undefined. This issue frequently arises when deploying applications on shared hosting environments, using certain Docker base images, or when switching between different PHP distributions like XAMPP, MAMP, or Alpine Linux containers without verifying the extensions list.

To resolve this, you must first verify which socket functions are available in your current environment. Run php -m from your command line to inspect the loaded modules. If sockets is not listed in the output, you have confirmed that the extension is missing.

Enabling the Socket Extension

In most standard distributions (such as Ubuntu, Debian, or CentOS), PHP is installed as a separate binary from the system's native sockets library. The PHP interpreter needs to be explicitly linked against libsocket and libbsd. If the sockets extension exists in your installation but is disabled, you can enable it by editing your php.ini file.

Look for a line resembling:

;extension=sockets

Remove the semicolon or set it to:

extension=sockets

After saving the changes, restart your web server (Apache, Nginx with PHP-FPM) or the PHP process manager (like Supervisor or Swoole). If you are running a CLI script directly, simply restarting the shell session is not enough; you must ensure the php.ini file used by that specific binary contains the correct settings.

The Extension vs. Library Distinction

A common misconception is that installing the PHP sockets extension via a package manager like apt-get install php-sockets is sufficient on its own. On many Linux systems, this package only installs the PHP module file (the .so file) but does not ensure the underlying C libraries are present or linked correctly.

If you are building your own Docker image or using Alpine Linux, note that Alpine uses musl-libc, which often breaks standard extensions that rely on GNU libc functions found in BSD sockets implementations. In such cases, you might need to switch to a Debian-based image or ensure you install the specific compatibility libraries:

# Example for Debian/Ubuntu based images
apt-get update
apt-get install -y php-sockets libbsd0

For Alpine Linux, you may need to use musl-socat or switch your base image entirely because standard PHP builds often fail to find socket_create unless the linker flags are passed correctly during compilation.

Debugging and Verification

Once you have configured the environment, verify that the function is recognized by running a simple test script:

<?php
if (function_exists('socket_create')) {
    echo "socket_create is available.";
} else {
    echo "socket_create is NOT available.";
}
?>

If this outputs successfully, your server-side setup is correct. However, if you are using a framework like Laravel or Symfony, ensure that you are not inadvertently loading an older PHP binary that lacks the extension while serving requests with a newer one. Frameworks often rely on php-fpm configurations; check that the cli and fpm versions match regarding their compiled extensions.

If you encounter issues where the function works in CLI but fails in a web context, double-check your virtual host configuration or load balancer settings to ensure the same PHP binary is serving the request. In enterprise environments, sometimes specific modules are loaded only for CLI tools and not for the web server daemon, requiring a dedicated package like php-apache-sockets or similar distribution-specific packages.

Conclusion on Connectivity

By ensuring the sockets extension is enabled and the underlying system libraries are correctly linked, your socket_create call will execute without triggering a fatal error. This allows your WebSocket client to proceed with establishing the TCP connection, moving past the "Closed" status and enabling successful message exchange between your PHP backend and the client application.

Our network