Linux – How to install a specific version of GCC in Kali Linux

aptitudedpkggcckali-linuxpackage-management

This question appears to be addressed properly in several questions and other
places easily found with Google, but I don't find the solution satisfactory
for the reasons explained below. But just for completion, I've included some
relevant links:

https://askubuntu.com/questions/138284/how-to-downgrade-a-package-via-apt-get
https://askubuntu.com/questions/428772/how-to-install-specific-version-of-some-package/428778
https://askubuntu.com/questions/26498/choose-gcc-and-g-version

And others.

However, this question is regarding installing a very specific version of GCC in Kali Linux, which does not appear readily available as a specific package. In particular, the question is regarding how to install version 6.3.0, as I need this version to compile a particular program: https://www.reddit.com/r/Monero/comments/6d0ah8/xmrig_miner_new_release/

(as a bonus question, if there is a more sane way to fix this particular
issue without using a different version of GCC, feel free to answer, but I believe this question is general and I would like to know how to do it regardless of how to make the aforementioned program link correctly)

The versions which are available to install of any package, e.g. gcc, can be
determined with:

apt-cache showpkg gcc

Which will list the available versions under "versions:", e.g.

4:7.2.0-1d1
4:4.9.2-2

Installation is then as simple as issuing

apt-get install gcc:4:4.9.2-2

This will install the older version 4:4.9.2-2, by simply (I believe)
overwriting the 7.2.0-1d1 install, if present.

To get version 4:4.9.2-2 available at all, I had to add deb
http://old.kali.org/kali sana main non-free contrib
to my
/etc/apt/sources.list file and then run apt-get update.

However, what if the version I need is not listed?

I've been experimenting with various sources, e.g. those found here:
http://snapshot.debian.org/ and at various other questions and websites from Google searches.

Most of them give me ignore or errors, e.g. as follows

Ign:3 http://snapshot.debian.org/archive/debian/20091004T111800Z lenny InRelease

Even if this would work, it seems to be a very bad approach to get a
particular version installed, as adding some arbitrary source might not have the particular version I want.

If I search on snapshot.debian.org for gcc, I get only very old versions: http://snapshot.debian.org/package/gcc/

I eventually became frustrated with this approach and compiled GCC 6.3.0 from the source tarball. The compilation was successful, but then I'm faced with how to install it. I'm cautious about running make install as I fear it will tamper with apt and dpkg and possibly break the system.

Instead, I attempted to run it from the build directory, directly. I tried to simply add the build directory as the first entry in my PATH, which didn't work. Then, I attempted to rename /usr/bin/gcc and do a symlink from /usr/bin/gcc to where my gcc-6.3.0 executable lives. This presents the following problem:

cc: error trying to exec 'cc1': execvp: No such file or directory, which

This was fixed with another entry in my PATH.

Then, I get this error:

/usr/include/stdio.h:34:21: fatal error: stddef.h: No such file or directory

Which I assume is because of a missing entry in /usr/lib/gcc/x86_64-linux-gnu. I tried to make a symlink from 6 to 6.3.0, but this wasn't sufficient. I also tried to actually copy everything with cp -R, same result.

This should be a 64-bit program, but I also considered the same for
/usr/lib/gcc/i686-linux-gnu.

I'm sure I could start doing strace to see where it attempts to open the files from, read log files, read the source, and eventually I imagine I'd be able to figure out how to hack together a poorly conceived solution. But it would be nice if someone could tell me how to do this in a sane manner.

Best Answer

How to install a specific version of GCC in Kali Linux?

GCC 6 is available on kali linux it can be installed as follow :

apt install g++-6 gcc-6

To switch between gcc6 and gcc7

update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 1 --slave /usr/bin/g++ g++ /usr/bin/g++-7
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 2 --slave /usr/bin/g++ g++ /usr/bin/g++-6
update-alternatives --config gcc

sample output:

There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path            Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-6   2         auto mode
  1            /usr/bin/gcc-6   2         manual mode
  2            /usr/bin/gcc-7   1         manual mode

Press <enter> to keep the current choice[*], or type selection number:

Select your default gcc version.

on 2017-08-05 the gcc-6 version is upgraded from 6.3.0 to 6.4.0 .

Installing xmrig following the build's instructions.

apt-get install git build-essential cmake libuv1-dev libmicrohttpd-dev
git clone https://github.com/xmrig/xmrig.git
cd xmrig
mkdir build
cd build
cmake ..
make

Building a specific gcc version 6.3.0

Download the tarball from the closest mirror : GCC Releases

wget https://ftp.gnu.org/gnu/gcc/gcc-6.3.0/gcc-6.3.0.tar.bz2
tar xvf gcc-6.3.0.tar.gz
cd gcc-6.3.0
apt build-dep gcc
./contrib/download_prerequisites
cd ..
mkdir objdir
cd objdir
$PWD/../gcc-6.3.0/configure --prefix=/usr/bin/gcc-6.3 --enable-languages=c,c++,fortran,go --disable-multilib
make -j 8
make install

Add gcc-6.3 to update-alternatives

Important : The --disable-multilib option is required to configure and build gcc for the current architecture.

GCC WIKI : Installing GCC