Blog

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.2.15.
    2. Change to the directory php-YOUR-VERSION/ext/EXTENSION which in my case was php-7.2.15/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.2/fpm/php.ini
      • /etc/php/7.2/cli/php.ini
    8. Finally restart either your webserver and/or the php-fpm service: sudo service php7.2-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.

    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!

    HackerNews submission / discussion

    Back to the article list.

    SmallData newsletter

    Subscribe to get articles as they are published direct to your inbox!