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 --iniUse any editor to add extension.
sudo vim /your/path/to/php.iniIn 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.
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.
- 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 imagickCompile from source
phpize
./configure
makeInstall
sudo make install- Add manually extension
- Verify extension
- You have an error like
attempt to perform an operation not allowed, check here
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.
- GitHub: https://github.com/mongodb/mongo-php-driver
- PECL: http://pecl.php.net/package/mongodb with
mongodb - PHP docs: https://www.php.net/manual/en/book.mongodb.php
- Official website: https://www.mongodb.com/
WARNING
You have to install MongoDB before you install PHP extension.
Install from PECL
sudo pecl install mongodbChoose default values when you have questions.
PCOV
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 pcovCompile from source
sudo phpize
./configure --enable-pcov
make
make testInstall
sudo make installRedis
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.
- GitHub PECL (PHP extension): https://github.com/phpredis/phpredis
- PECL (PHP extension): http://pecl.php.net/package/redis with
redis - GitHub
predis/predis(Composer package): 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 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
git clone https://github.com/phpredis/phpredis.git
cd phpredisCompile from source
sudo phpize
./configure [--enable-redis-igbinary] [--enable-redis-msgpack] [--enable-redis-lzf [--with-liblzf[=DIR]]] [--enable-redis-zstd]
makeInstall
sudo make installpredis/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.
composer require predis/predisWARNING
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.
# for phpredis
REDIS_CLIENT=phpredis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379# for predis/predis
REDIS_CLIENT=predis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379RedisException : Connection refused
Check if Redis server is running.
sudo systemctl status redisIf it's not running, start Redis server.
sudo systemctl start redisClass 'Redis' not found
If you use phpredis extension and you have this error, check if extension is enabled.
php -m | grep redisIf it's not enabled, enable it with Add manually extension guide.
If you use Laravel Valet, reinstall Valet.
valet installSQL 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.
- 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_sqlsrvbrew 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_sqlsrvbrew 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_sqlsrvTo upgrade
sudo pecl upgrade sqlsrv
sudo pecl upgrade pdo_sqlsrvsudo 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_sqlsrvSwoole
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++ makeClone the repository
git clone https://github.com/swoole/swoole-src.git && \
cd swoole-srcCompile from source. When you compile some questions may appear, you can use the default values.
phpize
./configure
makeInstall
sudo make installWinRAR
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-rarCompile from source
phpize
./configure
makeInstall
sudo make installTroubles
pcre2.h file not found
On macOS you need to install pcre2 and link it to current PHP version.
brew install pcre2
# OR
brew reinstall pcre2File /opt/homebrew/opt/pcre2/include/pcre2.h have to be present.
Check current versions
ls /opt/homebrew/Cellar/pcre2/
ls /opt/homebrew/Cellar/php/Link pcre2 to current php.
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.hScript
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.hAttempt 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_pathExample
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_pathNow you remove local php-rar.
rm -r ~/php-rar