Upgrading GCC from Source Code on Linux Systems


When working with older Linux distributions, you may encounter situations where your project requires a more recent version of GCC than what's available in the system repositories. This guide demonstrates how to compile and install a newer GCC version from source code.

The compilation process requires several prerequisite librareis. We'll build these dependencies first before proceeding with the main GCC compilation.

Prerequisites Setup

First, create a dedicated directory for our build environment:

mkdir -p ~/gcc_build
cd ~/gcc_build

Download the required dependency packages:

wget https://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.2.1.tar.bz2
wget https://gcc.gnu.org/pub/gcc/infrastructure/mpfr-4.1.0.tar.bz2
wget https://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.2.1.tar.gz

Building Dependencies

Let's start with the GMP library:

tar -xvjf gmp-6.2.1.tar.bz2
cd gmp-6.2.1
./configure --prefix=$(pwd)
make
sudo make install
cd ..

Next, compile the MPFR library:

tar -xvjf mpfr-4.1.0.tar.bz2
cd mpfr-4.1.0
./configure --prefix=$(pwd) \
    --with-gmp-include=$(pwd)/../gmp-6.2.1/include \
    --with-gmp-lib=$(pwd)/../gmp-6.2.1/lib
make
sudo make install
cd ..

Finally, build the MPC library:

tar -xvzf mpc-1.2.1.tar.gz
cd mpc-1.2.1
./configure --prefix=$(pwd) \
    --with-gmp-include=$(pwd)/../gmp-6.2.1/include \
    --with-gmp-lib=$(pwd)/../gmp-6.2.1/lib \
    --with-mpfr-include=$(pwd)/../mpfr-4.1.0/include \
    --with-mpfr-lib=$(pwd)/../mpfr-4.1.0/lib
make
sudo make install
cd ..

Update the library path to enclude our new dependencies:

export LD_LIBRARY_PATH=$(pwd)/gmp-6.2.1/lib:$(pwd)/mpfr-4.1.0/lib:$(pwd)/mpc-1.2.1/lib:$LD_LIBRARY_PATH

Compiling GCC

Now we can download and compile the desired GCC version. For this example, we'll use GCC 8.5.0:

wget https://ftp.gnu.org/gnu/gcc/gcc-8.5.0/gcc-8.5.0.tar.gz
tar -xvzf gcc-8.5.0.tar.gz
cd gcc-8.5.0

Configure the GCC build with our dependencies:

./configure \
    --disable-multilib \
    --prefix=$(pwd) \
    --with-gmp-include=$(pwd)/../gmp-6.2.1/include \
    --with-gmp-lib=$(pwd)/../gmp-6.2.1/lib \
    --with-mpfr-include=$(pwd)/../mpfr-4.1.0/include \
    --with-mpfr-lib=$(pwd)/../mpfr-4.1.0/lib \
    --with-mpc-include=$(pwd)/../mpc-1.2.1/include \
    --with-mpc-lib=$(pwd)/../mpc-1.2.1/lib

Start the compilation process (this may take several hours depending on your system):

make -j8
sudo make install

Final Configuration

Update the system environment variables to use our new GCC installation:

export PATH=$(pwd)/gcc-8.5.0/bin:$PATH
export LIBRARY_PATH=$(pwd)/gcc-8.5.0/lib64:$LIBRARY_PATH
export LD_LIBRARY_PATH=$(pwd)/gcc-8.5.0/lib64:$LD_LIBRARY_PATH
export C_INCLUDE_PATH=$(pwd)/gcc-8.5.0/include:$C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH=$(pwd)/gcc-8.5.0/include:$CPLUS_INCLUDE_PATH

Replace the system's default GCC with our newly compiled version:

# Remove old GCC
sudo rm /usr/bin/gcc
sudo rm /usr/bin/g++

# Create symlinks to new GCC
sudo ln -s $(pwd)/gcc-8.5.0/bin/gcc /usr/bin/gcc
sudo ln -s $(pwd)/gcc-8.5.0/bin/g++ /usr/bin/g++

Verify the installation:

gcc --version

Tags: GCC GCC compilation Linux development source code build GCC dependencies

Posted on Sat, 18 Jul 2026 16:37:07 +0000 by bapan