264

I know that this issue has been posted many times, but for me it seems to be a different problem.

Indeed, this error

Warning: require(vendor/autoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\site_web\send_mail.php on line 3

Fatal error: require(): Failed opening required 'vendor/autoload.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\site_web\send_mail.php on line 3

appears at the begining of my code from this line:

require 'vendor/autoload.php';

So, I guess there must be a /vendor/autoload.php file somewhere in my computer (I have installed composer and ran composer require phpmailer/phpmailer).

So, I looked for this file using: dir /s autoload.php in the Windows command line, and found one here: C:\Windows\SysWOW64\vendor\autoload.php,

but for me, syswow64 folder has nothing to see with autoload.php, I don't see what I am missing here.

Synchro
  • 35,538
  • 15
  • 81
  • 104
adrTuIPKJ44
  • 2,793
  • 2
  • 9
  • 8
  • 1
    This can also occur if you have php artisan up or down in your composer.json file in the scripts section on pre-install. It seems to need files in the vendor folder to execute the maintenance mode, which isn't available as yet. – Thomas Mar 02 '18 at 10:18
  • 1
    `PHP Warning: require_once(../vendor/autoload.php): Failed to open stream: No such file or directory`. Two potential causes: (1) wrong path, e.g. to PHP webserver root DIR (troubleshoot by including full path, to debug); (2) you have an improperly formatted `.env` file, e.g. `servername="localhost";` (note the semicolon `;` at the end of the line - remove it, as lines in `.env` files do not end in semicolons / punctuation ... – Victoria Stuart Jun 08 '22 at 21:10

19 Answers19

432

What you're missing is running composer install, which will import your packages and create the vendor folder, along with the autoload script.

Make sure your relative path is correct. For example the example scripts in PHPMailer are in examples/, below the project root, so the correct relative path to load the composer autoloader from there would be ../vendor/autoload.php.

The autoload.php you found in C:\Windows\SysWOW64\vendor\autoload.php is probably a global composer installation – where you'll usually put things like phpcs, phpunit, phpmd etc.

composer update is not the same thing, and probably not what you want to use. If your code is tested with your current package versions then running update may cause breakages which may require further work and testing, so don't run update unless you have a specific reason to and understand exactly what it means. To clarify further – you should probably only ever run composer update locally, never on your server as it is reasonably likely to break apps in production.

I often see complaints that people can't use composer because they can't run it on their server (e.g. because it's shared and they have no shell access). In that case, you can still use composer: run it locally (an environment that has no such restrictions), and upload the local vendor folder it generates along with all your other PHP scripts.

Running composer update also performs a composer install, and if you do not currently have a vendor folder (normal if you have a fresh checkout of a project), then it will create one, and also overwrite any composer.lock file you already have, updating package versions tagged in it, and this is what is potentially dangerous.

Similarly, if you do not currently have a composer.lock file (e.g. if it was not committed to the project), then composer install also effectively performs a composer update. It's thus vital to understand the difference between the two as they are definitely not interchangeable.

It is also possible to update a single package by naming it, for example:

composer update ramsey/uuid

This will re-resolve the version specified in your composer.json and install it in your vendor folder, and update your composer.lock file to match. This is far less likely to cause problems than a general composer update if you just need a specific update to one package.

It is normal for libraries to not include a composer.lock file of their own; it's up to apps to fix versions, not the libraries they use. As a result, library developers are expected to maintain compatibility with a wider range of host environments than app developers need to. For example, a library might be compatible with Laravel 5, 6, 7, and 8, but an app using it might require Laravel 8 for other reasons.

Composer 2.0 removed any remaining inconsistencies between install and update results; if you're running composer 1.x you should definitely upgrade.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • 4
    How do I run composer install? It's not recognised as a command – Yvonne Aburrow Mar 16 '17 at 09:41
  • thanks, found that, and have downloaded it - but it asks me where PHP is installed locally. I don't really want to install PHP locally (or Composer) I just want to run it on my Google Cloud app. – Yvonne Aburrow Mar 16 '17 at 10:52
  • I am following the steps in this bit of documentation here: https://github.com/googlecloudplatform/google-cloud-php – Yvonne Aburrow Mar 16 '17 at 10:55
  • 1
    Google cloud for PHP provides composer to install its dependencies, you just need to set it up, see here: https://cloud.google.com/appengine/docs/flexible/php/using-php-libraries – Synchro Mar 16 '17 at 11:04
  • Thanks. I've downloaded this from Github, which contains `composer.json`: github.com/googlecloudplatform/google-cloud-php and reuploaded it to my app, together with my settings.yml file, but I am getting an error saying that it can't find vendor/autoload.php, which suggests either that the path to it is incorrect (which I don't think it is), or that composer is not installed properly in my app (on Google Cloud). – Yvonne Aburrow Mar 16 '17 at 11:14
  • 2
    Yes, google cloud will need to run `composer install` and it will then read the contents of your `composer.json` file and install all your dependencies into the `vendor` folder. If it's not doing that, I can only suggest reading the docs I pointed at and double-checking it all. This is really the distinction between "upload" and "deployment". – Synchro Mar 16 '17 at 11:23
  • To clarify Synchro's point further--if possible, the best thing to do is install Composer on your Web server. That way, `composer install` can be run on the server. If you don't or can't do that, you basically will have to do most development/production work on your local machine, then move all files up to the server. This is what is being referenced as an "upload." – karolus Sep 10 '17 at 16:38
  • What is the correct syntax to find this ??? ` require 'C:\Users\AXM\.vscode\extensions\felixfbecker.php-intellisense-2.3.14\vendor\autoload.php'; ` – Meryan Jun 11 '20 at 10:27
  • No, that's the path to an autoload file belonging to a plugin for your IDE. Your own project's autoload file will usually be generated in `vendor/autoload.php` relative to your project root folder. – Synchro Jun 11 '20 at 10:38
  • @IMSoP I get that you wanted to say something different in your edit, but your edit was inappropriate. What you said was true, but what you removed was also true, to the detriment of the answer. – Synchro Sep 16 '20 at 13:04
  • I've clarified the two situations separately, incorporating what you suggested. I know what's "normal", I just wanted to be clear what happens if you *don't* do those things. That also led me to add the note about libraries usually not having lock files of their own (this isn't npm!). – Synchro Sep 16 '20 at 13:20
  • 1
    @Synchro Yep, that edit makes much more sense - it no longer suggests that the lack of vendor directory will itself change the behaviour, which is the perceived problem I was trying to correct in my edit. The note about libraries is also a good one, as I've seen this cause some confusion, because composer doesn't make a strong distinction between libraries and the projects using them. :) – IMSoP Sep 16 '20 at 13:24
  • 1
    Right. Something I often see when people use PHPMailer is that they download the tarball, then make edits to PHPMailer's own composer.json file, not realising that they shouldn't touch that and should make their own instead. Recipe for disaster! – Synchro Sep 16 '20 at 13:27
  • it's simple as that – MOHAMMED NABAWY Jun 02 '21 at 02:28
  • Yeah thanks, for me it was just an issue of referring to the wrong directory. I backed out of the current directory using ../ then I was able to call the file. – MosesK Sep 17 '21 at 11:59
  • It is true . excellent – amrmrp Oct 13 '21 at 08:15
77

If you get the error also when you run

composer install

Just run this command first

composer dump-autoload

This command will clean up all compiled files and their paths.

mbouzahir
  • 1,424
  • 13
  • 16
29

@Bashir almost helped me but I needed:

composer update --no-scripts

Apparently this prevents any scripts from being included before executing artisan.

I found the answer here half-way down the page: https://laracasts.com/discuss/channels/general-discussion/fatal-error-class-illuminatefoundationapplication-not-found-in-pathtoprojectbootstrapappphp-on-line-14?page=0

mcmacerson
  • 885
  • 2
  • 14
  • 17
  • 1
    Don't do this unless you understand exactly what it's doing; it's fairly likely to break an app in production. – Synchro May 26 '20 at 09:40
  • Please add some explanation to your answer such that others can learn from it – Nico Haase Mar 11 '21 at 17:02
  • 1
    @NicoHaase I stumbled upon this solution at https://laracasts.com/ I was desperately searching for a way to get my composer update to work. About half way down the page containing all kinds of ways to fix the problem was a humble little post suggesting the use of --no-scripts. I have no idea why this works but it does! – mcmacerson Mar 11 '21 at 22:58
14

Proper autoload.php configuration:

A) Quick answer:

Your autoload.php path is wrong. ie. C:\Windows\SysWOW64\vendor\autoload.php To date: you need to change it to: C:\Users\<Windows User Name>\vendor\autoload.php


B) Steps with example: We will take facebook/php-graph-sdk as an example; change to Package Name as needed.

  1. Install composer.exe
  2. Open CMD Prompt. + R + type CMD
  3. Run This command: composer require facebook/graph-sdk
  4. Include path in your PHP page: require_once 'C:\Users\<Windows User Name>\vendor\autoload.php';
  5. Define configuration Secrets and Access Token for your package...etc.
  6. Happy codinig.

