Victor is a full stack software engineer who loves travelling and building things. Most recently created Ewolo, a cross-platform workout logger.
How to install a php extension from source on Ubuntu, e.g. php_oci

I recently had to work on a project that was written in PHP and was getting data from an Oracle database. Installing php on Ubuntu is pretty straightforward but connecting to the database unfortunately required quite a few gymnastics! In this article we will look at how to compile PHP extensions on Ubuntu 18.04 and above, specifically the pdo_oci extension. Note that in order for this extension to work, the oracle drivers must be already installed. See this article for instructions on how to setup Oracle drivers on Ubuntu.

The general procedure to install extensions is as follows:

  1. Download the PHP source with same version that you have installed and unzip it. In my case it was PHP 7.4.3.
  2. Change to the directory php-YOUR-VERSION/ext/EXTENSION which in my case was php-7.4.3/ext/pdo_oci.
  3. Run phpize
  4. Configure the extension, which in my case was ./configure --with-pdo-oci=instantclient,/opt/oracle/instantclient_18_3
  5. Compile the extension via make
  6. Install it system-wide sudo make install
  7. Update the PHP configuration to use the newly minted extension. In my case, I needed to update the following files to add/uncomment the following line extension=pdo_oci.so:
    • /etc/php/7.4/fpm/php.ini
    • /etc/php/7.4/cli/php.ini
  8. php -m should now list PDO_OCI as an enabled module.
  9. Finally restart either your webserver and/or the php-fpm service: sudo service php7.4-fpm restart

This above method installs a pdo_oci.so in the PHP extensions folder, without the need for recompiling all of PHP. Note that in my case, the project in question was using the PDO OCI driver which is actually experimental and not recommended. The officially approved Oracle driver is OCI and can be installed via pecl install oci8 (provide the instant client path when prompted instantclient,/opt/oracle/instantclient_18_3). Note that if this does not work further instructions can be found here.

Note that if you use Apache as your webserver rather than Nginx then the pdo_oci module needs to be separately enabled in Apache as well. Create /etc/php/7.4/mods-available/pdo_oci.ini with the following:


; priority=20
extension=pdo_oci.so  

Finally sudo phpenmod pdo_oci and sudo service apache2 restart to enable the extension with Apache.

phpinfo() should now show all the extensions that have been enabled and you can always check your webserver error logs for any issues. Happy coding!