How to Install and Use PHPY: Bridge PHP with Python Libraries on Ubuntu
This guide walks you through installing PHPY on Ubuntu, covering the required environment setup, building PHP 8.3 from source, installing Anaconda and a Python 3.10 environment, compiling the PHPY extension, configuring php.ini, verifying the installation, and running a sample script that accesses Python's os module from PHP.
What Is PHPY?
PHPY is an open‑source project from the Shiwo team that enables PHP to call any Python package, bringing the rich Python ecosystem (e.g., PyTorch, TensorFlow, NumPy, Pandas, PyQt) into PHP applications. It is not recommended for short‑lived PHP‑FPM/Apache processes because importing and destroying modules can be resource‑intensive.
Prerequisites
Operating system: Ubuntu 22.04.3 LTS
PHP version: 8.1 or newer (the guide builds PHP 8.3)
Python version: 3.10 or newer
Installing PHP 8.3
Download
wget https://www.php.net/distributions/php-8.3.0.tar.gz
tar -zxvf php-8.3.0.tar.gzDownload page: https://www.php.net/downloads
Install Build Dependencies
sudo apt-get install libfcgi-dev libfcgi0ldbl libjpeg-turbo8-dev libmcrypt-dev libssl-dev libc-client2007e libc-client2007e-dev libxml2-dev libbz2-dev libcurl4-openssl-dev libjpeg-dev libpng-dev libfreetype6-dev libkrb5-dev libpq-dev libxslt1-dev libzip-dev libsqlite3-dev libonig-dev pkg-config libssl-dev libbz2-dev libpng-dev libjpeg-dev libfreetype6-dev libc-client2007e-dev libreadline-dev libxslt-dev libzip-devIf a package is missing, search for it with apt-cache search freetype and install the appropriate one.
Configure and Compile
./configure \
--prefix=/usr/local/php-8.3 \
--with-config-file-path=/usr/local/php-8.3/etc \
--with-zlib-dir \
--with-freetype \
--enable-mbstring \
--enable-soap \
--enable-calendar \
--with-curl \
--with-zlib \
--enable-gd \
--disable-rpath \
--enable-inline-optimization \
--with-bz2 \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-mbregex \
--enable-exif \
--enable-bcmath \
--with-mhash \
--with-zip \
--with-pdo-mysql \
--with-mysqli \
--with-mysql-sock=/var/run/mysqld/mysqld.sock \
--with-jpeg \
--with-openssl \
--with-fpm-user=www \
--with-fpm-group=www \
--with-libdir=/lib/x86_64-linux-gnu \
--enable-ftp \
--with-kerberos \
--with-gettext \
--with-xmlrpc \
--with-xsl \
--enable-opcache \
--enable-intl \
--with-pear \
--enable-fpmBuild and Install
make
make installAfter installation, copy the production php.ini:
cp php.ini-production /usr/local/php-8.3/etc/php.iniVerify the PHP version:
/usr/local/php-8.3/bin/php -vInstalling Python via Anaconda
Anaconda provides a bundled Python distribution with over 190 scientific packages.
wget https://repo.anaconda.com/archive/Anaconda3-2023.09-0-Linux-x86_64.sh
sh Anaconda3-2023.09-0-Linux-x86_64.shDefault install path: /home/username/anaconda3
Check the version:
conda --versionCreate a Python 3.10 Virtual Environment
conda create -n tinywan-python310 python=3.10Activate the environment:
conda activate tinywan-python310Installing PHPY
Clone the Repository
git clone https://github.com/swoole/phpy.gitGenerate the configure script
/usr/local/php-8.3/bin/phpize --with-php-config=/usr/local/php-8.3/bin/php-configConfigure with PHP and Python paths
./configure \
--with-php-config=/usr/local/php-8.3/bin/php-config \
--with-python-dir=/home/www/anaconda3/envs/tinywan-python310 \
--with-python-version=3.10Parameter meanings: --with-php-config : path to the PHP configuration binary. --with-python-dir : directory where the Python environment is installed. --with-python-version : major.minor version of Python (e.g., 3.10 ).
Compile and Install PHPY
make -j4
sudo make installAdd the extension to php.ini: extension=phpy.so Reload PHP or restart the FPM service, then verify:
php -m | grep phpyUsage Example
File os.php:
<?php
function main() {
$m = PyCore::import("os");
var_dump($m instanceof PyObject);
$rs = $m->uname();
echo $rs;
echo $rs->version;
}
main();
?>Running the script: /usr/local/php-8.3/bin/php os.php Output shows a PyObject representing the OS information, confirming that PHP can call Python's os.uname() function.
Optional: Upgrade GCC on Ubuntu 18.04
Add the toolchain PPA: sudo add-apt-repository ppa:ubuntu-toolchain-r/test Update packages: sudo apt-get update && sudo apt-get upgrade Install GCC‑9: sudo apt install gcc-9 Make GCC‑9 the default:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90After installation, gcc -v should report version 9.4.0.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
