DEV Community

Damara Lucena
Damara Lucena

Posted on

Installing composer and configuring permissions for Laravel projects

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.
  • 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 and bootstrap/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

  1. Download the Composer Installer

Open your terminal (CMD or PowerShell) and run:

   php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Enter fullscreen mode Exit fullscreen mode

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.

  1. Run the Installer

Execute:

   php composer-setup.php
Enter fullscreen mode Exit fullscreen mode

This command should display a success message and create the composer.phar file.

  1. Remove the Installer

Clean up by running:

   php -r "unlink('composer-setup.php');"
Enter fullscreen mode Exit fullscreen mode
  1. Using Composer

To use Composer, run:

   php composer.phar
Enter fullscreen mode Exit fullscreen mode

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" %*
Enter fullscreen mode Exit fullscreen mode

2.2 Installation Using the Windows Installer

  1. Download the Installer

Visit the official Composer website and click on Composer-Setup.exe to download the installer.

  1. 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.
  1. Verify the Installation

Open a new terminal window and run:

   composer --version
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

or

  ;extension=php_fileinfo.dll
Enter fullscreen mode Exit fullscreen mode

Remove the semicolon (;) to enable it:

  extension=fileinfo
Enter fullscreen mode Exit fullscreen mode
  • zip: Look for:
  ;extension=zip
Enter fullscreen mode Exit fullscreen mode

Remove the semicolon:

  extension=zip
Enter fullscreen mode Exit fullscreen mode
  • openssl: Ensure the line:
  ;extension=openssl
Enter fullscreen mode Exit fullscreen mode

is uncommented:

  extension=openssl
Enter fullscreen mode Exit fullscreen mode
  • allow_url_fopen: Confirm it is set to:
  allow_url_fopen = On
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

5. Testing the Installation

  1. Check Composer Version:
   composer --version
Enter fullscreen mode Exit fullscreen mode
  1. Verify PHP Extensions:
   php -m | grep -E "fileinfo|zip|openssl"
Enter fullscreen mode Exit fullscreen mode
  1. Create the Laravel Project:
   composer create-project laravel/laravel project_name
Enter fullscreen mode Exit fullscreen mode
  1. Adjust Folder Permissions: If necessary, adjust the permissions of the storage and bootstrap/cache directories.

6. Final Considerations and Troubleshooting

  • Extension Errors:

    If you encounter errors indicating that an extension (e.g., fileinfo) is missing, recheck your php.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 containing composer.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)