First check your own gcc version, the default is 7.3 on Ubuntu18.04
$ gcc --version
gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
- Download gcc/g++ 5
sudo apt-get install -y gcc-5
sudo apt-get install -y g++-5
- Link gcc/g++ to downgrade
cd /usr/bin
sudo rm gcc
sudo ln -s gcc-5 gcc
sudo rm g++
sudo ln -s g++-5 g++
Check the gcc version again and you can see that it has been downgraded.
$ gcc --version
gcc (Ubuntu 5.5.0-12ubuntu1) 5.5.0 20171010
cd /usr/bin
sudo rm gcc
sudo ln -s gcc-7 gcc
sudo rm g++
sudo ln -s g++-7 g++