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:
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:
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
Method 1: Change Directory Ownership (Recommended)
The most reliable solution is to change ownership of the /usr/local/bin
directory to your user account:
sudo chown -R $USER /usr/local/bin
After running this command:
- Open VS Code
- Press
Cmd + Shift + P
to open the command palette - Search for and select "Shell Command: Uninstall 'code' command in PATH"
- Search for and select "Shell Command: Install 'code' command in PATH"
- 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:
- Uninstall the
code
command from VS Code's command palette - Fully remove VS Code from your Applications folder
- Download and install the latest version from code.visualstudio.com
- Move the application to your Applications folder
- 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):
code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}
Then reload your shell configuration:
source ~/.zshrc # or ~/.bash_profile
Verifying the Solution
After applying any solution, verify it works by:
- Opening a new terminal window
- Navigating to any directory
- Running
code .
to open VS Code in that directory - 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.