How to fix Error: mkdir(): Permission denied when running composer

How to fix Error: mkdir(): Permission denied when running composer

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to fix Error: mkdir(): Permission denied when running composer

You are encountering a classic Unix file system issue where the process attempting to write data lacks the necessary execute or write bits on the target directory. The error mkdir(): Permission denied occurs immediately after composer create-project because Composer attempts to generate a new directory structure for your Laravel installation. Even if you can manually navigate to the location as ec2-user and create files, the specific user context in which the PHP process runs might differ from your shell's effective user.

On Amazon EC2 instances, this is frequently related to how the default home directory permissions are set up versus how the PHP-FPM or CLI processes execute. When you run a command like composer create-project laravel/laravel mydir, Composer tries to write into /home/ec2-user or a subdirectory depending on your configuration. If the directory exists but is owned by root, or if the permissions are set to 700 (read-execute-only) instead of 755 (read-execute for group and others), the process fails with this specific error.

The most direct fix involves ensuring the target directory is owned by your user and has appropriate permissions. You can resolve this using standard filesystem commands:

sudo chown -R ec2-user:ec2-user /home/ec2-user
sudo chmod -R 755 /home/ec2-user

Alternatively, if you are running Composer in a specific working directory that isn't your home folder, ensure that directory is writable by the current user. If you simply want to create the project in a location where you already have full control, specify an absolute path that you own:

composer create-project laravel/laravel /home/ec2-user/mydir

This bypasses any ambiguity regarding home directory permissions entirely.

However, relying on sudo commands for every Composer operation is not a sustainable long-term strategy. It introduces security risks and can lead to permission conflicts when you later run migrations or serve the application via PHP-FPM, which often runs as a different system user like www-data. If your web server process cannot write to storage or bootstrap/cache, you will encounter similar permission errors later in the development lifecycle.

A more robust approach involves modifying your shell configuration to avoid root execution. You can create a symbolic link from your home directory to a location where you have full administrative control, such as /var/www/mydir. This allows you to run Composer without elevated privileges for the creation step while keeping the web server's file structure separate.

mkdir -p /var/www
sudo chown ec2-user:ec2-user /var/www
cd /var/www
composer create-project laravel/laravel mydir

In this scenario, /var/www is owned by ec2-user, but the directory permissions remain open enough for the web server to function correctly after you adjust the internal Laravel directories. This separation of concerns—keeping home directories for development tools and /var/www for the actual application—is a best practice in production environments.

If your EC2 instance is part of an Auto Scaling Group, be aware that these permission fixes must be applied via a UserData script or a configuration management tool like Ansible or Chef. Manually running chmod or chown on a spot instance will result in the permissions being reset to default upon termination and relaunch. Always verify your user context by checking /proc/self/status for the UID/GID of the current process if issues persist after permission changes.

For production deployments, consider installing Composer globally within the application's directory rather than using the global system installation. This ensures that dependencies are isolated and reduces the likelihood of version conflicts or permission mismatches across different environments.

Our network