FUNDAMENTALS A Complete Guide for Beginners
The “code: command not found” error in Bash indicates that the VS Code is either not installed or inaccessible. To fix this error, install VS Code using:
sudo snap install --classic code
The code command is associated with Microsoft Visual Studio Code (VS Code) which is a popular source code editor and Integrated Development Environment (IDE). However, encountering “bash: code: command not found” indicates that the VS Code is not installed on your system or the PATH variable is not set. Let’s see the reasons for this error in detail and explore the possible fixes:
Reasons for “code: command not found” Error in Bash
Some common reasons for the “code” command not found error are:
- VS Code not Installed: If you are unable to use the “code” command, it could be that your system does not have Visual Studio Code installed.
- Incorrect Path: The PATH on your system may not contain “code”.
- Incorrect Shell Configuration: The shell configuration files (e.g. ~/.bashrc, ~/.bash_profile, ~/.zshrc) may not be properly configured to include the directory where VS Code is installed.
- Inadequate Permissions: The “code” command might not have the authorization it needs to function.
3 Methods to Solve “bash: code: command not found” Error
Follow the below methods to solve the “code command not found” error in Bash:
1. Install Visual Studio Code
The absence of Visual Studio Code on your system often causes the “bash: code: command not found” error. In this instance, snap can install Visual Studio Code (VS Code) on your system with all its dependencies. To do that, follow the steps:
- Press CTRL+ALT+T to open the terminal in Ubuntu.
- Run the command to install VS Code:
sudo snap install –classic code
- Execute
code --version
to verify the installation.
Now, the “code” command runs successfully.
2. Set “code” PATH Variable
A prevalent reason behind the “code command not found” is VS Code not present in your system’s PATH environment variables. To set the PATH variable for VS Code, follow the steps below:
- Press CTRL+ALT+T to open the terminal.
- Use which command to check the current path of “code”:
which code
- Open bashrc file using nano command:
nano ~/.bashrc
-
Now, replace <path to vscode> in
export PATH=$PATH:<path to vscode>
code with the which command’s output and paste it at the end of the file. For example:export PATH=$PATH:/snap/bin/code
- Press CTRL+S to save, and CTRL+X to exit.
- Then, execute the .bashrc file using:
source ~/.bashrc
- Now you can check running a “code” command. For example:
code –version
After adding the path, the code command ran without any error.
3. Set an Alias to “code”
To fix the “code: command not found” error, you can create a temporary solution by setting up an alias for “code”. Here’s how you can do it:
echo "alias code='$(which code)'" >> ~/.bashrc; . ~/.bashrc
This command appends an alias for the code command to the ~/.bashrc file and immediately applies the changes in the current terminal session. The alias ensures that the code command points to the correct path by using the $(which code)
syntax, which dynamically retrieves the actual path to the code executable.
How to Solve “code command not found” Error in MacOS?
The “code command not found” error in macOS indicates that the VS Code is not installed. To solve the error open the Mac terminal and run the below command to install VS Code:
brew update
brew install --cask visual-studio-code
During the installation process, you may have the option to add VS Code to your system’s PATH. If you didn’t see this option, open the terminal and add the following line to your shell profile file (e.g.~/.zshrc for Zsh or ~/.bash_profile for Bash):
export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
Adjust the path in the command if your VS Code installation location is different.
Conclusion
In conclusion, resolving the “bash: code: command not found” issue involves ensuring the installation of VS Code and configuring the system’s PATH appropriately. Applying these solutions allows you to seamlessly access code commands within the Bash environment and streamline various aspects of Microsoft Visual Studio Code IDE to enhance various programming and development tasks.
People Also Ask
How do I fix code command not found in Linux?
To fix the “code: command not found” issue in Linux, install Visual Studio Code (VS Code) on your system. After installation, check if the directory containing the code executable is in your system’s PATH. If not, you can either add the directory to your PATH manually using the export PATH=$PATH:$(which code)
command.
Why code command not found in Linux?
The “code: command not found” error in Linux typically occurs when the code command is not in the system’s PATH or when Visual Studio Code is not installed. Ensure that VS Code is installed, and you might need to add the directory containing the code executable to your PATH or use the full path to the executable when running the command.
How do I enable Bash terminal in VSCode?
To enable the Bash terminal in Visual Studio Code, open the editor and navigate to the “View” menu. Click on “Terminal” or use the shortcut CTRL + to open the terminal panel. By default, VS Code uses the system’s default shell. To specifically set it to Bash, click on the dropdown in the terminal panel and select “Select Default Shell.” Choose “bash” from the list, and VS Code will use Bash as the default terminal shell for that session
How do I link code command to VSCode?
To link the code command to Visual Studio Code (VS Code) on Linux, use the following command in the terminal: sudo ln -s /path/to/VSCode/bin/code /usr/local/bin/code
(replace “/path/to/VSCode” with the actual path to your VS Code installation). This creates a symbolic link named “code” in the “/usr/local/bin” directory, making the code command globally accessible.
Does VSCode install code command in PATH?
Yes, during the installation of Visual Studio Code (VS Code), you typically have the option to add the code command to your system’s PATH. If you choose this option, VS Code automatically adds the directory containing the code executable to the PATH environment variable. This enables you to use the code command globally.
Related Articles
- [Fixed] “bash: sudo: command not found” Error
- [Solved] Bash “yarn” Command Not Found Error
- [Solved] Bash “docker” Command Not Found Error
- [Fixed] “bash: ng: command not found” Error
- [Fixed] “bash: java: command not found” Error
- Solved “bash: yum: command not found” [RHEL, Fedora, CentOS]
- [Solved] “bash: vim: command not found” Error
- [Fixed] “bash: curl: command not found” Error
- [Solved] “bash: node: command not found” Error
- [Fixed!] “systemctl command not found” Error in Linux
<< Go Back to [Solved] “bash: command not found” Error | Bash Error Handling and Debugging | Bash Scripting Tutorial