File_put_contents(./composer.lock): Failed to open stream: Permission denied

Viewed 4166

I'm new to Ubuntu, and I'm trying to get my Ubuntu web server running, but I have a problem with composer. When I run composer install it outputs this error [ErrorException] file_put_contents(./composer.lock): Failed to open stream: Permission denied. I found a solution for this on the internet, but it doesn't work for me

Not working solution:

 sudo chown -R $USER ~/.composer/

It outputs

chown: cannot access '/home/admin/.composer/': No such file or directory

I'm using Ubuntu 20.04. Does anyone know what should I do?

3 Answers

This issue can easily be solved by creating the lock file and then modifying the ownership.

  1. sudo touch composer.lock

  2. sudo chown -R $USER ./composer.lock

  3. composer update

OR

Just own the projec folder:

sudo chown -R $USER path/to/project/folder

With this, you don't have to use sudo to run composer update

composer install is trying to create a .composer.lock file in your project directory, so php should have permission to do that.

A quick hack is to use sudo composer install ,but this is not the recommended way.

this should also work

sudo chown -R :www-data /fullpath-to-your-project

Running sudo chown -R $USER ~/.composer/ won't work because indeed there's no file/folder in the Laravel structure called 'composer' but rather 'composer.lock'. Instead, running sudo chown $USER composer.lock would make more sense (replace $USER with your actual username). This however won't fix the issue entirely.

That said, you should do the following to ensure that composer install (or composer update) runs successfully: I am assuming your project path is /var/www/html/your-project.

  1. Change ownership of your Laravel project folder: sudo chown -R www-data.www-data /var/www/html/your-project

  2. Change the permissions of your Laravel project folder: sudo chmod -R 755 /var/www/html/your-project

  3. Make the storage folder writable: sudo chmod -R 777 /var/www/html/your-project/storage

  4. Then make sure you add the current user to the www-data group: sudo usermod $USER -g www-data (replace $USER with your username)

Now you should be able to run composer install or composer update without permission errors.

Related