Sling Academy
Home/DevOps/Homebrew Permission Denied Error: Could not symlink (4 solutions)

Homebrew Permission Denied Error: Could not symlink (4 solutions)

Last updated: January 28, 2024

The Error

Homebrew is a popular package manager for macOS that allows users to install, manage, and uninstall various packages with command-line ease. However, it is not uncommon for users to encounter permission issues, specifically the ‘Permission Denied Error: Could not symlink’ when using Homebrew. This error typically surfaces when Homebrew doesn’t have the necessary permissions to create symlinks in the /usr/local directory. Below we will go through some common solutions to resolve this issue.

Solutions

Check Homebrew Permissions

Permissions in /usr/local need to be correctly set so that the user can write and link packages. If permissions are not set correctly, Homebrew cannot create symlinks necessary for package linking.

  1. Open a terminal window.
  2. Run ls -l /usr/local to inspect the current permissions.
  3. Check if ‘write’ permission is available on the directories Homebrew is attempting to link into.
  4. If permissions are incorrect, use the chown command to take ownership of the directory.

Example:

sudo chown -R $(whoami) /usr/local/*
sudo chown -R $(whoami) /usr/local

This command gives the current user ownership over the /usr/local directory, allowing Homebrew to create the necessary symlinks. This is generally safe but can pose a security risk if multiple users are managing packages on the same system.

Reset Homebrew’s Local Repository

Sometimes resetting Homebrew’s local repository can resolve symlink issues by restoring repository integrity.

  1. Launch terminal.
  2. Run brew update-reset.
  3. Attempt to run the previously failed Homebrew command again.

Example:

brew update-reset

Note: This solution is a non-destructive operation that should not affect the installed formulae, but it can take some time to complete depending on how outdated the local repository is.

Use sudo for Directory Permissions

As a last resort, using sudo can help overcome permission issues which are stopping Homebrew from creating symlinks.

  1. In terminal, open the /etc/sudoers file for editing with sudo visudo.
  2. Add the following line to grant necessary permissions: $(whoami) ALL=(ALL) NOPASSWD: /usr/local.
  3. Save and exit the editor.
  4. Now you can run Homebrew commands with sudo without being prompted for a password when accessing /usr/local.

Example:

sudo visudo
# add the line
$(whoami) ALL=(ALL) NOPASSWD: /usr/local

Notes: Be very cautious with this solution as it elevates your privileges and could potentially open your system up to risks if used improperly. Misconfiguring the sudoers file can also lead to losing administrative access.

Oftentimes, a broken symlink can cause issues. Reparing symlinks can resolve the ‘Cannot symlink’ error.

  1. Open terminal.
  2. Run brew doctor to diagnose issues.
  3. If a broken link is identified, remove it manually.
  4. Link the Homebrew package again using brew link.

Example:

brew doctor
# follow instructions to remove the broken link
# link again
brew link package-name

Note: This method is effective for resolving issues due to broken symlinks within Homebrew’s ecosystem but doesn’t resolve underlying permission issues that could cause future errors.

Conclusion

The ‘Permission Denied Error: Could not symlink’ in Homebrew nearly always points to issues with filesystem permissions or improperly handled links. While solutions like taking ownership of the /usr/local directory or repairing links often work, it’s important to understand the security implications of each approach and choose the one that best fits your system setup. Ensure to follow Homebrew’s best practices and keep it updated for the best experience.

Next Article: Homebrew: How to install a package from GitHub

Previous Article: Homebrew: How to Make a Service Auto-Start on System Boot

Series: Linux Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide