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.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
How to Install and Use PHPY: Bridge PHP with Python Libraries on Ubuntu

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.gz
Download 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-dev
If 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-fpm

Build and Install

make
make install

After installation, copy the production php.ini:

cp php.ini-production /usr/local/php-8.3/etc/php.ini

Verify the PHP version:

/usr/local/php-8.3/bin/php -v

Installing 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.sh
Default install path: /home/username/anaconda3

Check the version:

conda --version

Create a Python 3.10 Virtual Environment

conda create -n tinywan-python310 python=3.10

Activate the environment:

conda activate tinywan-python310

Installing PHPY

Clone the Repository

git clone https://github.com/swoole/phpy.git

Generate the configure script

/usr/local/php-8.3/bin/phpize --with-php-config=/usr/local/php-8.3/bin/php-config

Configure 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.10
Parameter 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 install

Add the extension to php.ini: extension=phpy.so Reload PHP or restart the FPM service, then verify:

php -m | grep phpy

Usage 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 90
After installation, gcc -v should report version 9.4.0.
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonPHPInstallationUbuntuphpy
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.