46

I have run my Laravel application with PHPUnit.

Everything is fine until at some point, when I run my test again, it turns out with this error.

Illuminate\Database\QueryException: could not find driver (SQL: PRAGMA foreign_keys = ON;)

Enter image description here

Caused by PDOException: could not find driver

Here's my phpunit.xml file:

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
     bootstrap="vendor/autoload.php"
     colors="true">

    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="sqlite"/>
        <server name="DB_DATABASE" value=":memory:"/>
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>
  • OS: Windows 10
  • PHP version: 7.4.11
  • SQLite version: 3.19.1

How can I fix this error in Laravel 6 and PHPUnit?

4
  • 1
    can you please post more information? Like your phpunit.xml file. Are you using sqlite? Commented Oct 16, 2020 at 1:10
  • Yes I'm using sqlite and database value as memory. Please refer my updated post @JonathanMartins Commented Oct 16, 2020 at 1:34
  • Whats you OS? Whats the PHP version? do you have the sqlite driver installed? Commented Oct 16, 2020 at 1:42
  • Yup I have sqlite installed. Updated my post as per your questions. Commented Oct 16, 2020 at 1:56

14 Answers 14

100

If you face this problem,

(could not find driver (SQL: PRAGMA foreign_keys = ON;))

You can simply run the below command on your Ubuntu system:

sudo apt-get install php-sqlite3

Also, if you want to install a specific version, like PHP 8.1, simply run the below command:

sudo apt install php8.1-sqlite3

You might need to enable the pdo_sqlite extension in your php.ini file too. You can get the path of your loaded ini with the below command:

php --ini

Simply add extension=pdo_sqlite to your php.ini file, and verify it is loaded by running:

php -m | grep pdo_sqlite
Sign up to request clarification or add additional context in comments.

2 Comments

For php8.1 the package is php8.1-sqlite3.
Editing php.ini solved my issue
33

This is actually happening after I upgraded my PHP version in Laragon. The solution is to enable pdo_sqlite in the PHP extension in Laragon.

That's it. This answer was found on PHPUnit SQLite error after updating Laragon (PDOException: could not find driver).

1 Comment

It worked. Do not forget to stop and start all services
10

PHP 8.1

sudo apt install php8.1-sqlite3

Note that it needs the 8.1 part!

Comments

8

For those using Windows;

Navigate to the php.ini file (C:\php\php.ini). Enable the following:

;extension=pdo_sqlite by removing the /;/ should look like this extension=pdo_sqlite

;extension=sqlite3 should be extension=sqlite3 without the ; symbol

1 Comment

Path to that ini file isn't necessarily always that C:\php\php.ini and can depend on your installation. You can run php --ini command in your terminal to get path to ini file for your current active PHP installation.
6

If you come across such an error, simply do the following;

  1. run this command to install an SQLite driver in your Ubuntu or Linux OS

    sudo apt-get install php-sqlite3
    
  2. You also need to enable pdo_sqlite extension in your php.ini too, check the path of your loaded .ini file with the below command

    php --ini
    
  3. Then check and verify that the pdo_sqlite extension is loaded.

    php -m | grep pdo_sqlite
    

Then the error will be solved.

Comments

5

This happens because PDO SQLite is not enabled. To enable it in Laragon, just open Laragon, and click on menu PHP → Extensions → pdo_sqlite . That's it.

And if you don't use Laragon, the solution might be different.

Comments

4

Just enabling these two extensions from the php.ini file worked for me.

extension=pdo_sqlite

extension=sqlite3

Comments

3

If you use Ubuntu try this:

php --version
sudo apt-get install PHP (your php version)-sqlite3

This works for me.

https://laracasts.com/discuss/channels/laravel/sqlite-database-throw-error-could-not-find-driver-sql-pragma-foreign-keys-on

1 Comment

It's very important to check if your version of PHP CLI first, php.ini might be load a different version so.. you should match phpx.y-sqlite3 packege
3

Just do the following changes in the php.ini file:

extension_dir = "<php installation directory>/php-7.4.3/ext"
extension=php_pdo_sqlite.dll
extension=php_sqlite3.dll
sqlite3.extension_dir = "<php installation directory>/php-7.4.3/ext"

Comments

3

Another suggestion: If you see this error after installing Laravel 11, and you're not using SQLite, you must know that Laravel 11 by default is using SQLite to manage sessions. To fix this, just change the environment variable like this:

...
SESSION_DRIVER=database
SESSION_LIFETIME=120
SESSION_ENCRYPT=false
SESSION_PATH=/
SESSION_DOMAIN=null
...

to

...
SESSION_DRIVER=cookie
SESSION_LIFETIME=120
SESSION_ENCRYPT=false
SESSION_PATH=/
SESSION_DOMAIN=null
...

Comments

1

In case if you have multiple versions of PHP in Laragon you will still see the error. The solution is to enable pdo_sqlite extension on all versions. The reason is that Laragon has its own PHP but Laravel uses the system PHP and you might not know which one is system PHP version.

Comments

1

You need to install the sqlite3 package in a Linux distribution:

sudo apt-get install php$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')-sqlite3  php-sqlite3

This command automatically installs the sqlite3 package based on your current PHP version in your Linux distribution.

Comments

0

At first uncomment ;extension=pdo_sqlite in php.ini in /etc/php/{your_php_version}/php.ini on Linux or C:\php\php.ini on Windows by removing the semicolon at the beginning.

then sudo service apache2 restart

then sudo apt-get install php-sqlite3

Comments

0

I've have the same issue in a shared hosting subdomain with SQLite enabled, and my solution has been modify this line in config/database.php:

// 'database' => env('DB_DATABASE', database_path('database.sqlite')),
'database' => database_path('database.sqlite'),

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.