Get CUDA without sudo.
EDIT: Pip might be the best way to handle cuda versions moving ahead. I will write a post on this as soon as I can.
(One that won’t break anything when you need to change or if you do not have sudo access)
This is a guide on how CUDA can be set up in a non-intrusive way and doesn’t even need “sudo” for the most part. It is not necessarily the only way but just one that has worked perfectly well for me across environments and quite a few colleagues have adopted over a couple of years now. I would recommend either doing this or using a docker(Using display and certain things is not as straightforward). While conda exists and makes things convenient, I am against conda because we get locked into a system and become reliant on another additional package.
I am going to time myself to check how fast I can do this. So you can also be sure you can be up and running ASAP. I am installing on a laptop with an Nvidia RTX 2070 and a core i7–10750H running a clean installation of Ubuntu 20.04(It should still work for Ubuntu18.04 without a hitch). So these instructions should hold for most people. Post a comment if you face any issues.
Start Timer !!
Well, hold on a minute — Download everything you need in advance or hold your timer.
Pre-requisites and Downloads
CUDA Installation file (.runfile) — This is the way I recommend installing CUDA always. It keeps things clean and gives us control over what we are doing.
CUDA: https://developer.nvidia.com/cuda-downloads (Old versions under ‘Archive of Previous CUDA Releases’)
I shall be sticking to CUDA 10.2 for now, considering most packages are stable with it, and some versions of the dependencies that we work with rely on it.
If no prior installation Nvidia graphics driver is available on the machine, you will need to install it. Run the following command in the terminal:
sudo apt install nvidia-driver-460
You may replace the 460 with the latest version available at the time of reading. While the CUDA runfile includes a copy of the driver, I prefer keeping them separate, and the included one is usually older. Best do a restart of the machine once the driver is installed. If you do not have a graphics driver installed yet and do not have sudo access, you are out of luck. In such a case, ask your system admin to install it for you.
You may also need GCC-7 if you want to stick to Nvidia’s recommendations which you may get using:
sudo apt install -y gcc-7 g++-7
I will override this setting since it is usually never an issue working with newer versions of compilers. However, if you have a GCC version lesser than 7, definitely update using the above command. If you do not
Back to installing CUDA, timers can go back on. From the folder that you have placed the CUDA runfile into,
sudo sh cuda_10.2.89_440.33.01_linux.run --override
Remove the override flag if you have the recommended GCC version. Upon starting, we may be greeted with a warning stating that an existing package manager installation of the graphics driver was found. Unless you have a version of the graphics driver lower than required from this page, you can choose to continue.
Accept the EULA. While we may go and read the whole thing, we don’t have much of a choice but to accept, do we?
We will be making significant changes here.
- Deselect the Driver installation
- Optionally, we may also deselect the CUDA Samples and Demo Suite too. Most people never really use it. Only ‘deviceQuery’ comes in handy once in a while, though this is the exception and not the norm.
- If you do not plan to use offline documentation, go ahead and deselect it too.
Moving to options,
- Change the toolkit install path to something custom. We will call this path CUDA_PATH for this guide from here on out.
- You may choose to keep symbolic links. People consider this to be easier. I will show a way to get the same convenience without making any changes in the system paths.
- If you do not have sudo access, you will need to disable anything and all that access system paths.
- For non sudo users, the options should effectively look like
Once done with configuring options, you can go back and run Install. On completion, you will receive a summary output on the terminal:
The two lines starting with PATH and LD_LIBRARY_PATH are key and are essential to make our CUDA installation appear to programs the same as a default installation. We will call the path indicated in line PATH as BIN_PATH and in line LD_LIBRARY_PATH as LIB_PATH.
In a new file named cuda_activate.sh, add the following lines
export PATH=BIN_PATH:$PATH
export LD_LIBRARY_PATH=LIB_PATH:$LD_LIBRARY_PATH
This will ensure our specific CUDA installation precedes all others in the system, if any. The CUDA installation is done, and we now have a way to use it. To make it easier to use, you may do any of the following:
- If you want the ability to enable only when needed, you could make an alias in your ‘.bashrc’ file.
alias activateCuda="source PATH/TO/CUDA_ACTIVATE.sh"
- If you do not plan to have another CUDA installation at the moment, you can go ahead and add the following line to the end of your ‘.bashrc’ file or any other environment file that you may be using.
source PATH/TO/CUDA_ACTIVATE.sh
- If you are a python developer who needs a CUDA installation for your virtual env, you can add the source command from the above step at the end of your virtual environment activate script, which you may again make an alias for, in your bashrc.
For each CUDA version you need to be installed, you can have a separate cuda_activate.sh script and enable as you go. We can do the whole of the above guide in under 10 to 15 minutes if no issues are faced.
This guide/article has been long overdue. I had developed this installation workflow pretty much out of being lazy to manage to replace CUDA versions required by each TensorFlow version that I was using at the time. Also, the same machines were used by other people at work, which made version conflicts inevitable, compounding the issue further. However, it has become the cleanest way of managing CUDA for a few colleagues and me who have adopted this since we are primarily focused on python.
Thanks to Laxmena for the machine to complete recording the installation. I was stuck for a long time, not having a spare machine to do it on.