102

I am using CentOS 7.2

When I use yum groupinstall "Development Tools", gcc version is 4.8.5, like this:

enter image description here

I would like to install gcc 5.3

How to approach this with yum?

HDJEMAI
  • 7,766
  • 41
  • 60
  • 81
sunshine
  • 1,785
  • 3
  • 14
  • 19

5 Answers5

203

Update:
Often people want the most recent version of gcc, and devtoolset is being kept up-to-date, so maybe you want devtoolset-N where N={4,5,6,7...}, check yum for the latest available on your system). Updated the cmds below for N=7.

There is a package for gcc-7.2.1 for devtoolset-7 as an example. First you need to enable the Software Collections, then it's available in devtoolset-7:

sudo yum install centos-release-scl
sudo yum install devtoolset-7-gcc*
scl enable devtoolset-7 bash
which gcc
gcc --version
tesch1
  • 2,416
  • 1
  • 11
  • 11
  • 2
    the gcc version in devtoolset has in the mean time been bumped to 5.3.1 – tesch1 Jan 25 '17 at 12:42
  • 4
    Not only that, but you can install devtoolset-6 to get 6.2.1 – Rahly May 16 '17 at 05:18
  • Thanks, you saved my day! Also, I see that my /usr/bin/c++ is still linked to gcc 4.8.5, so I had to relink to gcc 6: `ln -sf /opt/rh/devtoolset-6/root/usr/bin/g++ /usr/bin/c++` – Maksym Ganenko Aug 31 '17 at 15:12
  • 5
    changing the symlink isn't the best thing to do, since it's probably owned/managed by another package that could get upgraded. It's better to activate it in your login, or in the system-wide login script with `scl enable devtoolset-6 bash` or `. /opt/rh/devtoolset-6/bin/enable` [updated link to software tools docs](https://www.softwarecollections.org/en/scls/rhscl/devtoolset-6/) – tesch1 Sep 01 '17 at 16:34
  • 1
    Running `yum install devtoolset-4-binutils` might be helpful as well to get complete GCC toolchain. – scrutari Sep 26 '17 at 17:26
  • 5
    I used `source scl_source enable devtoolset-4` to switch my current gcc with the one installed by `devtoolset-4` – W.F. Jan 13 '18 at 13:05
  • 1
    Use devtoolset-6, because [devtoolset-4 is End of life](https://wiki.centos.org/SpecialInterestGroup/SCLo/CollectionsList). – Marián Černý Jan 17 '18 at 04:27
  • `devtoolset-6` now has `gcc 6.3.1` – TriskalJM Jan 31 '18 at 17:05
  • I see 4,6,7 on mine. Why no 5? – bshea Jul 09 '18 at 16:52
  • Just do a `yum install devtoolset-8` without anything after that. The devtoolset-N packages are meta packages that install the default packages and pull in the necessary dependencies – Rob T. Mar 26 '19 at 22:05
  • this answer doesn't work any more, it installs gcc 7 – Stepan Yakovenko Jul 03 '19 at 16:06
  • 1
    This answer still works, it's up to you to choose the right version of devtoolset to install. – HDJEMAI Jul 04 '19 at 03:54
  • How can I use g++7 as default, when I open a new terminal, it is still v4.8.5 – Shixiang Wang Sep 20 '19 at 07:27
  • you need to `scl enable devtoolset-7 bash` every time you open a new shell, or add it to your *rc – tesch1 Sep 24 '19 at 06:20
  • devtoolset-5 does not exists on centos7 hence the answer does not apply to the op question. – Fafaman May 28 '20 at 18:05
  • I installed devtoolset-9 but this does nothing at all... /usr/lib64/libstdc++.so.6 is still the ancient version. The libstdc++.so inside devtoolset-9 is just a few characters that links back to the useless /usr/lib64/libstdc++.so.6. Turns out you have to use podman to do anything with c++ in devtoolset, but I can't because I'm already operating inside a docker container and so podman can't do jack. So this whole piece of crap OS is useless. – scaly Jun 26 '20 at 04:48
77

Update: Installing latest version of gcc 9: (gcc 9.3.0) - released March 12, 2020:

Same method can be applied to gcc 10 (gcc 10.1.0) - released May 7, 2020

Download file: gcc-9.3.0.tar.gz or gcc-10.1.0.tar.gz

Compile and install:

//required libraries: (some may already have been installed)
dnf install libmpc-devel mpfr-devel gmp-devel

//if dnf install libmpc-devel is not working try:
dnf --enablerepo=PowerTools install libmpc-devel

//install zlib
dnf install zlib-devel*

./configure --with-system-zlib --disable-multilib --enable-languages=c,c++

make -j 8 <== this may take around an hour or more to finish
              (depending on your cpu speed)

make install

Tested under CentOS 7.8.2003 for gcc 9.3 and gcc 10.1

Tested under CentOS 8.1.1911 for gcc 10.1 (may take more time to compile)

Results: gcc/g++ 9.3.0/10.1.0

enter image description here enter image description here

Installing gcc 7.4 (gcc 7.4.0) - released December 6, 2018:

Download file: https://ftp.gnu.org/gnu/gcc/gcc-7.4.0/gcc-7.4.0.tar.gz

Compile and install:

//required libraries:
yum install libmpc-devel mpfr-devel gmp-devel

./configure --with-system-zlib --disable-multilib --enable-languages=c,c++

make -j 8 <== this may take around 50 minutes or less to finish with 8 threads
              (depending on your cpu speed)


make install

Result:

enter image description here

Notes:

1. This Stack Overflow answer will help to see how to verify the downloaded source file.

2. Use the option --prefix to install gcc to another directory other than the default one. The toplevel installation directory defaults to /usr/local. Read about gcc installation options

HDJEMAI
  • 7,766
  • 41
  • 60
  • 81
  • Why download from ftp.mirrorservice.org rather than ftp.gnu.org? You should also download and verify the corresponding `.sig` file (the pgp signature). – Keith Thompson May 21 '16 at 19:09
  • Configuring as shown without --prefix will install in /usr/local/ which may or may not come ahead of your original gcc installation on PATH – tim18 May 21 '16 at 21:27
  • 3
    `make -j $(nproc)` – Chepe Questn Dec 05 '17 at 15:47
  • 2
    This took me over 4 hours to compile and more than 6gb of HDD space. – iDevFS Dec 20 '17 at 09:04
  • As tim18 suggested, is it safe to run `make && make install` without `--prefix`? If do so, everything is installed into `/usr/` not even `/usr/local`, check the `config.log`: `Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info ...` – Celdor Jun 16 '18 at 10:43
  • Note that you will not be able to put binaries compiled with a gcc compiled in this way into an RPM (at least with the default RPM environment). I needed to add `--enable-linker-build-id` before that would work. – Daniel Schepler May 08 '19 at 20:03
  • this answers other question – Stepan Yakovenko Jul 03 '19 at 16:07
  • @StepanYakovenko: My answer includes installing gcc 5.3 but I deleted it recently because no one will search to install an old gcc version, and this method still work for that if some one needs it. – HDJEMAI Jul 04 '19 at 03:51
  • use ftp://ftp.gnu.org/pub/gnu/gcc/gcc-5.3.0/gcc-5.3.0.tar.bz2 if you want gcc 5.3 – HDJEMAI Jul 04 '19 at 04:00
  • @HDJEMAI - Could you please help me on https://stackoverflow.com/questions/61831832/angular-material-group-and-expand-rows-based-on-id-column – Ask_SO May 16 '20 at 04:34
10

You can use the centos-sclo-rh-testing repo to install GCC v7 without having to compile it forever, also enable V7 by default and let you switch between different versions if required.

sudo yum install -y yum-utils centos-release-scl;
sudo yum -y --enablerepo=centos-sclo-rh-testing install devtoolset-7-gcc;
echo "source /opt/rh/devtoolset-7/enable" | sudo tee -a /etc/profile;
source /opt/rh/devtoolset-7/enable;
gcc --version;
user1855153
  • 539
  • 4
  • 14
  • While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context. – Robert Columbia Dec 29 '18 at 11:12
  • Sadly this solution does not update the `/usr/lib64/libstdc++.so` file to have the newer symbols. – scaly Jun 25 '20 at 21:20
7

The best approach to use yum and update your devtoolset is to utilize the CentOS SCLo RH Testing repository.

yum install centos-release-scl-rh
yum --enablerepo=centos-sclo-rh-testing install devtoolset-7-gcc devtoolset-7-gcc-c++

Many additional packages are also available, to see them all

yum --enablerepo=centos-sclo-rh-testing list devtoolset-7*

You can use this method to install any dev tool version, just swap the 7 for your desired version. devtoolset-6-gcc, devtoolset-5-gcc etc.

Teshy
  • 316
  • 3
  • 8
-13

Command to install GCC and Development Tools on a CentOS / RHEL 7 server

Type the following yum command as root user: yum group install "Development Tools"

OR sudo yum group install "Development Tools

If above command failed, try: yum groupinstall "Development Tools

CaptJak
  • 3,542
  • 1
  • 25
  • 48