Running Composer without using sudo (tried changed owner and permissions)

Every time I run composer (e.g., sudo composer install, sudo composer self-update etc.) I need to run this with sudo as the owner of the file is root.

However every time I use the composer with sudo root owns the vendor folder and then I have to change the owner of that folder/privileges from root to www-data.

What is the best way to fix this so I do not have to run sudo every time?

Change the owner of /usr/local/bin/composer from root to www-data?

Is this the ideal way to handle this to avoid having to change ownership and assign permission every time I use sudo composer install?

Edit: The permissions for composer are currently -rwxr-xr-x. And I've tried switching the owner of /usr/local/bin/composer over to www-data:www-data with permissions set to 775, and still I can't run composer without running sudo.


If "everyone" is allowed to read and execute composer, you don't need to use sudo:

sudo chmod 755 /var/local/bin/composer

Since you already executed composer at least once as root, composers (per-user-)cache directory is now owned by root and therefore isn't writable by your normal user.

sudo chown -R lamp:lamp /home/lamp/.composer

will fix the file-owner.


I have been dealing with this issue for weeks.

I think the solution is to run composer self-update with the -H

sudo -H composer self-update

Before doing this be sure to remove the .composer directories in root and the home directory of the user you wish to execute composer.

sudo rm -rf /root/.composer
sudo rm -rf /home/ubuntu/.composer

Running sudo composer self-update without the -H flag will create ~/.composer that is owned by root and will prevent other composer commands to have permission errors.

composer config
composer install 

In my opinion calling sudo composer self-update should not create files owned by root in the current users home directory.

Note if you follow these instructions on Ubuntu 14.04 composer will place the cache in:

 ~/.cache/composer

Rather than:

~/.composer/cache

This is because of the XDG_RUNTIME_DIR environment variable defined in Ubuntu 14.04 but doesn't seem to be defined in Ubuntu 12.04

A related discussion here


I found this command useful to run composer as www-data:

sudo su -l www-data -s /bin/bash -c "cd $PWD; composer install"

reference: https://commandroll.com/command/run-command-as-www-data-using-su