Permission Denied When Creating Directory or Writing a File

Overview

If you receive an error telling you that you do not have permissions to create a directory or to write a file to a directory, this is likely an indication that your script is attempting to write to a directory that you do not own. 

This is a somewhat common pitfall that many users run into when moving into a CI environment. 

Builds run as the distiller user on MacOS and typically ubunutu on Linux. These users only have write permissions in their $HOME folders and places like /tmp. This is not unique to CI and is true by default in almost all Linux/Unix environments.

To confirm the user your build runs as has proper permissions, you can run the whoami command within your build process.

Solution

There are a couple of possible solutions to this issue:

  1. Create a folder that the user running the build has permissions to.
  2. Change the ownership of the directory with the chown command before trying to write to it.

We recommend the first solution. If you decide to go with the second solution then a command like this should work in both MacOS and Linux builds.

chown -R $USER:$USER /path/to/directory

$USER is a global environment variable that refers to the current logged in user.

/path/to/directory should be replaced with the path to where you want to write to.

Was this article helpful?
130 out of 615 found this helpful

Comments

0 comments

Article is closed for comments.