Search code examples
dockergccrustdebian

Installing gcc13 and g++13 in Debian bookworm rust docker image


I am trying to install gcc13 and g++13 in the following Rust docker image.

# Builder
FROM rust:1-bookworm AS builder

RUN apt update
RUN apt install software-properties-common -y
RUN apt-get install python3-launchpadlib -y
RUN add-apt-repository ppa:ubuntu-toolchain-r/test -y
RUN apt update
RUN apt install -y clang g++-13 gcc-13

Since gcc13 is not natively available in debian bookworm, I already tried using method like using ppa:ubuntu-toolchain-r/test as describe here.

However, I'm still getting the error E: Unable to locate package g++-13 and E: Unable to locate package g++-13. Any ideas what I have done wrong?


Solution

  • Option 1: Alpine

    You can use an Alpine base image.

    FROM rust:1-alpine
    
    RUN apk add g++
    

    In this case you'll immediately have access to GCC 13.

    / # gcc -v
    gcc version 13.2.1 20231014 (Alpine 13.2.1_git20231014) 
    / # g++ -v
    gcc version 13.2.1 20231014 (Alpine 13.2.1_git20231014)
    

    Option 2: Install onto Debian

    If you really want an image based on Debian Bookworm then you can install GCC 13 from source. It's a bit of a slog because the build takes a while.

    FROM rust:1-bookworm AS builder
    
    RUN apt-get update && \
        apt-get install -y \
            build-essential \
            wget \
            libgmp-dev \
            libmpfr-dev \
            libmpc-dev
    
    RUN wget -q https://ftp.gnu.org/gnu/gcc/gcc-13.2.0/gcc-13.2.0.tar.gz
    RUN tar -xf gcc-13.2.0.tar.gz && \
        cd gcc-13.2.0 && \
        ./contrib/download_prerequisites
    
    # This will cross-compile using the existing GCC on the image.
    RUN cd gcc-13.2.0 && \
        ./configure --disable-multilib --enable-languages=c,c++ && \
        make -j$(nproc) && \
        make install
    
    RUN update-alternatives \
            --install /usr/bin/gcc gcc /usr/local/bin/gcc 60 \
            --slave /usr/bin/g++ g++ /usr/local/bin/g++ && \
        rm -rf gcc-13.2.0.tar.gz gcc-13.2.0 && \
        apt purge -y gcc cpp