Skip to content

PHP extensions

PHP can be extended with extensions.

About

Traditional PHP extensions can be installed with apt like mysql, xml, curl, gd, mbstring, zip or intl. You can see more about in this guide.

But some extensions like swoole, imagick or pcov need to be installed manually. A package manager called PECL can be used to install PHP extensions, but it's not recommanded because it's not always up-to-date. You can see more about in this guide.

WARNING

Today, some extensions are maintained and some are not, check errors if you have it and check issues on extensions GitHub repositories to find solutions. For extensions listed here, you can install it with PHP 8.2.

Add manually extension

When you compile manually and extension, sometimes it's not added to your PHP installation. You have to add it manually.

INFO

Keep in mind when you install an extension, it is available only for the PHP version you compiled it with.

Find your php.ini file.

sh
php --ini

Use any editor to add extension.

sh
sudo vim /your/path/to/php.ini

In your php.ini, add this to the first line.

ini
extension="<extension>.so"

Verify extension

Check if PHP can use extension.

sh
php -m | grep <extension>

If you see extension, extension is ready.

For Windows

If you use Windows, installation guide is available in seperate guide.

Extensions

ImageMagick

ImageMagick, invoked from the command line as magick, is a free and open-source cross-platform software suite for displaying, creating, converting, modifying, and editing raster images. Created in 1987 by John Cristy, it can read and write over 200 image file formats. It is widely used in open-source applications.

WARNING

You have to install ImageMagick before you install PHP extension. You can check this guide.

Clone the repository

sh
git clone https://github.com/Imagick/imagick
cd imagick

Compile from source

sh
phpize
./configure
make

Install

sh
sudo make install

MongoDB

MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas.

WARNING

You have to install MongoDB before you install PHP extension.

Install from PECL

sh
sudo pecl install mongodb

Choose default values when you have questions.

PCOV

CodeCoverage compatible driver for PHP.

macOS

Make sure you have pcre2 installed on macOS, you can check 'pcre2.h' file not found if you have errors.

Clone the repository

sh
git clone https://github.com/krakjoe/pcov.git
cd pcov

Compile from source

sh
sudo phpize
./configure --enable-pcov
make
make test

Install

sh
sudo make install

Redis

The open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker.

INFO

To use Redis with PHP, you can use phpredis extension OR predis/predis composer package. phpredis is a C extension and is faster than predis/predis which is written in PHP. So, for production, it's recommended to use phpredis extension.

WARNING

You have to install Redis before you install PHP extension. You can check this guide.

To install this extension, you can install PHP extension OR composer package.

PHP extension

INFO

It's recommended to use phpredis extension for production because it's faster than predis/predis.

Clone the repository

sh
git clone https://github.com/phpredis/phpredis.git
cd phpredis

Compile from source

sh
sudo phpize
./configure [--enable-redis-igbinary] [--enable-redis-msgpack] [--enable-redis-lzf [--with-liblzf[=DIR]]] [--enable-redis-zstd]
make

Install

sh
sudo make install

predis/predis composer package

INFO

Predis is a Redis client written entirely in PHP and does not require any additional extensions. It's not as fast as phpredis extension but is easier to install.

sh
composer require predis/predis

WARNING

If you use predis/predis, you don't need to install phpredis extension but predis/predis is not as fast as phpredis extension.

Usage

Laravel

To use Redis with Laravel, you have to set your Redis client in your .env file.

txt
# for phpredis
REDIS_CLIENT=phpredis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
.env
txt
# for predis/predis
REDIS_CLIENT=predis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
.env
RedisException : Connection refused

Check if Redis server is running.

sh
sudo systemctl status redis

If it's not running, start Redis server.

sh
sudo systemctl start redis
Class 'Redis' not found

If you use phpredis extension and you have this error, check if extension is enabled.

sh
php -m | grep redis

If it's not enabled, enable it with Add manually extension guide.

If you use Laravel Valet, reinstall Valet.

sh
valet install

SQL Server

Microsoft SQL Server is a relational database management system developed by Microsoft. As a database server, it is a software product with the primary function of storing and retrieving data as requested by other software applications—which may run either on the same computer or on another computer across a network.

WARNING

For SQL Server, Microsoft offers only installation with PECL. You can check this guide to install PECL.

sh
sudo pecl install sqlsrv pdo_sqlsrv
sh
brew install autoconf automake libtool
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
HOMEBREW_ACCEPT_EULA=Y brew install msodbcsql18 mssql-tools
sudo pecl install sqlsrv pdo_sqlsrv
sh
brew install autoconf automake libtool
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
HOMEBREW_ACCEPT_EULA=Y brew install msodbcsql18 mssql-tools
sudo CXXFLAGS="-I/opt/homebrew/opt/unixodbc/include/" LDFLAGS="-L/opt/homebrew/lib/" pecl install sqlsrv
sudo CXXFLAGS="-I/opt/homebrew/opt/unixodbc/include/" LDFLAGS="-L/opt/homebrew/lib/" pecl install pdo_sqlsrv

