Search code examples
phplinuxlaravelcomposer-phpsudo

Composer: file_put_contents(./composer.json): failed to open stream: Permission denied


I'm trying to install Prestissimo to an Ubuntu 16.04 server, but that leads to an error:

$ composer global require "hirak/prestissimo:^0.3"
Changed current directory to /home/kramer65/.composer


  [ErrorException]
  file_put_contents(./composer.json): failed to open stream: Permission denied


require [--dev] [--prefer-source] [--prefer-dist] [--no-progress] [--no-update] [--no-scripts] [--update-no-dev] [--update-with-dependencies] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--] [<packages>]...

I'm logged in as user kramer65, so I wouldn't know why it can't write to my home folder. My normal reaction to a permission denied is to use sudo, but composer then always says:

Do not run Composer as root/super user! See https://getcomposer.org/root for details

Any idea how I can solve this?


Solution

  • I had this problem to install laravel/lumen.

    It can be resolved with the following command:

    $ sudo chown -R "$(id -un)" "$(composer config --global home)"
    

    What this command does: Execute as privileged user (root) the command to change file owner (not group) to the current user (by name) recursively to the COMPOSER_HOME directory.

    As the user-name as well as the composer home directory can be different on each system, the following two commands are in use to obtain the ...

    1. ... username: id -un
    2. ... composer home directory: composer config --global home

    This is an old question, and so a reader of the answer may want to become aware of the following:

    • Prestissimo was dedicated to be used with Composer 1
    • As of today, Composer 2 has been released which already didn't benefit from Prestissimo any longer.
    • But even further as of today, Composer 2.2 has been released which is a Long Term Support (LTS) version you should replace Composer 1 with immediatly if you deemed it to require Presstissimo in the past as it is in general and overall much more faster than Composer 1 ever was - with or without Prestissimo.

    How to continue:

    1. Remove Prestissmo from any of your systems.
    2. Replace Composer 1 with Composer 2.2 on any of your systems.
    3. Enjoy.