sudo composer install vs. composer install

sudo composer install vs. composer install

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

Stefan Bogdanescu

Founder & Senior Architect

The Hidden Cost of sudo composer install in Laravel

You are facing a classic Linux permission trap that plagues many PHP developers: trying to run Composer as root, then hitting a wall when your application scripts try to write files as the standard user.

The warning you received is explicit: Do not run Composer as root/super user. The official documentation provides a detailed explanation of why this is dangerous, primarily because it can lead to permission issues on the system-wide Composer cache and global bin directory. However, your specific error—file_put_contents(.../vendor/composer/installed.json): failed to open stream: Permission denied—reveals that simply dropping sudo isn't enough if your project's directory structure doesn't align with your current user permissions.

Why sudo composer install Fails Gracefully

When you execute sudo composer install, you are running the process as root. Composer downloads packages and writes metadata to vendor/composer/installed.json. Since root owns everything, this step succeeds. However, the real issue arises immediately after installation when your application runs.

Laravel’s post-install scripts, defined in your composer.json under the "scripts" key, often trigger commands like php artisan optimize. These commands are typically executed by your web server (running as a specific user) or via cron jobs. If the application folder is owned by root because of the initial sudo install, the web server process cannot write to storage/, bootstrap/cache/, or even update the Composer vendor directory for subsequent changes.

The Permission Mismatch in Your Project

Looking at your ls -la output, you can see a clear disconnect between your user (bheng) and the current ownership of your project root:

drwxr-xr-x   8 bheng  staff     272 Mar  6 13:25 database
-rw-------@  1 bheng  staff    1766 Mar 14 09:29 id_rsa
drwxr-xr-x  32 bheng  staff    1088 Mar 30 12:21 vendor

Your vendor directory is owned by bheng, which suggests that at some point, you either ran the install as a non-root user or changed ownership after the initial failure. However, the error log points to /Applications/MAMP/htdocs/code/bheng/vendor/composer/installed.json.

If you are running this on macOS with MAMP, the web server (Apache) often runs as _apache or www, while your local terminal sessions run as your user (bheng). The error occurs because Composer tried to write to the file while acting as root (via sudo), but subsequent processes or even the same process in a different context cannot access it if permissions are too restrictive.

More critically, if you have not yet cleaned up the vendor directory created by the sudo run, that directory might be owned by root. Your ls output shows vendor is currently bheng, which implies you may have manually fixed it or the error happened on a fresh install where the directory was just created.

The Correct Fix: Clean Ownership and Permissions

To resolve this permanently, you must ensure your project root belongs to your user, not root, and that the web server has write access to necessary directories like storage and bootstrap/cache.

First, stop using sudo for local development. Navigate to your project root in a terminal session where you are logged in as your standard user. If a previous sudo composer install left behind files owned by root, change the ownership of the entire project back to your user:

cd /Applications/MAMP/htdocs/code/bheng
sudo chown -R bheng:staff .

This command recursively changes the owner of all files and directories in the project to bheng. After this, run Composer normally without sudo:

composer install

If you encounter permission denied errors on specific directories like storage or bootstrap/cache, ensure those have the correct group permissions. A common pattern for Laravel development is:

sudo chgrp -R staff storage bootstrap cache
chmod -R 775 storage bootstrap cache

This allows members of the staff group (which includes your user and likely the web server process) to write logs, caches, and session files.

Understanding Composer Scripts and Artisan Commands

Your composer.json defines post-install scripts:

"scripts": {
    "post-install-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ]
}

These commands rely on the PHP CLI being able to write to the project directory. If you ever run composer update later, and it tries to modify files in a directory owned by root while your web server runs as _apache, you will hit the same wall again. Always ensure that any directory containing writable assets is owned by the user executing the PHP process or has appropriate group permissions.

Avoiding Future Permission Drift

The most robust solution is to maintain strict ownership boundaries. Your project should be owned by your development user, and the web server should only need read/execute access to code, plus write access to storage and bootstrap/cache. Never run Composer as root unless you are managing a system-wide installation for all users on a server, which is a rare scenario in modern web development.

By cleaning up the current ownership and adhering to these practices, you eliminate the risk of silent permission failures that can cause deployment scripts or automated tests to fail unexpectedly.

Our network