I wanted to install gcc-6 alongside my existing installation of gcc-9 and this is how I did it. First off, sudo apt install gcc-6 didn't work because the package wasn't found so I had to add a new repository that contained gcc-6. To do this, I first found a repository that contains gcc-6 from Google and ended up at: https://packages.ubuntu.com/bionic/gcc-6
From there, I chose an architecture (amd64) which took me to a page with all the mirrors. I added the first mirror (mirrors.kernel.org/ubuntu) to /etc/apt/sources.list and did sudo apt update and then installed gcc-6 with sudo apt install gcc-6.
To switch between gcc versions, I used the following:
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-6 6
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 9
sudo update-alternatives --config g++
Answer from Michael Kiros on Stack OverflowI wanted to install gcc-6 alongside my existing installation of gcc-9 and this is how I did it. First off, sudo apt install gcc-6 didn't work because the package wasn't found so I had to add a new repository that contained gcc-6. To do this, I first found a repository that contains gcc-6 from Google and ended up at: https://packages.ubuntu.com/bionic/gcc-6
From there, I chose an architecture (amd64) which took me to a page with all the mirrors. I added the first mirror (mirrors.kernel.org/ubuntu) to /etc/apt/sources.list and did sudo apt update and then installed gcc-6 with sudo apt install gcc-6.
To switch between gcc versions, I used the following:
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-6 6
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 9
sudo update-alternatives --config g++
You need to use the equals sign instead of the colon.
sudo apt-get install gcc=4:8.2.0-1ubuntu1
You'll also need to update your default gcc config.
How to change the default GCC compiler in Ubuntu?
Videos
Tested on Ubuntu 20.04:
sudo add-apt-repository ppa:ubuntu-toolchain-r/ppa
sudo apt update
sudo apt install g++-10
I replaced the version used in this article with mine and installed to /usr/loacl/bin instead.
Then I needed to update the gcc alternatives as shown here. I replaced this version with mine. I used /usr/local/bin/gcc-10.2.0/bin/gcc-10.2 to use the executable instead of the folder. It's currently working for me.