28

This is the command I run:

composer global require "laravel/installer"

and this is the text and error I got after that:

Changed current directory to /home/dimitar/.composer
./composer.json is not writable.

I'm running Ubuntu 16.04 LTS and PHP 7 and Composer version 1.3.0 and running this command from my home folder.

peterh
  • 11,875
  • 18
  • 85
  • 108
Dimitar
  • 1,830
  • 5
  • 32
  • 52

8 Answers8

51

Check the owner of composer.json.

ls -lh ~/.composer/composer.json

If it's 'root', run:

sudo chown -R yourusername:yourusername ~/.composer/composer.json

While in the .composer folder, check the 'cache' folder owner. If it's 'root', re-run above command but switch composer.json to the cache. Otherwise, you may end up with a 'Cannot create cache directory' warning.

Lin Du
  • 88,126
  • 95
  • 281
  • 483
PelleLV
  • 1,031
  • 9
  • 10
  • 2
    l believe changing the whole `.composer` file to be the owner would be the best thing. To avoid future error like when install laravel you can get into issues when the script may try to write something to like the vendor file. `sudo chown -R yourusername:yourusername .composer` – herbertug Oct 28 '18 at 15:59
  • Dont need to typing `-R` flag if u try to set permissions on one file – eldario Feb 23 '19 at 07:04
5

The error says it all, there is a problem with permissions. I guess easiest way to fix this is running this command:

sudo chmod -R 775 /home/dimitar
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
2

I think this code is missing in your composer file which doesn't allows Packagist to find the package of Laravel.

Put this is in your composer.json file and try again:

"repositories": {
    "packagist": { "url": "https://packagist.org", "type": "composer" }
 }
clemens
  • 16,716
  • 11
  • 50
  • 65
Divyesh pal
  • 952
  • 1
  • 11
  • 17
0

Your command is being ran as vagrant user, which hasn't enough permissions.

It can be run as www-data user:

sudo -u www-data composer global require "laravel/installer"

azurecorn
  • 395
  • 5
  • 12
0

anther case. if you are using dual boot operating system make sure other operating system is shut down/power off properly. for instance in my case if windows 10 is hibernated this error will happen to ubuntu os

user10620381
  • 61
  • 1
  • 4
0

You are not the file owner, however, you can change it by typing on your console the following command

 sudo chown -R ~/.composer/composer.json
-4
sudo composer global package_name

Example:

sudo composer global require "laravel/installer"

your password

enter

ztvmark
  • 1,319
  • 15
  • 11
-6

You can run with sudo privilege without change folder permission because you install in global.

Angga Ari Wijaya
  • 1,759
  • 1
  • 15
  • 31