14

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.

3 Answers 3

14

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.

6
  • I've tried this and I'm still unable to run composer self-update without using sudo. Do I need to use 775? Commented Mar 17, 2015 at 4:23
  • Well self-update won't work with 755 but does this matter? self-update is nothing you'll need to execute every day, just update it once every 2 weeks or so (with sudo of course).
    – tkausl
    Commented Mar 17, 2015 at 4:36
  • Tried composer update and this also doesn't work with those permissions. I still have to use sudo first. This is the error I get file_put_contents(/home/lamp/.composer/cache/repo/https---packagist.org/packages.json): failed to open stream: Permission denied Commented Mar 17, 2015 at 22:40
  • ooh, yes, you already used "sudo" to update some of your deps, i think "/home/lamp/.composer/" is owned by root, just delete this folder (its just a cache), then it should work without sudo. (Or change ownership: chown -R lamp:lamp /home/lamp/.composer )
    – tkausl
    Commented Mar 18, 2015 at 2:39
  • That did the trick, went with changing the owner to lamp. Can you update your answer and I'll accept it? Thanks takusl Commented Mar 18, 2015 at 21:22
6

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

5

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

2
  • This doesn't help if you're already running the most current version of composer. $ sudo -H composer self-update $ You are already using composer version 1.3.0 (stable channel).
    – CragMonkey
    Commented Jan 5, 2017 at 3:47
  • @Cragmonkey - I was running the latest version of composer but just removing the ~/.composer folder seems to solve the problem. Commented Feb 19, 2017 at 10:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .