0

I have different versions of CUDA installed in /usr/local/cuda-... and I need some way to select the right version for different projects.

I tried

export CUDA_HOME=/usr/local/cuda-10.0
export PATH=\$CUDA_HOME/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=\$CUDA_HOME/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

but it seems like variables in PATH don't get expanded.

My currently best idea is something like this:

BASEPATH=$PATH:$HOME/installed/pycharm-community-2020.2/bin
LIBBASEPATH=$LD_LIBRARY_PATH

function env100() { CUDA_HOME=/usr/local/cuda-10.0 setpaths }

function env101() { CUDA_HOME=/usr/local/cuda-10.1 setpaths }

function setpaths() { export PATH=$BASEPATH:$CUDA_HOME/bin export LD_LIBRARY_PATH=$LIBBASEPATH:$CUDA_HOME/lib64 }

but I cannot imagine that this is the easiest way to do what I want. And I haven't used it long enough to know all the weird ways in which it will explode, either. (Edit: First thing to explode: Python virtualenvs.)

Related: I am trying to do something similar to this question: What's the right way to set "dynamic" PATH in bash? (for android sdk)

So, let's hear your recommendations!

Sarien
  • 143

1 Answers1

0

Without more information about what you're trying to do it's hard to recommend something:

  • Path expansion is done like this: PATH=$PATH:$CUDA_HOME/bin:/foo/otherpath

  • You could create more users, setup the specific PATH for each user environment in .bashrc (if using bash) and export it. To switch between users run su username run exit to switch back. Run whoami for orientation. Run echo $SHLVL to see in which shell level you're in. In the first shell the SHLVL counter var starts with 1, each su adds one to it, exit subtracts one from it.

  • Setting the PATH var in a shell script without exporting it. This will only change the PATH for the subshell the script is running in.

test.sh

#!/bin/bash
PATH=/home/foo
echo $PATH

run ./test.sh;echo $PATH

  • If you're compiling code using make, you could add/change the path to a Makefile
Michael D.
  • 2,830