C) Further details:

Installing composer on windows will set this default path for your pacakges; you can find them there and include the autoloader path:

C:\Users\<Windows User Name>\vendor

For the same question you asked; the answer was this path for WAMP Server 64 BIT for Windows.

Then simply in your PHP Application change this:

require_once __DIR__ . '/vendor/autoload.php'; 

To:

require_once 'C:\Users\<Windows User Name>\vendor\autoload.php'; 

Find your windows username under C:\Users\

Before all this, as pointed before in B) , you need to run this command:

composer require <package name>

for facebook php SDK for example:

composer require facebook/graph-sdk

Thank you for asking this question; appreciated as it helped me fix similar issue and ended writing this simple tutorial.

wpcoder
  • 1,016
  • 11
  • 17
  • 3
    In general, Composer is designed to work with packages installed _locally to a particular project_, so directly including an autoload.php from a system or user profile directory would be _extremely_ unusual. – IMSoP Sep 16 '20 at 11:33
13

First make sure you have installed the composer.

composer install

If you already have installed then update the composer.

composer update
Tadas V.
  • 775
  • 1
  • 11
  • 22
IsharaNW
  • 249
  • 2
  • 3
  • 4
    This answer needs serious editing to be helpful to a user. Please clearly state your suggestions and provide code samples that would be useful in this situation. – Stefan Crain Apr 26 '18 at 14:48
  • Please add some more explanation to your answer. In which case should `composer update` resolve any problem? – Nico Haase Mar 11 '21 at 17:01
