kramer65
kramer65

Reputation: 53873

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?

Upvotes: 117

Views: 173472

Answers (14)

Samuel Martins
Samuel Martins

Reputation: 3580

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.

Upvotes: 296

Ryan Howard
Ryan Howard

Reputation: 9

There are 2 components to consider.

Composer wants you to run it as the logged in user. However, your webserver wants to have permissions over your application.

The solution is to add your user to the webserver group, then update the permissions of your application.

For Ubuntu running Apache webserver, use the following command to add yourself to the Apache group, replacing <username> with your username

    sudo usermod -a -G www-data <username>

Now you need to update your permissions on your application. Navigate to the root folder of your application and use the following command

    sudo chown -R $USER:www-data .

Composer now has the necessary permissions to pull in the packages you need and Apache has the necessary permissions to deliver your application.

Upvotes: -1

Arpit Jain
Arpit Jain

Reputation: 4043

I was facing the same issue when I was running the composer require inside /var/www/html ,the default root folder of the apache web server and I was able to solve it by making the current user the owner of this html directory by

sudo chown -R $USER /var/www/html

But you definitely want to set the permissions

chmod 755 -R /var/www/html

Upvotes: 0

Jason
Jason

Reputation: 4772

In my case all the permissions were correct at all the locations manetioned in other answers here, but I was still getting this error.

Turned out there were some vendor directories that were owned by root. Composer writes composer.lock files all over the place when it's doing an update or install.

So solving my case - and this is specifically for a laravel sail container - all ownerships were switched to user sail in the project:

Enter the sail container as root:

vendor/bin/sail root-shell

Set the file ownership for all files in the project:

chown -R sail:sail /var/www/html

You may just want to do the vendor directory only as a first try:

chown -R sail:sail /var/www/html/vendor

The ownership was wrong after switching from a hand-rolled docker-compose.yaml setup to Laravel Sail, which IMO handles file ownership and permissions in a sensible way, separating root from the application user sail.

Upvotes: 6

ganji
ganji

Reputation: 844

I had same issue in windows version of composer that has installed in

C:\composer

When I was trying this command

C:\composer require aws/aws-sdk-php

then simply I got into composer installed folder and try it again

C:\composer>composer require aws/aws-sdk-php

the package installed quickly .

Upvotes: 0

Basant Mandal
Basant Mandal

Reputation: 106

I was getting the same error when using it with WSL Windows 10. I used the following command to solve it:-

sudo chown -R $USER  /home/<username>/.config/composer

Upvotes: 0

Jonathan
Jonathan

Reputation: 11494

For me, in Ubuntu 18.04. I needed to chown inside ~/.config/composer/

E.g.

sudo chown -R $USER ~/.config/composer

Then global commands work.

Upvotes: 12

wp student
wp student

Reputation: 765

In my case I used sudo mkdir projectFolder to create folder. It was owned by root user and I was logged in using non root user.

So I changed the folder permission using command sudo chown mynonrootuser:mynonrootuser projectFolder and then it worked fine.

Upvotes: -1

javierfmv
javierfmv

Reputation: 1

I was getting the same exception, but in my case I am using PowerShell to run commands So, I fixed this with an instruction to unblock multiple files first. PS C:\> dir C:\executable_file_Path\*PowerShell* | Unblock-File and then use the following to load the package & 'C:\path_to_executable\php.exe' "c:\path_to_composer_.phar_file\composer.phar "require desired/package

Upvotes: 0

Tarasovych
Tarasovych

Reputation: 2398

In my case I don't have issues with ~/.composer.
So being inside Laravel app root folder, I did sudo chown -R $USER composer.lock and it was helpful.

Upvotes: 10

Yasin Okumuş
Yasin Okumuş

Reputation: 2388

I faced this issue as well but in my case, I was in wrong directory. Check the directory you are working

Upvotes: 5

Moak
Moak

Reputation: 12885

This might be super edge case, but if you are using Travis CI and taking advantage of caching, you might want to clear all cache and retry.

Fixed my issue when I was going from sudo to non sudo builds.

Upvotes: 2

Ken
Ken

Reputation: 569

To resolve this, you should open up a terminal window and type this command:

sudo chown -R user ~/.composer (with user being your current user, in your case, kramer65)

After you have ran this command, you should have permission to run your composer global require command.

You may also need to remove the .composer file from the current directory, to do this open up a terminal window and type this command:

sudo rm -rf .composer

Upvotes: 20

Nabil Kadimi
Nabil Kadimi

Reputation: 10394

In my case, .composer was owned by root, so I did sudo rm -fr .composer and then my global require worked.

Be warned! You don't wanna use that command if you are not sure what you are doing.

Upvotes: 3

Related Questions