Search code examples
phpsql-serverlaravellaravel-10laravel-sail

"Could not find driver" error with SQL Server on WSL 2 with Ubuntu 24.04


I'm encountering an issue while trying to connect my Laravel application to a SQL Server database. I'm running this setup on WSL 2 with Ubuntu 24.04, PHP 8.3.11, and Laravel Sail. Here’s a detailed overview of the problem and what I've done so far:

  • Installed Microsoft ODBC Driver for Ubuntu: I followed the official instructions to install the ODBC driver for SQL Server.

  • Installed the sqlsrv and pdo_sqlsrv Extensions: I installed these extensions using the official Microsoft documentation for PHP with FPM.

  • Reviewed Laravel's config/database.php: I ensured the configurations for the SQL Server connection were correct and matched the expected parameters.

  • Updated the .env File: I modified the .env file to specify the SQL Server connection parameters.

Despite all the above steps, when I try to run my Laravel application, I receive the could not find driver error message when trying to log in.

Additional Information

  • I can successfully connect to the SQL Server database using DBeaver, which indicates that the database is accessible and allows remote connections.
  • I have verified that the extensions sqlsrv and pdo_sqlsrv are installed correctly, and I checked that they are listed when running php -m. Each one of the extensions has it's own .ini file, but I've also added the lines extension=pdo_sqlsrv.so and extension=sqlsrv.so into my php.ini file to ensure that the extensions would be automatically loaded anyway.

So, with that all being said, what could be causing Laravel to not recognize the pdo_sqlsrv driver despite it being installed? Are there any additional configurations or troubleshooting steps I can take to resolve this issue?


Solution

  • Solution:

    I was able to resolve the issue by installing the drivers directly inside the container where my application is running. Initially, I was trying to install the drivers in WSL 2 on my host machine, but since the application runs inside a container, the drivers need to be installed within that container’s environment.

    Here’s a quick summary of the fix:

    • Access the container shell.
    • Install the drivers using the same steps from Microsoft’s official documentation.
    • Restart the container to ensure everything loads correctly.

    After doing this, the error no longer occurs.