I have gcc 7.3.0 on Ubuntu 18.04. But I am working on a project where I need gcc less than version 6. When I tried to install gcc 5.4 using sudo apt install gcc-5.4, I got this error:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package gcc-5.4
E: Couldn't find any package by glob 'gcc-5.4'
E: Couldn't find any package by regex 'gcc-5.4'  

What is the appropriate way to install gcc 5.4?

  • 1
    Try just sudo apt install gcc-5 Oct 25 '18 at 14:24
  • 1
    try to find it with apt search gcc-5 or apt search gcc-6 Oct 25 '18 at 14:25
  • @steeldriver the command worked for me, but when I run gcc --version, I still get 7.3.0
    – singrium
    Oct 25 '18 at 14:29
  • 1
    That's normal - the symlink /usr/bin/gcc will still point to gcc-7. You can either change the symlink (manually - or via update-alternatives) or simply call gcc-5 explicitly when you need it Oct 25 '18 at 14:40
  • @steeldriver, thank you, it worked. Please post an answer and don't forget to include the update-alternatives
    – singrium
    Oct 25 '18 at 15:09

Thanks to @steeldriver, I could solve the problem as following:

Install gcc and g++

sudo apt install g++-5
sudo apt install gcc-5

Change the symlink to point to gcc 5 and g++ 5

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 20
sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
sudo update-alternatives --set cc /usr/bin/gcc
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
sudo update-alternatives --set c++ /usr/bin/g++

Credits: How to choose the default gcc and g++ version?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.