How to Solve GCC Command Not Found Error in Bash

GCC, the GNU Compiler Collection, is an essential tool for compiling C, C++, and other programming languages. However, encountering the “gcc command not found” error in Bash can be frustrating, especially for developers working on projects that require compilation. This error typically indicates that GCC is not installed on your system or that it’s not in your system’s PATH.
In this article, we’ll explore various methods to troubleshoot and resolve this issue effectively. Whether you’re a seasoned developer or a beginner, these solutions will help you get GCC up and running smoothly so you can focus on your coding tasks without interruptions.
Check if GCC is Installed
Before diving into solutions, the first step is to determine if GCC is installed on your system. You can easily do this by using the command line. Open your terminal and run the following command:
gcc --version
If GCC is installed, you will see a version number. If not, you’ll receive the “command not found” error. This simple check can save you time by confirming the status of your GCC installation.
Output:
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
If you see the version number, you’re all set! If not, you may need to install GCC.
Install GCC on Your System
If you’ve confirmed that GCC is not installed, the next step is to install it. The installation process varies depending on your operating system. Below, we’ll cover how to install GCC on both Ubuntu and macOS.
Installing GCC on Ubuntu
For Ubuntu users, installing GCC is straightforward. You can use the package manager apt
to install it. Open your terminal and execute the following commands:
sudo apt update
sudo apt install build-essential
The build-essential
package includes GCC along with other essential tools for building software. After the installation completes, you can verify it again by checking the version.
Output:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
build-essential gcc gcc-9 g++ g++-9
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Once installed, you should be able to use the gcc
command without any issues. If you still encounter problems, it might be a PATH issue.
Installing GCC on macOS
For macOS users, the process is slightly different. You can install GCC using Homebrew, a popular package manager. If you haven’t installed Homebrew yet, you can do so by running:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
After Homebrew is installed, you can install GCC with:
brew install gcc
Once the installation is complete, check the version again to ensure GCC is set up correctly.
Output:
==> Downloading https://homebrew.bintray.com/bottles/gcc-11.2.0.catalina.bottle.tar.gz
==> Pouring gcc-11.2.0.catalina.bottle.tar.gz
After following these steps, you should be able to compile your code without encountering the “command not found” error.
Update Your PATH Variable
If you have installed GCC but still see the “command not found” error, the issue might be related to your system’s PATH variable. The PATH variable tells your system where to look for executable files. If the directory containing GCC is not in your PATH, you won’t be able to run the command.
Adding GCC to Your PATH
To resolve this, you can manually add the directory where GCC is installed to your PATH. First, you need to find out where GCC is located. You can do this using the which
command:
which gcc
This command will return the path to the GCC executable. Once you have the path, you can add it to your PATH variable. For example, if GCC is located in /usr/local/bin/gcc
, you would add the following line to your .bashrc
or .bash_profile
:
export PATH=$PATH:/usr/local/bin
After editing the file, apply the changes by running:
source ~/.bashrc
Or, if you edited .bash_profile
:
source ~/.bash_profile
Now, try running the gcc --version
command again. If everything is set up correctly, you should see the version number without any errors.
Conclusion
Encountering the “gcc command not found” error in Bash can be a common hurdle for developers, but it is usually easy to fix. By checking if GCC is installed, installing it if necessary, and ensuring that your PATH variable is correctly set, you can resolve this issue quickly. Remember, having a properly configured development environment is key to being productive. With these solutions in hand, you can compile your code without any interruptions and focus on what you love most—coding!
FAQ
-
What is GCC?
GCC stands for GNU Compiler Collection, which is a set of compilers for various programming languages, including C and C++. -
How do I check if GCC is installed?
You can check if GCC is installed by running the commandgcc --version
in your terminal. -
What should I do if I see “command not found” after installing GCC?
If you see “command not found” after installing GCC, check if the installation path is included in your PATH variable.
-
Can I install GCC on Windows?
Yes, you can install GCC on Windows using MinGW or Cygwin. -
What is the purpose of the build-essential package in Ubuntu?
The build-essential package includes essential tools for building software, including GCC and other necessary libraries.
Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.
LinkedIn