How to build VASP 4.6.38 using the GNU compiler

2014-07-28

This tutorial will explain how to install VASP 4.6.38 from scratch on a Linux system.

For VASP 5, please see this blogpost.

Step 0: Preparing

(VASP)[http://www.vasp.at/] relies on a couple of libraries which you have to install. You can install these system-wide (assuming you have root privileges) or locally. For the sake of this tutorial, I will consider that you do not have root privileges and install everything locally. If you want to install things system-wide though, just change the --prefix tags to something like /opt/software/version-x and perform the last make install or cp step as a root user.

Furthermore, I assume you have some basic understanding of make and have some basic software such as wget, tar, git, and grep installed. (most systems have this software installed by default)

A couple of things before I start:

  • I assume you have a valid VASP license and that you are using VASP according to that license.
  • Although it is very hard to break a Linux system as a regular (i.e. non-administrative) user, people still keep surprising me that they actually have done so. So use this guide at your own risk. If you break something, you have to suffer the consequences. This is especially true if you are going to do things as a superuser.
  • Last but not least: really carefully read this guide. If you cannot figure things out, read it again. If you run into something, remember that Google is your friend. If you are stuck at some point though and you are absolutely 100% sure that you have done everything in your power to try to resolve the issue yourself, feel free to leave a comment. But no promises! :-)

This tutorial proceeds in 6 steps:

If you run into problems, please have a look at this section.

OK, let's start! Make sure you have a sufficient amount of space available in your /home folder and start by creating a build and compile directory:


mkdir -pv $HOME/vasp/{libraries,compilation,software}
INSTALL=$HOME/vasp

Step 1: Install gcc-4.8.3 (optional)

In my previous post, I mentioned that gcc-4.8.3 gave the best performance for my type of processor. Therefore, I would like to show here how to compile your own compiler. If you don't think this is necessary or if it sounds overcomplicated to you, feel free to skip this step.

Start by grabbing the tarball of gcc-4.8.3.


mkdir -pv $INSTALL/compilation/gcc
cd $INSTALL/compilation/gcc
wget ftp://ftp.nluug.nl/mirror/languages/gcc/releases/gcc-4.8.3/gcc-4.8.3.tar.bz2
tar -xvjf gcc-4.8.3.tar.bz2

gcc-4.8.3 relies on a couple of other packages, which you can conveniently grab by executing the following commands:


cd gcc-4.8.3
./contrib/download_prerequisites

gcc needs to be compiled in a separate folder, create a separate build folder (the name is arbitrary), configure, make and make install. I assume that you have at least a quad-core machine, so we are going to compile with five threads. Feel free to change the -j5 flag to something more appropriate for your machine.


mkdir -v $INSTALL/compilation/gcc/build
cd $INSTALL/compilation/gcc/build
../gcc-4.8.3/configure --enable-languages=c,c++,fortran \
--enable-shared --enable-linker-build-id --with-system-zlib \
--without-included-gettext --enable-threads=posix \
--enable-nls --enable-clocale=gnu --enable-libstdcxx-debug \
--enable-libstdcxx-time=yes --enable-gnu-unique-object \
--enable-plugin --with-tune=generic --enable-checking=release \
--disable-multilib --prefix=$INSTALL/software/gcc/4.8.3
make -j5

To make sure that gcc-4.8.3 is going to be used for compiling the libraries and the VASP program, prepend the bin folder to the PATH like so


PATH=$INSTALL/software/gcc/4.8.3/bin:$PATH

jhpowell_at_odu_dot_edu (see the comment below) suggested to also append the lib and lib64 folders of gcc-4.8.3 to the LD_LIBRARY PATH variable, like so:


LD_LIBRARY_PATH=$INSTALL/software/gcc/4.8.3/lib:$INSTALL/software/gcc/4.8.3/lib64 :$LD_LIBRARY_PATH

to check if your gcc compiler is correctly added to the PATH, test the following


which gcc
which gfortran

it should output something like:


/home/user/vasp/software/gcc/4.8.3/bin/gcc
/home/user/vasp/software/gcc/4.8.3/bin/gfortran

Finally, it is time to test your freshly built compiler by creating a very small program:


cd $INSTALL/software
echo "int main(){return 0;}" > test.c
gcc test.c -o test
./test && echo 'Success!' || echo 'Something went wrong'

if you get the 'Success!' statement, feel free to remove the files:


rm -v test.c test

Step 2: Install OpenMPI

Download the latest tarball of the stable release.


cd $INSTALL/compilation
wget http://www.open-mpi.org/software/ompi/v1.8/downloads/openmpi-1.8.1.tar.bz2
tar -xvjf openmpi-1.8.1.tar.bz2
cd openmpi-1.8.1
./configure --prefix=$INSTALL/libraries/openmpi/1.8.1

