Sometimes one may find the version of the default gcc on CentOS might be out of date. However, without root privelige, the installation of the gcc will be painful.
Main Steps
First, you need to download the source from a gcc mirror. For instance, you can go here and choose a gcc version whatever you like. I would recommend gcc-7.4.0 (released 2018-12-06) since it supoorts most common C++ standards. After donwloading the source, unpack it, and cd into the directory where your version of gcc was unpacked. Then, enter the following commands. You might have to replace all instances of gcc-7.4.0 with your gcc version. Also, this script will install gcc in your ~/software
directory. If you want it installed somewhere else, change the prefix option accordingly. The whole step may take around 4 hours but you can allow multiprocessing in the building steps to shorten the duration.
1 | $ cd $HOME # pwd: ~ |
If you do not have all the prerequisites packages and libraries on your system or you are not sure about it, GCC can help you download and install them through download_prerequisites
.
1 | $ ./contrib/download_prerequisites |
After download all the prerequisites we can head to the configuration step.
1 | $ cd ../software # pwd: ~/software |
The --prefix
parameters specifies the installation path. The --disable-multilib
parameter will disable building 32-bit support on 64-bit systems. The --enable-languages
parameter identifies which languages to build. You can modify this command to remove undesired language. Potential languages you can choose to build or to remove are c
, c++
, fortran
, go
, objc
(Objective-C) and obj-c++
(Objective-C++). Usually we only need c,c++,fortran,go
these four.
Once the configuration is complete we can go to the building and installing step. The make
command could take around 3 hours, but you can use the parameter -j[n]
to allow multiprocessing. With -j60
it will take less than half hour.
1 | # Use the parameter -j[n] to allow multiprocessing |
Optionally you can create a direcrtory to store the resource file.
1 | $ cd $HOME # pwd: ~ |
After make and make install you need to set the enviornment in your .bashrc
file.
1 | $ vi ~/.bashrc |
Add following lines into your .bashrc
file with the insert mode of your vim editor:
1 | $ export PATH=$HOME/software/gcc-7.4.0/bin:$PATH |
Save and exit your .bashrc
file. Do following commands to check if your gcc is successfully installed.
1 | $ source ~/.bashrc |
If it is successfully installed it should print following message:
1 | g++ (GCC) 7.4.0 |
Now gcc and g++ are successfully installed on your own server.
Troubleshooting
- Sometimes if you do
make
ormake install
in gcc download directory directly may cause error, so a better way is to create a new folder calledgcctemp
, and do the congfiguration, building and installation step in this temporary folder. - Remember to unset
LIBRARY_PATH
andLD_LIBRARY_PATH
before configuration, or it will raiseLIBRARY_PATH shouldn't contain the current directory when building gcc
ERROR. - In the configuration step, if you want to refer your
root
path in--prefix
parameter you should use$HOME
instead of~
, or it will cause error. - If you are installing gcc on a 64-bit system remember to add –disable-multilib flag, or it will cause error.
- You need to add
fortran
in your--enable-languages
parameter if you want to install R in the future, or it will cause error. - For the
-j[n]
parameter in themake
step, do not letn
greater than the processor number you have in your system, or it may cause erorr. You can uselscpu
command to check how many processors you have in your system.