I’ve been trying to find a way to set a specific version of GCC and G++ as the default compiler on my Ubuntu system, but I’m kind of hitting a wall here. I’m running Ubuntu 20.04, and I’m working on a project that depends on GCC 9, but for some reason, my system keeps defaulting to GCC 10. I know I can just use the version number directly when I compile, but that gets tedious after a while, especially if I have to type it over and over again in multiple terminal windows.
So, here’s what I’ve tried so far: I looked into update-alternatives but I’m not entirely sure I’m using it correctly. I managed to add both GCC 9 and 10 to the alternatives, but when I switch the default to GCC 9, I still seem to be getting GCC 10 when I run `gcc –version`. I’ve also made sure to select the right alternatives for G++, but it’s just not sticking.
What’s more confusing is that I’ve read some resources online that suggest changing the symlinks in `/usr/bin` directly, but that feels risky since I don’t want to mess up my system. I’d appreciate it if someone could break this down for me in a more straightforward way.
Is there a step-by-step guide or commands that I can simply run in the terminal to ensure that GCC 9 and G++ 9 are the ones being used by default? I’m looking for the simplest method here because I don’t want to get into a rabbit hole of configurations and end up with a system that doesn’t even compile anymore.
I’d love to hear from anyone who’s dealt with this kind of situation before! How did you set the default versions? Did you face any issues along the way, or is it a pretty smooth process once you get the hang of it? Any help or insights would be greatly appreciated!
How to Set GCC 9 and G++ 9 as Default on Ubuntu 20.04
If you want to set GCC and G++ to version 9 as the default on your Ubuntu 20.04 system, here’s a simple step-by-step guide. It sounds like you’ve already tried the alternatives method, which is the right way to go!
Step 1: Install GCC and G++ 9
First, ensure that you have GCC 9 installed on your system. You can install it using:
Step 2: Update Alternatives
Next, you’ll want to set up the alternatives. You can do this with the following commands:
Step 3: Choose the Default Version
Now, you can select GCC 9 as the default version. Run:
You’ll see a list of installed versions. Enter the number corresponding to GCC 9 and press Enter.
Do the same for G++:
Step 4: Check the Versions
To confirm that it worked, run:
Note
If after all this, you still see GCC 10, you might want to check your PATH or any shell configuration files (.bashrc, .bash_profile) to ensure they’re not overriding your settings.
Final Thoughts
Editing symlinks directly in /usr/bin isn’t recommended as it can lead to unstable behavior in your system. Using update-alternatives is indeed the safest method.
To set GCC and G++ 9 as the default compilers on your Ubuntu 20.04 system, you’ll want to use the `update-alternatives` command, which is designed to manage different versions of software. First, add both GCC versions to the alternatives system. Open your terminal and run the following commands:
After adding the alternatives, you can select the default version by running the following commands:
Follow the prompts to select GCC and G++ 9 from the list. Once you’ve made your selections, you should verify the default version with:
This should now show GCC 9 and G++ 9 as the default compilers. If you’re still encountering issues, ensure that no other configurations or symlinks are conflicting with your choices. Using `update-alternatives` is generally safer than manually changing symlinks in `/usr/bin`, but if problems persist, rebooting your system may help clear cached configurations.