rseletsk's avatar

CLI App works, but the build generates runtime error with DB

CLI App Works, but the build generates runtime error with DB: Database file at path [phar:...buildname/database/database.sqlite

Note buildname is the executable build file and Not a directory name

0 likes
1 reply
LaryAI's avatar
Level 58

Certainly! Here is a solution to the problem described on the Laracasts forum.


Problem Recap:
You have a CLI app built with Laravel components (or similar PHP framework) that works while developing but fails with a runtime database error after building it into a PHAR/executable. The error mentions database file path [phar:...buildname/database/database.sqlite]. You noticed that buildname is an executable, not a directory.


Solution:

When you package your PHP app into a PHAR (a single-file executable), all files inside it ("phar://...") become part of a read-only archive. Your app is trying to write to (or reference) database/database.sqlite inside the PHAR, which is not possible.

You must ensure your app creates and writes the database outside the PHAR file.

How to Fix:

  1. Change the SQLite database path to a location outside the PHAR file at runtime.
    For example, use a temp or user directory.

    If you have something like:

    'database' => [
        'driver' => 'sqlite',
        'database' => __DIR__.'/database/database.sqlite',
        'prefix' => '',
    ]
    

    Change it to:

    'database' => [
        'driver' => 'sqlite',
        'database' => sys_get_temp_dir() . '/myapp-database.sqlite',
        'prefix' => '',
    ]
    

    Or, for a per-user DB:

    'database' => [
        'driver' => 'sqlite',
        'database' => getenv('HOME') . '/.myapp-database.sqlite',
        'prefix' => '',
    ]
    
  2. If your app needs a pre-populated database, extract the database/database.sqlite file from the PHAR to a writable location at runtime (only if it does not exist yet):

    $pharDbPath = 'phar://' . __FILE__ . '/database/database.sqlite';
    $userDbPath = getenv('HOME') . '/.myapp-database.sqlite';
    if (!file_exists($userDbPath)) {
        copy($pharDbPath, $userDbPath);
    }
    

    Then point your SQLite connection at $userDbPath.

  3. Do NOT attempt to write to or create files inside the PHAR archive (phar:// paths are strictly read-only).


Summary:
Always store/write your runtime SQLite database outside the PHAR archive. Only use the PHAR archive for reading default data or templates.


References:


If you need to detect if you’re running from a PHAR:

if (Phar::running()) {
    // Running from PHAR, adjust path
} else {
    // Local development, use normal path
}

Let me know if you need more help or a full code example!

Please or to participate in this conversation.