You need to update your system:
sudo apt update
sudo apt upgrade
sudo apt install gcc
Answer from GAD3R on Stack ExchangeInstalling gcc error
Can't install GCC in Ubuntu 20.04 WSL - Stack Overflow
error while installing gcc on ubuntu 14.04.3 - Stack Overflow
linux - how to install gcc-12 on ubuntu - Stack Overflow
Videos
If sudo apt-get update does not work for you, you should maybe try sudo apt-get clean to clean the cache and then execute sudo apt-get update
This may or may not be your problem, but I had the same issue when first installing my copy of Ubuntu 18.04.01. I did not yet have the drivers of my wifi-dongle installed, and, as such, I couldn't reach any site (including the sites of Ubuntu working with package updates).
So, it may be an internet-related issue. I read in the comments that
you could execute apt-get update. Maybe there is a different
connection issue at work? I would check those settings.
I tried to install gcc in Ubuntu on WSL2 and get the following error,
E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/l/linux/linux-libc-dev_5.4.0-54.60_amd64.deb 404 Not Found [IP: 91.189.88.152 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
Please help me fix this.
Is GCC-4.8 available for 12.04 Precise now?
Update: As of 6/05/2013, there is currently a release of gcc 4.8.1 for 12.04(precise) available at https://launchpad.net/~ubuntu-toolchain-r/+archive/test.
The short answer is: gcc-4.8 is currently available for 12.04(Precise) via the toolchain PPA or by compiling the source(see below for details).
Release Announcement
The announcement for the release of gcc-4.8 was made on March 22nd, so you probably won't be seeing this in an official repository just yet.
However, it appears that Launchpad does have a PPA available for the toolchain test builds that does include gcc-4.8, here: https://launchpad.net/~ubuntu-toolchain-r/+archive/test
According to the changelog and package list, there are debs for i386, amd64, arm, and powerpc and were built against raring 13.04.
The latest available version of gcc, for 12.04, is 4.8.1 and is available in the toolchain PPA.
Adding the toolchain/test PPA:
To add the PPA to your system, open a terminal(Ctrl+Alt+t) and run the following commands:
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50
You will also be prompted to install a number of suggested packages. These packages are recommended, but optional, for the installation of gcc-4.8.
- What are PPAs and how do I use them?
- Are PPA's safe to add to my system and what are some "red flags" to watch out for?
What C++ features are available in gcc-4.8 that are not available in gcc-4.7.2?
- According to GNU.ORG, gcc-4.7, supports a number of C++11 features.
As of the gcc-4.8 release, gcc-4.7 does not support 8 out of 64 features (See C++0x/C++11 Support in GCC)
These features are:
Language Features - Rvalue references for *this - Generalized attributes - Alignment support - Inheriting constructors Concurreny - Bidirectional Fences - Memory model - Abandoning a process and at_quick_exit - Thread-local storage- For a full list of changes in gcc-4.8, see the Changelist
I need these gcc features on 12.04 Precise, now. How can I get them?
Building GCC-4.8 from source:
If you need gcc-4.8 on 12.04 now, your only option is to build it from source.
Please read the GCC installation FAQ prior to installation.
You can download gcc-4.8 from one of gnu.org's mirror sites or directly from their SVN server.
Here is an example of steps to compile from source (see here for additional details.) Note that these may vary depending on your system and preferences.
Download the source code
- Make a build directory (
mkdir gcc-build && cd gcc-build) - Download the source file:
wget http://www.netgull.com/gcc/releases/gcc-4.8.0/gcc-4.8.0.tar.bz2(adjust this command to use an appropriate mirror site. - Unzip the file (
tar -xvjf <file name>)
- Make a build directory (
Install some additional libraries (
sudo apt-get install libgmp-dev libmpfr-dev libmpc-dev libc6-dev)- Compile the source:
./gcc-4.8.0/configure --prefix=/app/gcc/4.8.0 - Run
make(This will take some time to complete. Go make some coffee, or bake some cookies. ;-)) - Install the code:
sudo make install
Once this process has completed, run the command gcc --version to verify that the installation has completed successfully. You should see something similar to the following output:
maggotbrain@foucault:~$ gcc --version
gcc (Linaro 4.8.0) 4.8.0
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
The bottom line: Thank all those folks who provide easy to install Ubuntu backports for you. Give them some of the cookies that you baked while running make. ;-)
So answer above works, at least for manual install. Though even after sudo make install, running gcc still just runs the stock version 4.6.3. Even trying gcc-4.8 or gcc-4.8.0 doesn't work. Running /app/gcc/4.8.0/bin/gcc appears to be the only thing that works for me. I mean it is kind of lame but at least it works.
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.
There's a metapackage called build-essential which should install the compiler and a bunch of related/needed packages:
sudo apt-get install build-essential
The suggestion to first do sudo apt-get update is sound, this will update package lists and all versions should match nicely.
That is, however, if you didn't modify your /etc/apt/sources.list file manually. If so, then maybe some of the changes you made or repositories you added contain conflicting packages which cause the problems you see. If that's the case, please look at the file and fix the troublesome entries. Of course, if you want help doing so, you can post your sources.list file (or any fragments in /etc/apt/sources.list.d) and maybe someone can help you fix them.
Try to keep going deeper:
sudo apt-get update
sudo apt-get install gcc-4.6-base cpp-4.6 libgomp1 libquadmath0 libc6-dev
gcc-12 is not available in ubuntu 20.04, so we need to compile it from source code, here are the steps which I borrowed from this video:
- Step 1: clone gcc source code and checkout gcc-12 branch
$ git clone https://gcc.gnu.org/git/gcc.git gcc-source
$ cd gcc-source/
$ git branch -a
$ git checkout remotes/origin/releases/gcc-12
- Step 2: make another build dir
Note this is important as running ./configure from within the source directory is not supported as documented here.
$ mkdir ../gcc-12-build
$ cd ../gcc-12-build/
$ ./../gcc-source/configure --prefix=$HOME/install/gcc-12 --enable-languages=c,c++
- Step 3: installing GCC prequisites and run configure again
The missing libraries will be shown in above ./confgiure output, search and install them one by one.
$ apt-cache search MPFR
$ sudo apt-get install libmpfrc++-dev
$ apt-cache search MPC | grep dev
$ sudo apt-get install libmpc-dev
$ apt-cache search GMP | grep dev
$ sudo apt-get install libgmp-dev
$ sudo apt-get install gcc-multilib
$ ./../gcc-source/configure --prefix=$HOME/install/gcc-12 --enable-languages=c,c++
An alternative is to run the download_prerequisites script.
$ cd ../
$ cd gcc-source/
$ ./contrib/download_prerequisites
$ ./../gcc-source/configure --prefix=$HOME/install/gcc-12 --enable-languages=c,c++
- Step 4: compile gcc-12
$ make -j16
Still flex is missing:
$ sudo apt-get install flex
$ ./../gcc-source/configure --prefix=$HOME/install/gcc-12 --enable-languages=c,c++
$ make -j16
$ make install
Another way is to use Ubuntu 22.04 where gcc-12 is available. In Ubuntu 22.04, gcc-12 can be installed with apt:
$ sudo apt install gcc-12
You can use Homebrew to install pre-built binaries. Follow instructions to install Homebrew at https://brew.sh/, then
brew install gcc for default GCC (currently 11) or brew install gcc@12 for gcc-12.
Note that it may compile missing dependencies.
You need to:
- Update and upgrade:
$ sudo apt-get update && sudo apt-get upgrade -y

