Fixing VSCode ‘command not found: code’ in MacOS

Introduction
If you’ve ever restarted your Mac and found that the code
command no longer works, you’re not alone. This issue is often caused by App Translocation, a macOS security feature that runs applications from a temporary location. As a result, the symlink for code
(Visual Studio Code’s CLI command) breaks after a reboot.
In this guide, we’ll explain why this happens and how to fix it permanently.
The Problem: Why Does This Happen?
When running the ls -l /usr/local/bin/code
command after a restart, you might see something like this:
lrwxr-xr-x 1 root wheel 167 Feb 5 10:10 /usr/local/bin/code -> /private/var/folders/5b/a1b2c3d4e5_6f7g8h9i_jk10000lm/T/AppTranslocation/F1234567-89AB-CDEF-0123-456789ABCDEF/d/Visual Studio Code.app/Contents/Resources/app/bin/code
The key issue is that the path points to a translocated version of VS Code inside /private/var/folders/.../AppTranslocation/
. This happens when:
- You run VS Code directly from the
.dmg
file instead of installing it properly. - macOS treats the app as unverified and runs it from a temporary directory.
- The temporary path disappears after a restart, breaking the
code
command.
The Solution: Properly Installing VS Code and Fixing the Symlink
To fix this issue permanently, follow these steps:
1. Install VS Code Correctly
Just make sure you’ve VSCode in the Application directory.
2. Remove the Broken Symlink
Delete the incorrect code
command symlink:
sudo rm /usr/local/bin/code
3. Create a New Symlink
Now, create a correct symlink pointing to the proper VS Code installation:
sudo ln -s /Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code /usr/local/bin/code
This ensures that the code
command always points to the actual installed location.
4. Verify the Fix
Run the following command to check that the symlink is correct:
ls -l /usr/local/bin/code
Expected output:
/usr/local/bin/code -> /Applications/Visual Studio Code.app/Contents/Resources/app/bin/code