This guide details how to install Composer and configure the necessary PHP extensions and permissions to run a Laravel project.
1. Prerequisites
Before starting, make sure you have:
- PHP: A compatible version (PHP 8.0 or higher is recommended).
-
Required PHP Extensions:
-
fileinfo: Needed for file handling (used by packages such as
league/flysystem
). - zip: For extracting compressed packages.
- openssl: For secure HTTPS communication.
-
fileinfo: Needed for file handling (used by packages such as
- Internet Access: To download packages and dependencies via Composer.
-
Write Permissions: The user executing the commands must have read and write permissions in the project directories (e.g., the
storage
andbootstrap/cache
directories in Laravel).
2. Installing Composer
You can install Composer in two ways: manually via the terminal or by using the Windows installer.
2.1 Manual Installation via Terminal
- Download the Composer Installer
Open your terminal (CMD or PowerShell) and run:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Note: If you encounter HTTPS-related errors, ensure that the openssl
extension and the allow_url_fopen
option are enabled in your php.ini
file.
- Run the Installer
Execute:
php composer-setup.php
This command should display a success message and create the composer.phar
file.
- Remove the Installer
Clean up by running:
php -r "unlink('composer-setup.php');"
- Using Composer
To use Composer, run:
php composer.phar
Tip: For global usage, move composer.phar
to a directory that is in your PATH. You can also create a batch file (e.g., composer.bat
) with the following content:
@ECHO OFF
php "%~dp0composer.phar" %*
2.2 Installation Using the Windows Installer
- Download the Installer
Visit the official Composer website and click on Composer-Setup.exe to download the installer.
- Run the Installer
- Open the downloaded file.
- The installer will ask for the path to your PHP executable (e.g.,
C:\xampp\php\php.exe
or another PHP installation path). - Follow the on-screen instructions to install Composer globally. The installer will automatically add Composer to your system PATH.
- Verify the Installation
Open a new terminal window and run:
composer --version
If the command returns the installed version, Composer is ready to use.
3. Configuring PHP Extensions
For Laravel packages to install correctly, certain PHP extensions must be enabled.
3.1 Locating the php.ini
File
Run the following command to identify which configuration file PHP is using:
php --ini
Typically, the path might be something like C:\tools\php84\php.ini
.
3.2 Enabling the Extensions
Open the php.ini
file in your preferred text editor and check or update the following items:
- fileinfo: Look for a line such as:
;extension=fileinfo
or
;extension=php_fileinfo.dll
Remove the semicolon (;
) to enable it:
extension=fileinfo
- zip: Look for:
;extension=zip
Remove the semicolon:
extension=zip
- openssl: Ensure the line:
;extension=openssl
is uncommented:
extension=openssl
- allow_url_fopen: Confirm it is set to:
allow_url_fopen = On
After saving your changes, restart your terminal to apply the new settings.
3.3 Verifying the Extensions
To confirm the extensions are active, run:
php -m
Check that fileinfo, zip, and openssl appear in the output.
4. Creating and Configuring a Laravel Project
Once Composer is installed and PHP extensions are configured, you can create a new Laravel project.
4.1 Creating the Project
Navigate to the directory where you want to create your project (e.g., C:\www
) and run:
composer create-project laravel/laravel project_name
This command downloads the necessary packages and sets up the Laravel project structure.
4.2 Setting Folder Permissions in the Laravel Project
After project creation, ensure that the following directories have the correct permissions:
storage:
This folder stores logs, caches, and generated files. The web server (or the user running PHP) needs write permissions in this directory.bootstrap/cache:
This directory stores cached configuration and routes. It should also be writable.
Setting Permissions on Windows
- Ensure the user running the server (e.g., the IIS user or the CLI PHP user) has write permissions for these directories.
- If using XAMPP or WAMP, right-click the folder, select Properties, and adjust the permissions under the Security tab.
Setting Permissions on Linux
If you are using Linux, run the following commands (adjust the user if needed):
sudo chown -R www-data:www-data /path/to/project/storage
sudo chown -R www-data:www-data /path/to/project/bootstrap/cache
sudo chmod -R 775 /path/to/project/storage
sudo chmod -R 775 /path/to/project/bootstrap/cache
5. Testing the Installation
- Check Composer Version:
composer --version
- Verify PHP Extensions:
php -m | grep -E "fileinfo|zip|openssl"
- Create the Laravel Project:
composer create-project laravel/laravel project_name
-
Adjust Folder Permissions:
If necessary, adjust the permissions of the
storage
andbootstrap/cache
directories.
6. Final Considerations and Troubleshooting
Extension Errors:
If you encounter errors indicating that an extension (e.g.,fileinfo
) is missing, recheck yourphp.ini
file to ensure the extension is uncommented.Permission Issues:
If the server is unable to write to certain directories, review and adjust the directory permissions accordingly.Global Composer Usage:
For convenience, consider installing Composer globally using the Windows installer or by adding the directory containingcomposer.phar
to your system PATH.
By following these steps, your environment will be correctly configured to develop and run Laravel projects using Composer.
Top comments (0)