Optimize your Laravel files BEFORE production
I've previously mentioned how you can upload your app to production:
Laravel to production/deployment using Apache and MySQL
I wanted to add a little additional information related to the vendor folder, as it's key to understand how it works, especially when we go into production. We can compare it to what happens in the Node.js ecosystem with the node_modules folder.
When we create a project in Laravel, it's common to also have a node_modules folder, especially if we work with tools like Vue, React, Tailwind, etc. This folder contains packages needed only during development, and we then run npm run build to generate files optimized for production.
A similar thing happens with the vendor folder. Although you can upload it as is to your production server, it's best to optimize it beforehand, as it also contains dependencies that are only used during development.
Recommended command to optimize vendor:
composer install --no-dev --optimize-autoloader
What does each part do?
- install Installs dependencies.
- --no-dev: Avoid installing dependencies marked as development in composer.json.
- --optimize-autoloader: Generates a more efficient autoloader to improve performance.
This is equivalent, in simple terms, to having a “minified” version of your PHP dependencies for production.
Clearing cache (very important)
In production, it's also recommended to clear the cache, as it could be causing errors that don't appear locally.
You can do this with:
php artisan config:clear php artisan route:clear php artisan view:clear php artisan cache:clear
If you don't have access to a terminal (such as on shared hosting), you can do this manually from an FTP client. For example, you can:
- Rename the bootstrap/cache folder
- Create a new, empty folder called cache
Upload the project and verify that everything works.
If everything checks out after a few days, you can delete the old folder.
Practical recommendations
Before running the Composer command: Move or rename the current vendor folder, in case you need it later, as it's the folder we use when developing.
Do not upload the vendor or node_modules folders to the Git repository. This is important if, based on the previous steps, you rename the vendor folder and keep it in the project; it will be tracked by Git.
If you're uploading the project via FTP: you can also compress it with a ZIP file (including the new optimized vendor folder), upload it, and unzip it on the server.
Final tip
I apply these recommendations myself, for example, when I am going to update the files in the public/build folder. I do not delete it immediately, but rather rename it (for example, __build) and keep that backup for a few days until you are sure that everything works correctly.
I agree to receive announcements of interest about this Blog.
We will see the command that we must execute when uploading your app to Laravel production
- Andrés Cruz