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.
php --ini
Use any editor to add extension.
sudo vim /your/path/to/php.ini
In your php.ini
, add this to the first line.
extension="<extension>.so"
Verify extension
Check if PHP can use extension.
php -m | grep <extension>
If you see extension
, extension is ready.
For Windows
If you use Windows, installation guide is available in seperate guide.
Swoole
INFO
Swoole is an event-driven, asynchronous, coroutine-based concurrency library with high performance for PHP.
- Official website: https://www.swoole.com/
- GitHub: https://github.com/swoole/swoole-src
- PECL: https://pecl.php.net/package/swoole with
swoole
- PHP docs: https://www.php.net/manual/en/book.swoole.php
- Compile from source: https://github.com/swoole/swoole-src#2-install-from-source-recommended
Requirements
sudo apt install -y g++ make
Clone the repository
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.
phpize
./configure
make
Install
sudo make install
PCOV
INFO
CodeCoverage compatible driver for PHP
- GitHub: https://github.com/krakjoe/pcov
- PECL: https://pecl.php.net/package/pcov with
pcov
- Compile from source: https://github.com/krakjoe/pcov/blob/develop/INSTALL.md
macOS
Make sure you have pcre2
installed on macOS, you can check 'pcre2.h' file not found if you have errors.
Clone the repository
git clone https://github.com/krakjoe/pcov.git
cd pcov
Compile from source
phpize
./configure --enable-pcov
make
make test
Install
sudo make install
Redis
INFO
The open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker.
- GitHub PECL: https://github.com/phpredis/phpredis
- PECL: http://pecl.php.net/package/redis with
redis
- GitHub
predis/predis
: https://github.com/predis/predis - Official website: https://redis.io/
WARNING
You have to install Redis before you install PHP extension. You can check this guide.
To install this extension, you can install extension OR composer package.
Install from source
Clone the repository
git clone https://github.com/phpredis/phpredis.git
cd phpredis
Compile from source
phpize
./configure [--enable-redis-igbinary] [--enable-redis-msgpack] [--enable-redis-lzf [--with-liblzf[=DIR]]] [--enable-redis-zstd]
make
Install
sudo make install
Install from composer
INFO
Predis is a Redis client written entirely in PHP and does not require any additional extensions.
composer require predis/predis
ImageMagick
INFO
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.
- GitHub: https://github.com/Imagick/imagick
- PECL: http://pecl.php.net/package/imagick with
imagick
- PHP docs: https://www.php.net/manual/en/book.imagick.php
- Official website: https://imagemagick.org/index.php
WARNING
You have to install ImageMagick before you install PHP extension. You can check this guide.
Clone the repository
git clone https://github.com/Imagick/imagick
cd imagick
Compile from source
phpize
./configure
make
Install
sudo make install
- Add manually extension
- Verify extension
- You have an error like
attempt to perform an operation not allowed
, check here
WinRAR
INFO
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.
- GitHub: https://github.com/cataphract/php-rar
- PECL: https://pecl.php.net/package/rar with
rar
- PHP docs: https://www.php.net/manual/en/book.rar.php
- Official website: https://www.win-rar.com/start.html?&L=0
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
cd ~
git clone https://github.com/cataphract/php-rar
cd php-rar
Compile from source
phpize
./configure
make
Install
sudo make install
SQL Server
INFO
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.
- GitHub: https://github.com/Microsoft/msphpsql
- PECL: https://pecl.php.net/package/sqlsrv with
sqlsrv
- PHP docs: https://www.php.net/manual/en/book.sqlsrv.php
- Official website: https://www.microsoft.com/en-us/sql-server/sql-server-downloads
WARNING
For SQL Server, Microsoft offers only installation with PECL. You can check this guide to install PECL.
sudo pecl install sqlsrv pdo_sqlsrv
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
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
sudo pecl upgrade sqlsrv
sudo pecl upgrade pdo_sqlsrv
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
Troubles
pcre2.h
file not found
On macOS you need to install pcre2
and link it to current PHP version.
brew install pcre2
Check current versions
ls /opt/homebrew/Cellar/pcre2/
ls /opt/homebrew/Cellar/php/
Link pcre2
to current php
.
ln -s /opt/homebrew/Cellar/pcre2/YOU_VERSION/include/pcre2.h /opt/homebrew/Cellar/php/YOU_VERSION/include/php/ext/pcre/pcre2.h
Script
You can use this script to execute all steps.
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
attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/408.
Check you have ghostscript
installed with gs --version
. And if it's okay, open /etc/ImageMagick-6/policy.xml
to remove some lines
<!-- 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" /> -->
VSCode with PHP Intelephense
With PHP Intelephense, you can add imagick
to your settings.json
.
{
"intelephense.stubs": ["imagick", "rar"]
}
Scripts
You can add extension with a script.
Find PECL extension directory
pecl_path=$(pecl config-get ext_dir)
Find PHP configuration file
phpini_path=$(php -i | grep /.+/php.ini -oE)
Copy extension to PECL extension directory and add extension to PHP configuration file
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.
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
.
rm -r ~/php-rar