9

First, review route inside index.php

require __DIR__.'/../vendor/autoload.php';

$app = require_once __DIR__.'/../bootstrap/app.php';

in my case the route did not work, I had to review the directories.

9

If you have cloned your project from Github or got it from somewhere else, you will encounter this error. That's because you are missing the vendor folder and other files. The vendor folder contains packages which are dependent to your project. The package dependencies are stored in composer.json file and the folder was excluded while pushing to Github.

Fix this error by simply running:

composer install

Then you will get all the assets needed for your project.

bart
  • 14,958
  • 21
  • 75
  • 105
Anand Mainali
  • 993
  • 1
  • 14
  • 23
6

For me Just run this command first

composer dump-autoload

to add vendor folder.

then run this command

composer update --no-scripts

to update composer.

4

Create composer.json file with requisite library for ex:

{
    "require": {
        "mpdf/mpdf": "^6.1"
    }
}

Execute the below command where composer.json exists:

composer install

In case of Drupal :

Use the web root folder of drupal to include autoload for ex:

define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/vendor/autoload.php';

In case of other systems: Use the root folder variable or location to include the autoload.php

3

run composer update. That's it

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
Bashir ahmad
  • 241
  • 4
  • 12
  • 11
    Yeah, thats already part of the accepted answer. Whats the point in duplicating it? – Nico Haase Feb 19 '18 at 09:03
  • 3
    It's also bad advice, likely to break an app in production. – Synchro May 26 '20 at 09:38
  • 1
    Please add some explanation to your answer such that others can learn from it – Nico Haase Mar 11 '21 at 17:00
  • Extracted from composer help: `The update command reads the composer.json file from the current directory, processes it, and updates, removes or installs all the dependencies.` So. if you do that, no matter where you do it (test environment, locally, production environment), all of you're project dependencies may be subject to change or deletion. So this is a bad solution and without explanation. Downvoted. – iDon'tKnoware Aug 17 '21 at 15:49
2

I had this path in my machine:

C:/xampp5.0/htdocs/project-recordando-symfony/project-recordando-symfony

Then I ran composer install or/and composer update and it returned this error:

ErrorException ZipArchive::extractTo...

That error is because your path is too much long, I changed to:

