1, Foreword
Installing TensorFlow under Ubuntu is not a simple installation using pip. If you can't use GPU, the efficiency will be very low. The methods here are found out by stepping on some pits under the system. I hope they will be helpful.
- System: Ubuntu 21 ten
- CUDA version: cuda11 one
- Cudnn version: cudnn8 two point one
2, Install NVIDIA driver
1. Add PPA source
Add PPA source:
sudo add-apt-repository ppa:graphics-drivers/ppa
Install dependencies for the system to build kernel modules:
sudo apt-get install dkms build-essential
2. Check the hardware model of the graphics card
Open the terminal and enter the following command
ubuntu-drivers devices
The output interface is as follows. You can see that my hardware graphics card model here is GP108M [GeForce MX250], and the recommended driver version number is: Driver: nvidia-driver-470 - distro non free recommended
3. Install NVIDIA driver
Here we can download the driver version recommended by our computer:
sudo ubuntu-drivers autoinstall
The system will automatically install the recommended version driver, and then restart the system.
3, Installing cuda
The cuda version I use here is 11.1 run file installation, the latest version 11.6 tried to have some problems, so I changed to version 11.1.
1. Download related dependencies
The command is as follows:
sudo apt-get install freeglut3 freeglut3-dev libxi-dev libxmu-dev
2. Download the installation file
Just use wget to download directly. Here I provide the download link of version 11.1:
cd ~/Downloads wget https://developer.download.nvidia.com/compute/cuda/11.1.0/local_installers/cuda_11.1.0_455.23.05_linux.run
There are probably more than three gigabytes. Pay attention to the network connection.
3. Install CUDA Toolkit
Here we only use the installation file to install CUDA toolkit. The driver installation has been completed in the above operations, so the command is as follows:
sudo sh ./cuda_11.1.0*.run --toolkit --silent --override
4. Configure environment variables
Modify ~ / bashrc file:
vim ~/.bashrc
Add the following:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64 export PATH=$PATH:/usr/local/cuda/bin export CUDA_HOME=$CUDA_HOME:/usr/local/cuda
Make changes effective immediately:
source ~/.bashrc
5. Inspection and installation
The command is as follows:
nvcc --version
4, Install cudnn
1. Download the installation file
Here I download version 8.2.1. The installation of cudnn needs to correspond to your cuda version. The download link on the official website is as follows: https://developer.nvidia.com/rdp/cudnn-archive , you may need to register to download (domestic email can register).
2. Install cudnn8 two point one
Enter the download directory:
cd ~/Downloads
Unzip tgz package:
mkdir cudnn-11.1 tar -zxf cudnn-11.1*.tgz -C ./cudnn-11.1
Enter the extracted file directory:
cd cudnn-11.1
Install the executable and grant permissions:
sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
3. Configure environment variables
Edit ~ / bashrc file:
vim ~/.bashrc
Add the following:
export LD_LIBRARY_PATH=/usr/local/cuda/extras/CUPTI/lib64:$LD_LIBRARY_PATH
source:
source ~/.bashrc
5, Install TensorFlow
1. Install dependent packages
Before installing TensorFlow, we need to install a dependency package. Here, my CUDA version is 11.1 and the download dependency package is libcudnn8_8.0.5.39-1+cuda11.1_amd64.deb, the official website link is as follows: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/
Here I use wget to download:
cd ~/Downloads wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/libcudnn8_8.0.5.39-1+cuda11.1_amd64.deb
The installation commands are as follows:
sudo dpkg -i libcudnn8_8.0.5.39-1+cuda11.1_amd64.deb
2. Install TensorFlow
You can install it directly with pip. Here, Python version is required to be 3.6-3.9, pip > = 19.0. First upgrade your pip tool with the following command:
python3 -m pip install --upgrade pip
Install TensorFlow using pip. Here I choose Alibaba cloud:
pip install -i https://mirrors.aliyun.com/pypi/simple tensorflow
3. Test
Open Python and test with the following command:
import tensorflow as tf print(tf.__version__)
The output is as follows:
Next, test our GPU support:
tf.config.list_physical_devices("GPU")
be accomplished!