How to Install GCC in Ubuntu via Terminal or running on VirtualBox

How to Install GCC in Ubuntu via Terminal or running on VirtualBox

Installing the GNU Compiler Collection (GCC) on Ubuntu is a crucial step for developers who are interested in programming in C, C++, or other languages supported by GCC. Whether you’re working directly on a local Ubuntu machine or within a virtual environment using VirtualBox, having access to GCC ensures you can compile and run your code efficiently. In this guide, we will walk you through the process of installing GCC via the Terminal, providing a reliable and professional approach for both new and experienced users.

Why Use GCC on Ubuntu?

GCC is one of the most widely-used compilers in the open-source community. It’s renowned for its stability, performance, and support for a wide range of programming languages. Ubuntu, being a popular Linux distribution, integrates well with GCC and the ecosystem of tools that come along with it.

Before You Begin

Make sure your system is updated and has administrative privileges to install new packages. This guide assumes you have a working version of Ubuntu installed either directly or inside a virtual machine like VirtualBox. If you’re running Ubuntu within VirtualBox, ensure the virtual machine has internet access as the installation will pull packages from the official repositories.

Step-by-Step Installation Guide

1. Open the Terminal

Press Ctrl + Alt + T to open a new Terminal window. Alternatively, you can search for “Terminal” using the system Dash.

2. Update the Package Index

Before installing any package, it’s good practice to update the package lists to ensure you’re getting the latest versions available:

sudo apt update

3. Install the build-essential Package

To get GCC and other necessary compiling tools, install the build-essential meta-package. It includes:

  • GCC Compiler
  • G++ (C++ compiler)
  • Make utility
  • Standard libraries
sudo apt install build-essential

When prompted, enter your password and confirm the installation by typing Y when asked.

4. Verify Installation

Once the installation completes, you can verify that GCC was installed correctly by checking its version:

gcc --version

This should output something like:

gcc (Ubuntu 11.4.0) 11.4.0

Congratulations! You now have GCC installed and ready to use.

Troubleshooting Tips

If you encounter errors during installation, consider the following:

  • Ensure your internet connection is active in VirtualBox by selecting Bridged Adapter or NAT in network settings.
  • Clear the package cache with sudo apt clean and try the installation again.
  • Check for broken or missing packages using:
    sudo apt --fix-broken install

Running Your First Test Program

After installing GCC, it’s a good idea to test it with a simple C program:


#include <stdio.h>
int main() {
    printf("GCC installation successful!\n");
    return 0;
}

Save this in a file named test.c and compile it by running:

gcc test.c -o test

This will produce an executable named test. Run it using:

./test

You should see the output: GCC installation successful!

Conclusion

Installing GCC on Ubuntu, whether on a native system or in VirtualBox, is a straightforward process that unlocks the full power of programming on Linux. Equipped with GCC, you’ll be ready to compile C/C++ projects and explore advanced development tools. By following this guide carefully, you ensure a clean and secure installation suitable for academic, professional, or personal programming work.