Software Engineer · Teacher · Author · Vegan

Using SQLite with PHP on Heroku

Last reviewed on January 24, 2020

1. Our Project

Let’s say we have a project with the following directory structure:

  • index.php
  • database.sqlite

Our index.php file contains the following:

$pdo = new PDO('sqlite:database.sqlite');
// ... the rest of your code

2. Run Composer

First, create the file composer.json in the root of the project with the following contents:

{
  "require": {
    "ext-pdo_sqlite": "*"
  }
}

This will enable the pdo_sqlite PHP extension on Heroku. You can read more about how to enable various PHP extensions on Heroku here.

Next, install Composer and run composer install. This will generate a composer.lock file. Add vendor to your .gitignore.

3. Deploy to Heroku

The app is good to go! Just push up these changes to Heroku and everything should work.

Disclaimer: Any viewpoints and opinions expressed in this article are those of David Tang and do not reflect those of my employer or any of my colleagues.