Problem: When executing a CUDA program under Linux, you get the following error:
error while loading shared libraries: libcudart.so.2: cannot open shared object file: No such file or directory
Solution: You need to add the path to the CUDA libraries to your $LD_LIBRARY_PATH environment variable.

Explanation:
When a CUDA program is executed, it needs to dynamically link to the CUDA runtime libraries. By default, these libraries are located in the /usr/local/cuda/lib directory. When searching for these libraries, the operating system looks in directories specified in the $LD_LIBRARY_PATH environment variable. If the CUDA library directory is not specified here, the program will fail with the error shown above.

There are two solutions (these assume you are using the bash shell, which is the default CS Department shell):
  1. Run the following command:
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib
    This change is not persistent and will need to be re-run each time you log in.

  2. Edit your .profile file (located at ~/.profile). Find the line that sets the $LD_LIBRARY_PATH variable, which should look similar to the following:
    LD_LIBRARY_PATH="/usr/lib:/usr/openwin/lib:/usr/dt/lib:/X11.6/lib:/X11.5/lib:/uva/lib:/gnu/lib"
    Modify that line to add the path to the CUDA libraries:
    LD_LIBRARY_PATH="/usr/lib:/usr/openwin/lib:/usr/dt/lib:/X11.6/lib:/X11.5/lib:/uva/lib:/gnu/lib:/usr/local/cuda/lib"
    After editing the file, you either need to log out and log back in or run the following command:
    source ~/.profile
    This solution is persistent and only needs to be performed once.