Making a flawless ML env. with Tensorflow 2 and CUDA 10.1 on Ubuntu 20.04 with dual boot 2021 | by Tajinder Singh Sondhi | Towards Data Science

As simple as I hoped it to be, after searching through various articles on the internet... it turned out it was not. Especially if your PC is dual boot with Windows 10.
So after nights of struggles, seeing red errors and hearing installation failure beeps... I made it! And here’s the summary of my path to success!
Step 1: The ‘Dual Boot’ installation and BIOS adjustments.
Step 2: Checking Python3 and GCC.
Step 3: Tidy up! (optional — skip if fresh installation)
Step 4: Installation! (the most awaited)
Trust me this is a crucial step to ensure the OS and BIOS settings are fine so you don’t have to go through the trouble of uninstalling and re-installing stuff again and again.
‘Dual Boot’ installation:
For Ubuntu 20.04 dual boot installation, I followed this video with slight changes. I had Windows 10 pre-installed. I have a different hardware setup for my PC from the video, so I configured disk partitions for installations according to my needs. At 9 mins in that video, I had checked the “Install third-party software for graphics and Wi-Fi hardware and additional media formats” checkbox (optional) while installation. Once done you should see the GRUB Boot Loader with Ubuntu and Windows Boot Manager options like this as shown in that video.
BIOS adjustments:
After your ‘Dual Boot’ installation, in your BIOS settings, ensure the “Secure Boot” option must be “Enabled” (the video above asked to disable it), the “Fast Boot” option must still be “Disabled” and you should still see the GRUB Boot Loader with the options available. Once done, we can move on to the next step! (NOTE: The boot options are different for different BIOS, please refer to a manual for your variant if you can’t find the options.)
Ubuntu 20.04 ships with Python3 and GCC pre-installed. To verify this, you can run the following commands.
sudo apt updatesudo apt -y upgradepython3 -Vgcc --version
Also, verify that pip is installed and working fine with this command (it could be pip3 for you if have it set up that way, but it wasn’t for me).
pip --version
My versions are:
Python 3.8.5
gcc (Ubuntu 9.3.0–17ubuntu1~20.04) 9.3.0
pip 21.1.2
Even if you were already through with the first two steps, I still recommend going through the above to fix anything you might have missed in your earlier attempts.
Uninstall/remove/revert all CUDA-related changes you have made to the system so far! The following commands should do that for you:
sudo rm /etc/apt/sources.list.d/cuda*sudo apt remove --autoremove nvidia-*sudo apt-get purge nvidia*sudo apt-get autocleansudo apt-get autoremovesudo rm -rf /usr/lib/cuda*sudo rm -rf /usr/local/cuda*
Remove CUDA paths (usually appended at the end between ifand fi) from .profile and .bashrc files and save them.
sudo gedit ~/.profilesudo source ~/.profilesudo gedit ~/.bashrcsudo source ~/.bashrc
Once removed, echo $PATH | grep cudaand echo $LD_LIBRARY_PATH | grep cudashould not have ‘cuda’ in it!
Now we will tidy up python packages. If you have any python virtual environments set up (read here), you should switch to them. But for my setup, I ran this single command:
pip uninstall tb-nightly tensorboardX tensorboard tensorflow tensorflow-gpu
This might throw some errors depending upon whether you had that package installed or not so no need to worry!
Before we start, ensure steps until now are cleanly done. Installation is not that scary if done in the right sequence! So let’s begin...
These are stable versions and the commands are tried and tested, so just run them one by one and see the magic happening! (Check this to know more.)
sudo add-apt-repository ppa:graphics-driverssudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pubsudo bash -c 'echo "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/cuda.list'sudo bash -c 'echo "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/cuda_learn.list'sudo apt install cuda-10-1
After the last command, the installation pauses and shows this screen here:
Press the down-arrow key and read the full text. Hit the tab key, then hit enter to <Ok>. The screens have nicely written instructions and they walk you through the steps. You will be asked to set a password (8 to 16 characters) which will be asked again on reboot to verify it’s the authentic user. Do not forget the password! Note it down somewhere if so (I kept mine mycuda1234). If you are still not comfortable with “Enroll MOK” screens, refer to this article.
Once that is done and the system boots back, run the command:
sudo apt install libcudnn7
This should install CUDA and cuDNN on your system. The last piece is to run sudo gedit ~/.profileand append the block below and save it.
# set PATH for cuda 10.1 installationif [ -d "/usr/local/cuda-10.1/bin/" ]; then    export PATH=/usr/local/cuda-10.1/bin${PATH:+:${PATH}}    export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}    export LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}fi
Restart your computer and try the following commands:
/sbin/ldconfig -N -v $(sed 's/:/ /' <<< $LD_LIBRARY_PATH) 2>/dev/null | grep libcudnncat /proc/driver/nvidia/versionnvidia-settings -q NvidiaDriverVersionnvidia-sminvcc --version
These should now be running without any errors! If yes, congratulations! You have successfully set up CUDA 10.1 on your machine.
If you are using a python virtual environment, switch to the one suitable to your needs. To set up Tensorflow 2.0 with Python 3, first, we run a command to get a dependency:
sudo apt install python3-testresources
That would prevent this error while Tensorflow installation:
ERROR: launchpadlib 1.10.13 requires testresources, which is not installed.
Now run these commands to finish up (this ensures you have Tensorflow version 2.2 and all of its dependencies):
pip install tensorflowpip install --upgrade tensorflow==2.2pip install --force-reinstall tensorflow==2.2
Check the installation by running pip freeze | grep tensorflow which gives this output on my system:
tensorflow==2.2.0tensorflow-estimator==2.2.0
As the final step run command:
python3 -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
This could possibly print several lines, but the second last one should contain Created TensorFlow device and the last line should return a similar tensor: tf.Tensor(368.0642, shape=(), dtype=float32)
I hope this article was easy to follow and it got your flawless ML environment ready to go!
A huge shout out to the resources I referred to get this set up working!