How to Install Swoole

Install by binary releases

Linux users

Swoole is available as a PECL compatible package

#!/bin/bash
pecl install swoole
Swoole PECL configure options

You can also install swoole extension with PHP PECL configure options, some options require the third party libraries you can find bellow.

#!/bin/bash
pecl install --configureoptions 'enable-sockets="no" enable-openssl="yes" enable-http2="yes" enable-mysqlnd="yes" enable-swoole-json="no" enable-swoole-curl="yes"' swoole

Or

#!/bin/bash
pecl install -D 'enable-sockets="no" enable-openssl="yes" enable-http2="yes" enable-mysqlnd="yes" enable-swoole-json="no" enable-swoole-curl="yes"' swoole
MacOS X (macOS) users
#!/bin/bash
brew install php # if you haven't got PHP installed
pecl install swoole

Building swoole from sources

Compiling requirements
  • Linux, OS X or Cygwin, WSL
  • PHP 7.0.0 or later (The higher the version, the better the performance.)
  • GCC 4.8 or later

The process of compile and install the swoole extension for PHP:

Download the source packages from Releases or clone from git repo:

git clone https://github.com/swoole/swoole-src.git && \
cd swoole-src && \
git checkout v4.6.x

Compile and install at the source folder:

phpize && \
./configure && \
make && make install

Enable Swoole extension in PHP

After installing the swoole extension to the PHP extensions directory, you will need to edit php.ini and add an extension=swoole.so line before you can use the swoole extension.

php -i | grep php.ini                      # check the php.ini file location
sudo echo "extension=swoole.so" >> php.ini  # add the extension=swoole.so to the end of php.ini
php -m | grep swoole                       # check if the swoole extension has been enabled

Configuration paramaters

These configurations are used for enabling some features.

--enable-swoole-debug

Enable the debug logs of swoole. Don't enable this configuration in production environment.

--enable-sockets

Enable sockets support. It depends on the PHP sockets extension. If this configuration has been enabled, the function swoole_event_add() could add the connection created by the sockets extension to the event loop of swoole. And the function getSocket() depends on this configuration.

--enable-openssl

Enable openssl support. It depends on the libssl.so library given by operating system.

--with-openssl-dir=DIR

Set the path of openssl library, for example:--with-openssl-dir=/opt/openssl/.

--enable-http2

Enable the support of HTTP2. It depends on nghttp2 library.

--enable-mysqlnd

Enable the support of mysqlnd, for example $mysql->escapse.

--enable-swoole-json

Enable Swoole version JSON support.

--enable-swoole-curl

Enable native curl hook support for coroutines.

Openssl errors

You have to include openssl library in your path:

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

You have to install libopenssl-dev if enable --enable-openssl

Segment fault errors

You can follow the instruction about how to debug the segment fault errors.

Missing libcurl errors

swoole/thirdparty/php/curl/curl_interface.h:8:10: fatal error: curl/curl.h: No such file or directory
    8 | #include <curl/curl.h>
      |          ^~~~~~~~~~~~~

You have to install libcurl-dev if enable --enable-swoole-curl, for example:

apt-get install libcurl4-openssl-dev

How to find check current enabled options:

php --ri swoole