skip to Main Content

In order to use the C++17 include <filesystem> I need gcc-9 package in my centos 7 docker.

By default centos:7 will install gcc 4.8.5 from the regular distro repo.

docker run --rm -it centos:7
# yum install -y gcc
# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)

Is there any way to easily install a gcc-9 (or later) package (i.e. not build it from source)

Thanks !

note: need gcc-9 to have good C++17 <filesystem> support.
GCC 9 Release note:

Using the types and functions in <filesystem> does not require linking with -lstdc++fs now.

src: https://gcc.gnu.org/gcc-9/changes.html

2

Answers


  1. Chosen as BEST ANSWER

    What I have so far:

    cat Dockerfile

    FROM centos:7 AS env
    
    RUN yum update -y
    RUN yum install -y centos-release-scl
    RUN yum install -y devtoolset-9
    
    RUN echo "source /opt/rh/devtoolset-9/enable" >> /etc/bashrc
    SHELL ["/bin/bash", "--login", "-c"]
    RUN gcc --version
    

    So you must:

    1. Add the source stuff in a bashrc
      note: On Centos it's /etc/bashrc while on ubuntu it's /etc/bash.bashrc

    2. Update the docker default shell to be bash AND to "load" the bashrc using --login

    Output

    docker build .
    Sending build context to Docker daemon  4.096kB
    Step 1/32 : FROM centos:7 AS env
     ---> 8652b9f0cb4c
    Step 2/32 : RUN yum update -y
     ---> Using cache
     ---> a2bb269cd8dc
    Step 3/32 : RUN yum install -y centos-release-scl
     ---> Using cache
     ---> 1184e26c71cf
    Step 4/32 : RUN yum install -y devtoolset-9
     ---> Using cache
     ---> e678665d2a4e
    Step 5/32 : RUN echo "source /opt/rh/devtoolset-9/enable" >> /etc/bashrc
     ---> Using cache
     ---> fe1745d4ca87
    Step 6/32 : SHELL ["/bin/bash", "--login", "-c"]
     ---> Running in 2dd7955f4487
    Removing intermediate container 2dd7955f4487
     ---> 3cf4835bf680
    Step 7/32 : RUN gcc --version
     ---> Running in b5de3266d607
    gcc (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2)
    Copyright (C) 2019 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.
     ...
    

    What won't work

    Test 1

    RUN scl enable devtoolset-9 bash
    RUN gcc --version | head -1
    

    each RUN is a new shell so the sub-bash is lost on the second line.

    Test 2

    RUN source /opt/rh/devtoolset-9/enable && gcc --version | head -1
    RUN gcc --version | head -1
    

    Here again the source is only for the first RUN shell command but will be lost...

    Test 3

    This may work but with potential unexpected behaviour

    ENV PATH=/opt/rh/devtoolset-9/root/bin:$PATH
    RUN gcc --version | head -1
    

    here we only "fix" the PATH variable but if you look at the /opt/rh/devtoolset-9/enable script there is so more to do than only updating the PATH...


  2. You may give it a try using the below steps if that may help:
    Download the latest package from http://ftp.gnu.org/gnu/gcc/gcc-9.2.0/

    wget http://ftp.gnu.org/gnu/gcc/gcc-9.2.0/gcc-9.2.0.tar.gz
    

    Extract the files using the steps below:

    tar -xzvf gcc-9.20.tar.gz
    cd gcc-9.2.0
    

    Build a configuration using the below,

    ./configure
    

    Compile the installation using make and then make install.

    make 
    make install 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search