Skip to content

Fix "Permission Denied, Unlink 'usr/local/bin/code'" Error in VS Code on macOS

Problem Description

When attempting to install the code command in Visual Studio Code on macOS, users encounter a permission error:

bash
EACCES: permission denied, unlink 'usr/local/bin/code'

Additionally, even if the installation initially succeeds, the code command may disappear from the terminal after closing VS Code or after some time, resulting in:

bash
zsh: command not found: code

This issue is particularly common on Apple Silicon Macs (M1/M2/M3) but can affect any macOS installation.

Root Cause

The error occurs because VS Code lacks the necessary permissions to modify files in the /usr/local/bin directory. This directory is typically owned by the root user, while VS Code runs with your user permissions.

Solutions

The most reliable solution is to change ownership of the /usr/local/bin directory to your user account:

bash
sudo chown -R $USER /usr/local/bin

After running this command:

  1. Open VS Code
  2. Press Cmd + Shift + P to open the command palette
  3. Search for and select "Shell Command: Uninstall 'code' command in PATH"
  4. Search for and select "Shell Command: Install 'code' command in PATH"
  5. Test with code . in your terminal

WARNING

Use sudo commands with caution. Only modify system directories if you understand the implications.

Method 2: Reinstall VS Code Completely

If changing ownership doesn't resolve the issue:

  1. Uninstall the code command from VS Code's command palette
  2. Fully remove VS Code from your Applications folder
  3. Download and install the latest version from code.visualstudio.com
  4. Move the application to your Applications folder
  5. Install the code command via the command palette

Method 3: Manual PATH Configuration

As a fallback solution, you can manually add the code function to your shell configuration:

For bash (~/.bash_profile) or zsh (~/.zshrc):

bash
code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}

Then reload your shell configuration:

bash
source ~/.zshrc  # or ~/.bash_profile

Verifying the Solution

After applying any solution, verify it works by:

  1. Opening a new terminal window
  2. Navigating to any directory
  3. Running code . to open VS Code in that directory
  4. Restarting your computer and testing again

Prevention

To prevent future permission issues:

  • Avoid running VS Code with sudo privileges
  • Regularly update VS Code to the latest version
  • Ensure your user account has appropriate permissions for /usr/local/bin

TIP

If you continue experiencing issues, check the official VS Code macOS setup guide for the latest recommendations.

Conclusion

The "permission denied" error when installing the code command is typically resolved by changing ownership of the /usr/local/bin directory to your user account. The most effective solution combines directory ownership modification with a reinstallation of the code command through VS Code's command palette.