At this point, you probably want to make sure that the proper version of gcc is going to be used. Especially if you have spend some effort and time in building one yourself. To do so, check for the OPAL_CC_ABSOLUTE and OMPI_FC_ABSOLUTE patterns in config.log like so:


grep -E "OPAL_CC_ABSOLUTE=|OMPI_FC_ABSOLUTE=" config.log

which should give you something like:


OMPI_FC_ABSOLUTE='/home/user/vasp/software/gcc/4.8.3/bin/gfortran'
OPAL_CC_ABSOLUTE='/home/user/vasp/software/gcc/4.8.3/bin/gcc'

If everything looks OK. It is time to build and install the package.

make -j5
make install -j5

Testing OpenMPI is a bit more difficult as testing GCC, moreover, it really isn't that necessary. From personal experience I found out that if OpenMPI compiles without any errors, it will run just fine.

Step 3: Install OpenBlas

In my previous post, I mentioned that OpenBLAS gave better performance than ATLAS, as such, we are going to install OpenBLAS.

Grab the latest version from Github:


cd $INSTALL/compilation
git clone https://github.com/xianyi/OpenBLAS.git --depth 1
cd OpenBLAS

We should use the non-threaded variant of OpenBLAS, as such, build the library using


make USE_THREAD=0

Finally install the library


make PREFIX=$INSTALL/libraries/openblas/1.13 install

Step 4: Install ScaLAPACK

Grab the package from the repository and extract it. ScaLAPACK reads the compilation instructions from SLmake.inc, which does not exist yet. Copy SLmake.inc.example to SLmake.inc and refer to our previously built OpenBLAS library. In the listing below, we use sed to do this for us. By adding the bin folder of our OpenMPI installation to the PATH, we will use our own mpicc compiler for building ScaLAPACK. Acter properly configuring, we can now use make to build the library.


cd $INSTALL/compilation
wget http://www.netlib.org/scalapack/scalapack-2.0.2.tgz
tar -xvzf scalapack-2.0.2.tgz
cd scalapack-2.0.2
PATH=$PATH:$INSTALL/libraries/openmpi/1.8.1/bin
OPENBLASPATH=$INSTALL/libraries/openblas/1.13/lib
cp SLmake.inc.example SLmake.inc
sed -i "s,^BLASLIB.*$,BLASLIB = ${OPENBLASPATH}/libopenblas.a,g" SLmake.inc
sed -i "s,^LAPACKLIB.*$,LAPACKLIB = ${OPENBLASPATH}/libopenblas.a,g" SLmake.inc
make

Now, we copy the freshly built libopenblas.a file to our custom installation directory.


mkdir -pv $INSTALL/libraries/scalapack/2.0.2/lib
cp -v libscalapack.a $INSTALL/libraries/scalapack/2.0.2/lib

Step 5: Building VASP 4.6.38

Finally, we are ready to build VASP. VASP is build in two steps. First, we have to build a VASP library and finally we can build a VASP executable. Start by downloading the two corresponding TAR files from the VASP repository (or get them from your system administrator).


mkdir -pv $INSTALL/compilation/vasp
# I assume that you have placed here your vasp.4.6.tar.gz and vasp.4.lib.tar.gz files
tar -xvzf vasp.4.6.tar.gz
tar -xvzf vasp.4.lib.tar.gz
cd vasp.4.lib
cp makefile.linux_gfortran Makefile

At this point, make sure that which gfortran refers to the correct version of gfortran. That is, either the one your compiled previously, or the one of your system! If you have compiled gfortran yourself and which gfortran refers to the wrong version, execute the command below:


PATH=$INSTALL/software/gcc/4.8.3/bin:$PATH

If everything is set up correct, you are ready to build the VASP library.


make

Now it is time to build the VASP executable, start by going to the source folder, copy the example makefile.linux_gfortran to Makefile and edit this file.


cd ../vasp.4.6
cp makefile.linux_gfortran Makefile
nano Makefile

We have to change a couple of directives in this file. To start, we are going to link to OpenBLAS.


# Atlas based libraries
#ATLASHOME= /usr/lib/blas/threaded-atlas
# BLAS=   -L/usr/lib/blas/atlas -lblas #!! this line has to be commented out
#BLAS=   -L$(ATLASHOME)  -lf77blas -latlas

# use specific libraries (default library path points to other libraries)
#BLAS= $(ATLASHOME)/libf77blas.a $(ATLASHOME)/libatlas.a

# use the mkl Intel libraries for p4 (www.intel.com)
#BLAS=-L/opt/intel/mkl/lib/32 -lmkl_p4  -lpthread

# LAPACK, simplest use vasp.4.lib/lapack_double
#LAPACK= ../vasp.4.lib/lapack_double.o