To upgrade

sh
sudo pecl upgrade sqlsrv
sudo pecl upgrade pdo_sqlsrv
sh
sudo CXXFLAGS="-I/opt/homebrew/opt/unixodbc/include/" LDFLAGS="-L/opt/homebrew/lib/" pecl upgrade sqlsrv
sudo CXXFLAGS="-I/opt/homebrew/opt/unixodbc/include/" LDFLAGS="-L/opt/homebrew/lib/" pecl upgrade pdo_sqlsrv

Swoole

Swoole is an event-driven, asynchronous, coroutine-based concurrency library with high performance for PHP.

Requirements

sh
sudo apt install -y g++ make

Clone the repository

sh
git clone https://github.com/swoole/swoole-src.git && \
cd swoole-src

Compile from source. When you compile some questions may appear, you can use the default values.

sh
phpize
./configure
make

Install

sh
sudo make install

WinRAR

WinRAR is a trialware file archiver utility for Windows, developed by Eugene Roshal of win.rar GmbH. It can create and view archives in RAR or ZIP file formats, and unpack numerous archive file formats.

With recent PHP versions, you could have some problem with installation from package managers.

To install package, you have to compile the package, you can find an example here.

PHP 8.2

For PHP 8.2, even manual compilation, you will have maybe some errors. A pull request offer to fix this problem, in some months PR could be accepted. But even with this bug, PHP and rar extension will work.

Clone the repository

sh
cd ~
git clone https://github.com/cataphract/php-rar
cd php-rar

Compile from source

sh
phpize
./configure
make

Install

sh
sudo make install

Troubles

pcre2.h file not found

On macOS you need to install pcre2 and link it to current PHP version.

sh
brew install pcre2
# OR
brew reinstall pcre2

File /opt/homebrew/opt/pcre2/include/pcre2.h have to be present.

Check current versions

sh
ls /opt/homebrew/Cellar/pcre2/
ls /opt/homebrew/Cellar/php/

Link pcre2 to current php.

sh
ln -s /opt/homebrew/Cellar/pcre2/<PCRE2_VERSION>/include/pcre2.h /opt/homebrew/Cellar/php/<PHP_VERSION>/include/php/ext/pcre/pcre2.h
# OR
ln -s /opt/homebrew/opt/pcre2/include/pcre2.h /opt/homebrew/opt/php@8.4/include/php/ext/pcre/pcre2.h

Script

You can use this script to execute all steps.

sh
php_version=$(php -v | grep ^PHP | awk '{print $2}')
pcre2_version=$(ls /opt/homebrew/Cellar/pcre2/)
sudo ln -s /opt/homebrew/Cellar/pcre2/$pcre2_version/include/pcre2.h /opt/homebrew/Cellar/php/$php_version/include/php/ext/pcre/pcre2.h

Attempt to perform an operation not allowed

If you have an error like this

sh
attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/408.
output

Check you have ghostscript installed with gs --version. And if it's okay, open /etc/ImageMagick-6/policy.xml to remove some lines

xml
<!-- disable ghostscript format types -->
<!-- <policy domain="coder" rights="none" pattern="PS" /> -->
<!-- <policy domain="coder" rights="none" pattern="PS2" /> -->
<!-- <policy domain="coder" rights="none" pattern="PS3" /> -->
<!-- <policy domain="coder" rights="none" pattern="EPS" /> -->
<!-- <policy domain="coder" rights="none" pattern="PDF" /> -->
<!-- <policy domain="coder" rights="none" pattern="XPS" /> -->
/etc/ImageMagick-6/policy.xml

VSCode with PHP Intelephense

With PHP Intelephense, you can add imagick to your settings.json.

json
{
  "intelephense.stubs": ["imagick", "rar"]
}

Scripts

You can add extension with a script.

Find PECL extension directory

sh
pecl_path=$(pecl config-get ext_dir)

Find PHP configuration file

sh
phpini_path=$(php -i | grep /.+/php.ini -oE)

Copy extension to PECL extension directory and add extension to PHP configuration file

sh
sudo cp ./modules/<extension>.so $pecl_path
sudo echo "extension=<extension>.so" > $phpini_path

Example

INFO

After compilation, you can use this script to add rar extension.

You will find PHP extension here: ~/php-rar/modules/rar.so. To use it, find PECL extension directory.

sh
pecl_path=$(pecl config-get ext_dir)
phpini_path=$(php -i | grep /.+/php.ini -oE)
sudo cp ~/php-rar/modules/rar.so $pecl_path
echo "extension=rar.so" | sudo tee -a $phpini_path

Now you remove local php-rar.

sh
rm -r ~/php-rar

MIT License