2.Clean unrequired packages:
$ sudo apt autoremove -y

- Install GCC:
$ sudo apt-get install gcc -y

- Check and confirmed installed gcc version:
gcc --version

I too was trying to install gcc for my WSL.
I first tried the "sudo apt-get install gcc" to begin with (after coming here).
But then after entering "gcc --version", the terminal spat out:
Command 'gcc' not found, but can be installed with:
sudo apt install gcc
I then just tried just "sudo apt install gcc" like the terminal had suggested.
Then after again entering "gcc --version", the terminal spat out:
gcc (Ubuntu 13.2.0-23ubuntu4) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Suggesting that it worked.
To install gcc-7 on ubuntu you should use this ppa:
sudo add-apt-repository ppa:jonathonf/gcc
sudo apt-get update
Then run: apt-cache search gcc-7
sample output :
gcc-7-base - GCC, the GNU Compiler Collection (base package)
gcc-7 - GNU C compiler
gcc-7-multilib - GNU C compiler (multilib support)
gcc-7-plugin-dev - Files for GNU GCC plugin development.
gcc-7-test-results - Test results for the GCC test suite
lib32gcc-7-dev - GCC support library (32 bit development files)
libgcc-7-dev - GCC support library (development files)
gcc-7-doc - Documentation for the GNU compilers (gcc, gobjc, g++)
gcc-7-hppa64-linux-gnu - GNU C compiler (cross compiler for hppa64)
gcc-7-locales - GCC, the GNU compiler collection (native language support files)
gcc-7-source - Source of the GNU Compiler Collection
libx32gcc-7-dev - GCC support library (x32 development files)
gcc-7-offload-nvptx - GCC offloading compiler to NVPTX
lib64gcc-7-dev - GCC support library (64bit development files)
Install gcc-7:
sudo apt install gcc-7
If you need to setup a test rig with the compiler, then Fedora 26 ships with GCC 7.1.
You can also find GCC 7 in Debian 10/Buster (Debian 9 with Testing repo enabled). For Debian, you must apt-get install gcc-7 g++-7.
For completeness, its not clear which version of the Microsoft compilers support C++17 and std::byte (the reason I needed the compiler for testing).
And thanks to GAD3R for his help. The reason I avoided it was I could not establish provenance for some of the PPAs.
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++
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?