# use atlas optimized part of lapack
#LAPACK= ../vasp.4.lib/lapack_atlas.o  -llapack -lblas

# use the mkl Intel lapack
#LAPACK= -lmkl_lapack

#LAPACK= -L/usr/lib/lapack/atlas -llapack #!! this line has to be commented out

# Use our own OpenBLAS
LAPACK=/home/user/vasp/libraries/openblas/1.13/lib/libopenblas.a

Take care to change user to the proper folder name!

We are going to use MPI, so set the proper compiler below.


#-----------------------------------------------------------------------
# fortran linker for mpi: if you use LAM and compiled it with the options
# suggested above,  you can use the following lines
#-----------------------------------------------------------------------


FC=mpif90
FCL=$(FC)

#-----------------------------------------------------------------------
# additional options for CPP in parallel version (see also above):
# NGZhalf               charge density   reduced in Z direction
# wNGZhalf              gamma point only reduced in Z direction
# scaLAPACK             use scaLAPACK (usually slower on 100 Mbit Net)
#-----------------------------------------------------------------------

CPP    = $(CPP_) -DMPI  -DHOST=\"LinuxPgi\" \
     -Dkind8 -DNGZhalf -DCACHE_SIZE=2000 -DPGF90 -Davoidalloc -DRPROMU_DGEMV \
     -DscaLAPACK #!! note the addition here

#-----------------------------------------------------------------------
# location of SCALAPACK
# if you do not use SCALAPACK simply uncomment the line SCA
#-----------------------------------------------------------------------

BLACS=/usr/local/BLACS_lam
SCA_= /usr/local/SCALAPACK_lam

SCA= $(SCA_)/scalapack_LINUX.a $(SCA_)/pblas_LINUX.a $(SCA_)/tools_LINUX.a \
 $(BLACS)/LIB/blacsF77init_MPI-LINUX-0.a $(BLACS)/LIB/blacs_MPI-LINUX-0.a $(BLACS)/LIB/blacsF77init_MPI$

SCA=/home/user/vasp/libraries/scalapack/2.0.2/lib/libscalapack.a

#-----------------------------------------------------------------------
# libraries for mpi
#-----------------------------------------------------------------------

LIB     = -L../vasp.4.lib -ldmy  \
      ../vasp.4.lib/linpack_double.o $(LAPACK) \
      $(SCA) $(BLAS)

# FFT: only option  fftmpi.o with fft3dlib of Juergen Furthmueller

FFT3D   = fftmpi.o fftmpi_map.o fft3dlib.o

Now we are ready to build vasp:


make

Troubleshooting

If you get an error message such as:


base.f:1.1:

