What the 'gcc: command not found' Error Means
The error gcc: command not found (or bash: gcc: command not found) appears in the Linux terminal when the system cannot find the GCC compiler executable file in the directories specified in the PATH environment variable. This occurs when attempting to compile a C/C++ program or run a script that requires GCC.
The full error text usually looks like this:
bash: gcc: command not found
Or simply:
gcc: command not found
The error blocks code compilation and indicates that the compiler is missing or misconfigured.
Causes
- GCC is not installed. In a fresh Linux installation (especially minimal ones), the compiler is often absent by default.
- GCC is installed, but not in a standard directory. For example, if you compiled GCC from source and installed it to
/opt/gcc, but that folder is not added toPATH. - The PATH variable is corrupted or changed. The system only searches for executable files in the directories listed in
PATH. If the path to GCC is removed from this variable, the command is not found. - GCC is installed only for another user. For example, installed via
sudofor root, but not accessible to a regular user (rare, but possible with non-standard configuration). - Symbolic links to GCC are missing. Sometimes GCC is installed in a subdirectory (e.g.,
/usr/lib/gcc/x86_64-linux-gnu/11/), and a symbolic linkgcc -> /usr/lib/gcc/.../gccshould exist in/usr/bin. If the link is broken or removed, the command is not found.
Solutions
Method 1: Installing GCC via Package Manager
The simplest and most reliable method is to install GCC from your distribution's official repositories. This ensures correct paths and dependencies.
For Ubuntu/Debian and derivatives:
sudo apt update
sudo apt install build-essential
The build-essential package includes GCC, G++, make, libc6-dev, and other compilation tools.
For CentOS/RHEL 7:
sudo yum install gcc
For CentOS/RHEL 8 / Fedora / AlmaLinux / Rocky Linux:
sudo dnf install gcc
For Arch Linux:
sudo pacman -S gcc
After installation, verify:
gcc --version
If you see a version number, the issue is resolved.
Method 2: Adding the GCC Path to the PATH Variable
If GCC is already installed (e.g., you manually installed it to /usr/local/gcc), but the system doesn't find it, you need to add the directory containing the executable to PATH.
- Find where
gccis located:which gcc
If the command outputs nothing, search in typical locations:sudo find / -name gcc 2>/dev/null | head -20
GCC is usually in/usr/bin/gcc,/usr/local/bin/gcc, or/opt/gcc/bin/gcc. - Add the path to
PATH. Open the~/.bashrcfile (for bash) or~/.zshrc(for zsh) in a text editor:nano ~/.bashrc
At the end of the file, add a line (replace/path/to/gccwith the actual path, e.g.,/usr/local/bin):export PATH=$PATH:/usr/local/bin
Save and close the editor. - Apply the changes:
source ~/.bashrc
Or open a new terminal window. - Verify:
echo $PATH gcc --version
Method 3: Creating a Symbolic Link (if GCC is Installed in a Non-Standard Location)
If GCC is installed but the executable file resides in a deep subdirectory (e.g., /opt/gcc/bin/x86_64-pc-linux-gnu/gcc), you can create a symbolic link in /usr/local/bin:
sudo ln -s /opt/gcc/bin/x86_64-pc-linux-gnu/gcc /usr/local/bin/gcc
Then verify with gcc --version.
Method 4: Checking and Fixing the PATH Variable
If you or other programs modified PATH and broke it, restore the standard value. Typically, PATH includes /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin.
- View the current
PATH:echo $PATH - If paths are missing, add them temporarily:
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH - To make changes permanent, edit
~/.bashrc(as in Method 2) and add the fullPATHline there.
Method 5: Reinstalling GCC (if the Package is Corrupted)
If GCC was installed via a package manager but functions incorrectly, reinstall it:
For Ubuntu/Debian:
sudo apt remove --purge gcc
sudo apt install build-essential
For CentOS/Fedora:
sudo dnf remove gcc
sudo dnf install gcc
Prevention
- Install GCC from official repositories — this ensures correct paths and automatic updates.
- Do not remove standard directories from
PATH— especially/usr/binand/bin. - Check
PATHafter changes — if you edited~/.bashrcor system files, ensure the syntax is correct. - Use virtual environments for C/C++ projects (e.g., via
condaordocker) to isolate dependencies. - Regularly update your system — this includes updating compiler packages:
sudo apt update && sudo apt upgrade # for Debian/Ubuntu sudo dnf upgrade # for Fedora/CentOS 8+
After completing these steps, the gcc: command not found error should disappear, and you will be able to compile C and C++ programs on your Linux system.