1

In order to setup CUDA 9.1, I read it is convenient to add its installation folder to PATH and LD_LIBRARY_PATH, as:

PATH="/usr/local/cuda-9.1/bin:$PATH"
LD_LIBRARY_PATH="/usr/local/cuda-9.1/lib64:$LD_LIBRARY_PATH"

Following this and this SE answers I tried to edit my .profile file adding the last lines as below.

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin directories
PATH="$HOME/bin:$HOME/.local/bin:$PATH"

# settings for CUDA
PATH="/usr/local/cuda-9.1/bin:$PATH"
LD_LIBRARY_PATH="/usr/local/cuda-9.1/lib64:$LD_LIBRARY_PATH"

With .profile as above, I can open a terminal and run echo $PATH to see the instruction worked, as PATH now contains the folder I indicated. However, for some reason this doesn't work for LD_LIBRARY_PATH.

I guessed the problem might have been that LD_LIBRARY_PATH did not exist before, so I attempted to modify .profile with the slightly different code below at the last 2 lines.

PATH="/usr/local/cuda-9.1/bin:$PATH"
export LD_LIBRARY_PATH=/usr/local/cuda-9.1/lib64

Again, however, unsuccessfully.

Noticing the first warning at the beginning of .profile, I checked whether I had a ~/.bash_profile or a ~/.bash_login files. They do not exist, and in any case they would not explain how my PATH gets successfully updated.

Doing some research, I stumbled upon this other answer, which explains that .profile is not necessarily executed when I open a terminal. However, again, how can I explain that PATH is updated?

What could be the problem? Is there something wrong with my syntax?

EDIT:

I tried to log off and on after I changed the .profile to contain

PATH="/usr/local/cuda-9.1/bin:$PATH"
export LD_LIBRARY_PATH=/usr/local/cuda-9.1/lib64

and it now works. If I enter a terminal and type echo $LD_LIBRARY_PATH I finally see it. I still do not understand why the first version of my instruction list did not work...

dessert
  • 39,982
raggot
  • 221

2 Answers2

2

The reason why PATH works without export is that it is set as an environmental variable before ~/.profile is run. To change an existing environment variable,

VAR=foo

is sufficient.

To add a variable to the environment, you need to do

export NEWVAR=bar

Please see EnvironmentVariables for further reading on the topic.

Gunnar Hjalmarsson
  • 33,540
  • 3
  • 64
  • 94
  • The awkward thing is that if I open a terminal and write VAR=x it actually does work. My assumption has been that anything working on terminal would have worked in .profile. Do you know the reason there's a mismatch? Perhaps because terminal variables are 'temporary'? – raggot Feb 21 '18 at 08:38
  • @raggot: Then you set a shell variable, which is not the same thing. Please study the document I linked to. – Gunnar Hjalmarsson Feb 21 '18 at 22:24
0

I think your solution is self-explanatory, the export keyword did the trick and is requird to "export" environmental variables for use. You can export shell variables using the export command.

To see the list of exported variables run export -p

See man bash:

export [-fn] [name[=word]] ...
export -p
           The  supplied  names are marked for automatic export to the environment of subsequently executed commands.  If the -f option is given, the names
           refer to functions.  If no names are given, or if the -p option is supplied, a list of names of all  exported  variables  is  printed.   The  -n
           option  causes  the  export property to be removed from each name.  If a variable name is followed by =word, the value of the variable is set to
           word.  export returns an exit status of 0 unless an invalid option is encountered, one of the names is not a valid shell variable name, or -f is
           supplied with a name that is not a function.
George Udosen
  • 36,677