/* Copyright (C) 1991-2014 Free Software Foundation, Inc. 1 Error: Invalid character in name at (1)

then you have to remove the -C directive from the CPP line in the Makefile as below and run make again.


#-----------------------------------------------------------------------
# whereis CPP ?? (I need CPP, can't use gcc with proper options)
# that's the location of gcc for SUSE 5.3
# 
#  CPP_   =  /usr/lib/gcc-lib/i486-linux/2.7.2/cpp -P -C
# 
# that's probably the right line for some Red Hat distribution:
# 
#  CPP_   =  /usr/lib/gcc-lib/i386-redhat-linux/2.7.2.3/cpp -P -C
#
#  SUSE 6.X, maybe some Red Hat distributions:

#!! I removed the -C directive in the line below
CPP_ =  ./preprocess <$*.F | /usr/bin/cpp -P -traditional >$*$(SUFFIX)

So far, I have only seen this error occurring on Arch Linux. Most likely because this is a very cutting-edge distro. For Debian and Ubuntu, you should be fine. For any other distro, I am not aware.

Update (18-sep-2014)

If you encounter the error below compiling VASP


/opt/scalapack/2.0.2-gcc-4.8.3/lib/libscalapack.a(PB_Cztypeset.o): In function `PB_Cztypeset':
PB_Cztypeset.c:(.text+0x233): undefined reference to `zgeru_'
PB_Cztypeset.c:(.text+0x249): undefined reference to `zher_'
PB_Cztypeset.c:(.text+0x275): undefined reference to `zsymm_'
PB_Cztypeset.c:(.text+0x28b): undefined reference to `zsyrk_'
PB_Cztypeset.c:(.text+0x296): undefined reference to `zherk_'
PB_Cztypeset.c:(.text+0x2a1): undefined reference to `zsyr2k_'
/opt/scalapack/2.0.2-gcc-4.8.3/lib/libscalapack.a(zvvdotu.o): In function `zvvdotu_':
zvvdotu.f:(.text+0x2b): undefined reference to `zdotu_'
collect2: error: ld returned 1 exit status
Makefile:264: recipe for target 'vasp' failed
make: *** [vasp] Error 1

Then you have to change the order in which the libraries are linked, like so:


LIB     = -L../vasp.4.lib -ldmy  \
      ../vasp.4.lib/linpack_double.o \
      $(SCA) $(BLAS) $(LAPACK)

If you have questions or comments, feel free to drop a line! Like what you read? Share this page with your friends and colleagues.

Comments

Question:
What is the answer to Two + Two?
Please answer with a whole number, i.e. 2, 3, 5, 8,...
jhpowell_at_odu_dot_edu
2015-02-20 17:44:15
Just wanted to add to your GCC-4.8.3 section. You also need to modify the $LD_LIBRARY_PATH to include $INSTALL/software/gcc/4.8.3/lib and $INSTALL/software/gcc/4.8.3/lib64 of openmpi fails the gfortran check.

$LD_LIBRARY_PATH=$INSTALL/software/gcc/4.8.3/lib:$INSTALL/software/gcc/4.8.3/lib64 :$LD_LIBRARY_PATH
Question:
What is the answer to Three + Four?
Please answer with a whole number, i.e. 2, 3, 5, 8,...
ivo_at_ivofilot_dot_nl
2015-02-22 10:43:07
Thanks for your comment. I tested the above on a Debian system and I didn't alter the LD_LIBRARY_PATH and OpenMPI compiled without any problem. Nevertheless, your suggestion makes perfect sense so I have added it to the tutorial. Many thanks for contributing!
Question:
What is the answer to Two + Nine?
Please answer with a whole number, i.e. 2, 3, 5, 8,...
c00jsh00_at_nchc_dot_narl_dot_org_dot_tw
2015-05-22 15:27:24
Have you tried to build vasp 5.3.5 with GCC 4.9.2? I tried several times but failed to compile the program. Perhaps you have a better experience?
Question:
What is the answer to Six + One?
Please answer with a whole number, i.e. 2, 3, 5, 8,...
ivo_at_ivofilot_dot_nl
2015-05-29 11:34:20
I haven't done so myself. I used the Intel compiler for VASP5. I also believe that the people from Vienna are now recommending this instead of using the GNU compilers.

Peter Larsson has published a tutorial on how to compile VASP5 for Ubuntu. Perhaps his instructions can help you out:
https://www.nsc.liu.se/~pla/blog/2013/11/05/compile-vasp-on-ubuntu/
Question:
What is the answer to Nine + One?
Please answer with a whole number, i.e. 2, 3, 5, 8,...
shijun_liang_at_mymail_dot_sutd_dot_edu_dot_sg
2015-06-23 13:22:16
I tried to follow the instructions by Perter Larsson, however, i failed to compile the scalapack and vasp.5.3 version on my Ubuntu. May i ask whether you have installed VASP 5.3 on your Ubuntu system?
Question:
What is the answer to Nine + Eight?
Please answer with a whole number, i.e. 2, 3, 5, 8,...
ivo_at_ivofilot_dot_nl
2015-06-26 11:03:35
Like I said in my post, I did not do so myself. However, because you mentioned that you were not able to create a successful compilation using the instructions of Peter Larsson, I have tested building VASP on Ubuntu myself using his blog post. I can confirm that his instructions are absolutely 100% correct.

Could you perhaps explain what kind of errors are you are encountering? Feel free to send me an e-mail about it.
Question:
What is the answer to Three + Two?
Please answer with a whole number, i.e. 2, 3, 5, 8,...
ivo_at_ivofilot_dot_nl
2015-06-28 21:45:37
To add to my previous comment. I have written a small post with some more detailed instructions on how I compiled VASP5 for Ubuntu.

http://www.ivofilot.nl/posts/view/28/How+to+build+VASP+5.3.5+using+the+GNU+compiler+on+Linux+Ubuntu

Maybe this post will help you out further. Still, me previous offer stands. Feel free to send me an e-mail with the errors you are encountering and I can have a look at them.
Question:
What is the answer to Seven + One?
Please answer with a whole number, i.e. 2, 3, 5, 8,...
hwang163_at_illinois_dot_edu
2016-05-20 08:29:06
Hi Ivo,
Following your instructions, i compiled the vasp. When I run it using 1 core, it works very well. But If I use more than 1 cores, then I received the error as
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Do you know how I can get rid of this error?
Thank you very much!
Haoran
Question:
What is the answer to One + Two?
Please answer with a whole number, i.e. 2, 3, 5, 8,...
ivo_at_ivofilot_dot_nl
2016-06-30 08:15:41
I am not sure. Without any further details, it is a bit of a guess. What comes to mind is that it might be related to the OpenMPI interface. Did you for instance use the same OpenMPI interface for compilation as you use for running the application?
Question:
What is the answer to Five + Eight?
Please answer with a whole number, i.e. 2, 3, 5, 8,...