Installation Guide – Installing GCC on Ubuntu

“I just want to run a Go program, but I found out I need to set up a C compiler first?!”Don’t worry, I’ll guide you through this journey easily.

๐Ÿ“Œ Prerequisites: You need the following

โœ” A computer that can run Ubuntu (like 18.04, 20.04, 22.04)โœ” Administrator privileges (you should be able to use <span>sudo</span>)โœ” Most importantly: you want to run a Go program, but the system says “you need to install GCC first!”

๐Ÿง Why does Go need GCC?

Go is inherently a “standalone” language, and you can compile it using the official <span>go</span> toolchain, but sometimes it willcall the C compiler (GCC) to handle underlying dependencies, for example:

  • โ€ข Using CGO (Go calling C language libraries)
  • โ€ข Compiling Go libraries that contain C code
  • โ€ข Using the GCCGo compiler (another version of Go from the GCC family)
  • โ€ข The system lacks <span>build-essential</span>, causing errors in the Go build process

๐Ÿ”ง So even if you are a pure Go developer, it’s best to have some tools in your system’s “toolbox”,install GCC, so your Go code won’t “fail” halfway through.

๐Ÿ› ๏ธ Installation Steps:

1๏ธโƒฃ Update the system: Don’t treat an old system like a new computer

sudo apt update
sudo apt upgrade -y

โ˜•๏ธ This step is like washing your face and brushing your teeth, don’t skip it, the system will be revitalized.

STEP 01:

Installation Guide - Installing GCC on Ubuntu

STEP 02:

Installation Guide - Installing GCC on Ubuntu

2๏ธโƒฃ Install GCC and friends: The building blocks of the world

sudo apt install build-essential -y

๐Ÿ” <span>build-essential</span> not only installs <span>gcc</span>, but also brings along these:

  • โ€ข <span>g++</span>: C++ compiler
  • โ€ข <span>make</span>: The building tool
  • โ€ข Header files & library files: Without them, the Go build process might directly fail
Installation Guide - Installing GCC on Ubuntu

3๏ธโƒฃ Check the installation:

gcc --version
g++ --version

You should see something like this (don’t worry too much about the version):

gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Installation Guide - Installing GCC on Ubuntu

4๏ธโƒฃ Compile test code:

nano hello.c

Paste this little snippet in:

#include &lt;stdio.h&gt;

int main() {
    printf("Hello, GCC!\n");
    return 0;
}

Then press:

  • โ€ข <span>Ctrl+O</span> โ†’ Save
  • โ€ข <span>Enter</span> โ†’ Confirm
  • โ€ข <span>Ctrl+X</span> โ†’ Exit the editor

Next:

gcc hello.c -o hello
./hello

โœ… If you see <span>Hello, GCC!</span>, it means it really works, not just a decoration.

๐Ÿ“ฆ Installing a specific version of GCC (like if you really want version 12)

sudo apt install gcc-12 g++-12

Then tell the system “use this version by default from now on”:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-12 100

โš™๏ธ If you want to switch versions, you can run:

sudo update-alternatives --config gcc

๐Ÿงน Uninstall GCC:

sudo apt remove build-essential gcc g++

With this set of instructions, installing GCC on Ubuntu is a breeze, now everyone can start practicing…

Leave a Comment