C:/xampp5.0/htdocs/p-symfony/*

and worked!

jjoselon
  • 2,641
  • 4
  • 23
  • 37
  • This seems to be the answer to a different question, since you had a different error. You can [ask and answer your own question](https://stackoverflow.com/help/self-answer) so that this can be found more easily by people seeing the same error. – IMSoP Sep 16 '20 at 11:39
  • Please share more details. How is this answer related to the given question? – Nico Haase Mar 11 '21 at 17:03
1

I was able to resolve by removing composer and reinstalling the proper way. Here is what I did:

I was then able to get composer install to work again. Found my answer at the bottom of this issue: https://github.com/composer/composer/issues/5510

nwolybug
  • 462
  • 5
  • 12
  • 1
    This seems to be the answer to a different question - the question on this page doesn't mention any errors during `composer install`, rather the user just hadn't run the command yet. – IMSoP Sep 16 '20 at 11:41
  • I received the same error message and it was due to an improper composer installation. Once I installed composer with my answer above, I no longer received the same error as the OP. It is a possible resolution to the original question. – nwolybug Sep 17 '20 at 15:16
  • Just reinstalling composer itself wouldn't solve an error in your PHP code, since it's not used directly when the PHP code runs. One you've installed it, you have to actually run it to install and generate the relevant files. So presumably you also had a different error when trying to run the command, which is different from what's described in this question. – IMSoP Sep 17 '20 at 15:33
  • The given problem shows paths from Windows. You should not use `apt` on Windows – Nico Haase Mar 11 '21 at 17:02
1

In your project folder, the vendor folder is missing so you got this error:

Warning: require(vendor/autoload.php): failed to open stream: No such file or directory in

When you download the project through git, the project is downloaded without the vendor folder

You need /vendor because all your packages are there, including all the classes Laravel uses. The vendor directory contains your Composer dependencies.

The solution is simple, Just run this command:

composer update --no-scripts 
composer update
  • composer update --no-scripts It will Skips execution of scripts defined in composer.json file.
  • composer update It will update your depencencies as they are specified in composer.json file.

With this command, you will re-create the vendor folder in your project and after that your project will start working normally.

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
  • You haven't explained _why_ you would want to skip executing scripts, and should really mention that the most common command will be `composer install` if the project has a correctly committed `composer.lock` file. – IMSoP Sep 16 '20 at 11:40
  • Running `composer update` should not be done in case you freshly checked out an existing project – Nico Haase Mar 11 '21 at 17:00
1

This error occurs because of missing some files and the main reason is "Composer"

enter image description here

First Run these commands in CMD

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Then Create a New project
Example:

D:/Laravel_Projects/New_Project
laravel new New_Project

After that start the server using

php artisan serve
hexhad
  • 1,139
  • 12
  • 14
1

In my case, It was due to the non-fully installation of the project, because I didn't have enough space on my hard disk

  • Sort of a very specific problem situation, however it's always worth watching for out-of-the-ordinary hints outside the project itself: A file is missing. Why? Was it created? Why not? – Sven Nov 28 '22 at 13:11
1

You're just missing the composer install on your project.

After you created your laravel project, open the terminal and type:

composer install
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
0

There will be a directory called "vendor" that needs to be in your root directory if you have a cloned repository and trying to set up that time this type of error occurring.

".gitingore" file has written code to not include vendor directory on GIT so after cloning GIT your project facing the issue of missing vendor directory.

Once you add vendor directory your project will start working again.

Ajay Gadhavana
  • 415
  • 5
  • 5
-4

Change the auto_prepend_file property on php.ini

; Automatically add files before PHP document. 
;http://php.net/auto-prepend-file 
auto_prepend_file =
ercvs
  • 337
  • 1
  • 5
  • 13
  • How does this relate to the question? There is an accepted answer with a completely other solution – Nico Haase Feb 19 '18 at 08:03
  • I had the following error in this morning. "Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0" Therefore I writed this solution. – ercvs Feb 19 '18 at 08:48
  • 1
    This seems to be an answer to a completely different question. You can [ask and answer your own question](https://stackoverflow.com/help/self-answer) if you want to document something so that other people with the same error can find it. – IMSoP Sep 16 '20 at 11:43
-4

In linux first add github Personal access tokens

  1. Navigate to GitHub's Personal Access Tokens page.
  2. Hit "Generate new token" button.
  3. Type something meaningful "Note", select "repo" as scope and hit "Generate token" button.
  4. Take a note of the token.

5 type in terminal with you new "personal access token"

export COMPOSER_AUTH='{"github-oauth":{"github.com":"AB8cd4cab23a9d5399934a7d7698d3fa74e9cfAB"}}'

Run in terminal composer install

Beowulfdgo
  • 141
  • 1
  • 1
  • 9
  • Please share more details. Why should that be needed? Using Composer since years, I've never needed such a token – Nico Haase Mar 11 '21 at 16:59