Update/Install GCC in Ubuntu

1. Through repository

# Add the Toolchain PPA
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update

# Install GCC 14
sudo apt install gcc-14 g++-14

# Verify Installation
gcc-14 --version

# Switch between GCC Versions
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 60 --slave /usr/bin/g++ g++ /usr/bin/g++-14
# Configure the default version of GCC to use:
sudo update-alternatives --config gcc
# Follow the on-screen instructions to choose GCC 14 as the default.

#5. Check the GCC Version
gcc --version

2. Build from source code

If the repository doesn't contain the desired version, GCC can be built from source code.

git clone git://gcc.gnu.org/git/gcc.git
cd gcc
mkdir build
cd build
make -j$(nproc)
sudo make install

This installs the latest version of GCC in the main branch.

$ which gcc
/usr/local/bin/gcc
$ gcc --version
gcc (GCC) 15.0.0 20241008 (experimental)
Copyright (C) 2024 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.
  • To install another version of gcc, e.g., GCC 14, either checkout the version or get the source from the website:
wget https://ftp.gnu.org/gnu/gcc/gcc-14.0.0/gcc-14.0.0.tar.gz

And follow the same build process as aforementioned.

  • To switch between GCC 14 and GCC 15:
# Add both versions to update-alternatives:
sudo update-alternatives --install /usr/bin/gcc gcc /opt/gcc-14/bin/gcc 14
sudo update-alternatives --install /usr/bin/gcc gcc /usr/local/bin/gcc 15

# Select which version to use
sudo update-alternatives --config gcc
# Follow the instructions

3. Uninstall a manually installed GCC

sudo rm -rf /usr/local/bin/gcc*
sudo rm -rf /usr/local/lib/gcc*
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容