Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fsdolphin's avatar

Connect to an Sqlite DB - Laravel documentation procedure doesn't work

As I understand the database connection starts as follow...

In file database.php, Laravel will check if a variable called DB_CONNECTION exists in the .env file, if it does it will use the values from the .env file, if NOT it will use the values in the database.php, correct? Simple enough.

If the above statement is true, can you please confirm that my steps below are correct?

So, in theory and according to the documentation if we want to use Sqlite in a new/fresh Laravel project this is what we would do...

  1. Create new project

  2. Create the sqlite database file (database/database.sqlite)

  3. Go to the .env file and change the DB_CONNECTION and the DB_DATABASE values as follow...

        DB_CONNECTION=sqlite   
        DB_HOST=127.0.0.1   
        DB_PORT=3306   
        DB_DATABASE=database/database.sqlite   
        DB_USERNAME=homestead   
        DB_PASSWORD=secret  
    
  4. Migrate database php artisan migrate

  5. Done.

In theory at this point we should have a working DB, correct?

... But not on my case, I get Database (database/database.sqlite) does not exist. error.

Documentation REF : https://laravel.com/docs/5.2/database

Let me clarify that I'm able to connect to the database but NOT using the method described above, what I did is deleted the DB_CONNECTION and the DB_DATABASE variables from the .env file and specified sqlite in the database.php file ('default' => env('DB_CONNECTION', 'sqlite'),) and it worked but the fact that what is recommended in the docs doesn't work is bothering me.

BTW - I'm on a Mac and used php artisan serve to view my pages, I'm not sure if that matters.

0 likes
9 replies
zachleigh's avatar

From the docs:

DB_DATABASE=/absolute/path/to/database.sqlite

You need to put the full path to the database file, not just 'database/database.sqlite'.

2 likes
fsdolphin's avatar

Excuse my ignorance but what would the absolute path be in this case?

 /app/database/database.sqlite
zachleigh's avatar
Level 47

No. That would be a relative path. Your absolute path would be something like /home/username/projects/app/database/database.sqlite. You need the full path to the file.

3 likes
gator's avatar

In the .env file, delete ALL the 'DB_' entries after DB_CONNECTION (This is important!) Next,

touch database/database.sqlite

this is the default mysqlite database path that laravel looks in. Done.

3 likes
MadMikeyB's avatar

@gator beat me to it. Just delete the none DB_CONNECTION .env variables for sqlite.

I wrote a pull to try to add a DB_PATH env variable for sqlite only but it wasn't included. :)

1 like
fsdolphin's avatar

@zachleigh thanks a lot for the clarification.

@gator / @MadMikeyB Thanks a lot for pointing that out, it makes it easier as long as your database is located in the default location (database/database.sqlite).

Thank you all!

vegaguinaga's avatar

In my case using homestead DB_DATABASE=/home/vagrant/Code/laravel/storage/database.sqlite

hfally's avatar

Hello, the mistake been made is not being able to differentiate the features of using homestead and php artisan serve to run our app.

When using homestead (vagrant up/vagrant reload --provision): DB_DATABASE=/home/vagrant/Code/your_project_folder_name/database/database.sqlite

While when using php artisan serve: DB_DATABASE=/Users/your_user_folder/parent_folder_if_there_is_one/your_project_folder_name/database/database.sqlite

Conclusion is php artisan serve works directory with your physical directory tree, since it is not on a virtual environment so to speak, but vagrant is purely a virtual environment, therefore it reads the directory in a somewhat different manner.

Briareos's avatar

I just made little fix in /config/database.php - 'database' => database_path(env('DB_DATABASE','database.sqlite'))

And DB_DATABASE=dbname.sqlite in .env

I really do not understand, why do not use this by default.

Please or to participate in this conversation.