How to Fix “Unable to execute gcc: No such file or directory” Error When Installing Python Packages on Ubuntu


2 views

When working with Python packages that require compilation (like Scrapy, pandas, or NumPy), you'll encounter the dreaded GCC error if your system lacks proper build tools. This is particularly common in:

  • Fresh Ubuntu installations
  • Docker containers with minimal images
  • Cloud instances with stripped-down environments

First, confirm whether GCC is actually missing:

which gcc
# Expected output: /usr/bin/gcc
# If blank, GCC isn't installed

gcc --version
# Should return version info if installed

The most robust fix involves installing both GCC and Python development headers:

sudo apt update
sudo apt install -y build-essential python3-dev libssl-dev libffi-dev

For virtualenv users, ensure the environment inherits system packages:

python3 -m venv myenv --system-site-packages
source myenv/bin/activate

Case 1: If you're using Docker, add this to your Dockerfile:

RUN apt-get update && \
    apt-get install -y build-essential python3-dev && \
    rm -rf /var/lib/apt/lists/*

Case 2: For AWS EC2 Ubuntu instances, sometimes you need to set the PATH:

export PATH=$PATH:/usr/bin

After installation, verify with a test package:

pip install --no-cache-dir cryptography
# This package typically requires compilation

For development machines, consider installing these meta-packages:

sudo apt install -y ubuntu-dev-tools python3-venv python3-pip

And add this to your ~/.bashrc:

export C_INCLUDE_PATH=/usr/include/x86_64-linux-gnu
export CPLUS_INCLUDE_PATH=/usr/include/x86_64-linux-gnu

When working with Python packages that require compilation (like Scrapy, lxml, or scientific computing packages), you'll often encounter this error:

Unable to execute gcc: No such file or directory
Error: command 'gcc' failed with exit status 1

This occurs because Ubuntu doesn't include build tools by default, and many Python packages need GCC to compile C extensions during installation.

First, update your package lists:

sudo apt-get update

Then install the essential build tools package:

sudo apt-get install build-essential

For Python-specific development headers (required for many packages):

sudo apt-get install python3-dev

For Scrapy specifically, you'll also need these:

sudo apt-get install libssl-dev libffi-dev

If you're working with data science packages:

sudo apt-get install libblas-dev liblapack-dev

Check if GCC is now available:

gcc --version

You should see output similar to:

gcc (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0

Even when working in a virtualenv, the system-wide GCC is used for compilation. After installing the dependencies, reactivate your virtual environment:

deactivate
source venv/bin/activate

For consistent development environments, consider using Docker with a Python image that includes build tools:

FROM python:3.9-slim

RUN apt-get update && \
    apt-get install -y build-essential python3-dev && \
    rm -rf /var/lib/apt/lists/*

If problems continue:

  1. Check PATH environment variable: echo $PATH
  2. Verify gcc location: which gcc
  3. Reinstall build-essential: sudo apt-get --reinstall install build-essential