This article describes how to configure a new version of the Python deep learning library that can run on CPU or GPU in the distribution version of the Linux operating system Ubuntu .tensorflow
In the article Anaconda, the method of configuring the new version of the tensorflow library of Python (common to CPU and GPU) (https://blog.csdn.net/zhebushibiaoshifu/article/details/129285815) and the configuration method of the new version of the GPU-accelerated tensorflow library (https: //blog.csdn.net/zhebushibiaoshifu/article/details/129291170), we have introduced the method of configuring the CPU and GPU versions of the library under the Windows platform ; in this article, we will introduce the method in the Linux Ubuntu environment , the configuration method of the CPU and GPU version libraries.tensorflow
tensorflow
This article is divided into two parts, the first 1
part is the configuration method of the CPU version of the library, and the second part is the configuration method of the GPU version of the library; if your computer has a GPU , then skip the first part directly and start from the first part of this article It looks good. It needs to be clear that the Python version in this article is a relatively new version; but if you have other versions of Python , there is no problem, and the overall configuration idea is the same.tensorflow
2
tensorflow
1
2
3.10
1 CPU version
First, let's introduce the configuration method of the CPU version of tensorflow
the library.
Configuring the CPU version of tensorflow
the library can be said to be very simple. First of all, it is recommended that you follow the content mentioned in the article Linux Ubuntu Configuration Anaconda and Python (https://blog.csdn.net/zhebushibiaoshifu/article/details/130807267), first configure the Anaconda environment; secondly, if you need Configure the library in the virtual environment , then you can create a virtual environment by yourself and start the follow-up operations-I will configure tensorflow
it directly in the default environment, which is the environment. base
Regarding the creation of a virtual environment by Anaconda , you can refer to the article Creating, using and deleting a Python virtual environment in Anaconda (https://blog.csdn.net/zhebushibiaoshifu/article/details/128334614), so I won’t go into details here.
We can view the environment in the current Anaconda environment by entering the following code in the terminal .
conda info -e
Run the above code, you will get the situation as shown in the figure below. Among them, you can see that because I have not created a virtual environment here, there is only one base
environment.
Then, we enter the following code in the terminal to install tensorflow
the library.
conda install tensorflow
Run the above code, we will automatically start installing the latest version of the library supported by the current environment (that is, the Pythontensorflow
version) ; as shown in the figure below.
After the installation is complete, the interface as shown in the figure below will appear.
So far, we have completed the configuration of the CPU version of tensorflow
the library. We follow the method mentioned in the new version of the GPU-accelerated tensorflow library configuration method (https://blog.csdn.net/zhebushibiaoshifu/article/details/129291170), enter the following code in Pythontensorflow
, and check whether the current library is Support GPU computing.
import tensorflow as tf
print(tf.config.list_physical_devices("GPU"))
Run the above code, if you get an empty list as shown in the figure below []
, it means that the current tensorflow
library does not support GPU computing - of course this is affirmative, what we configure here is the CPU version of tensorflow
the library, naturally it cannot be computed on the GPU up.
At this point, tensorflow
the library can also be used normally, but it can only support CPU operations. It is necessary to mention here that in fact, the library we configured through the aforementioned method also supports GPU computing tensorflow
in principle —because in the Linux operating system, from the version of the library onwards, there is no longer a distinction between the CPU and GPU versions, as long as After downloading the library, it is supported by both CPU and GPU ; the reason why the library we have configured so far cannot run on the GPU is because we have not configured other dependencies required for GPU computing (or There is no GPU in the computer at all ).tensorflow
1.15
tensorflow
tensorflow
2 GPU versions
Next, let's introduce the configuration method of the GPU version of tensorflow
the library.
2.1 NVIDIA Driver Configuration
First, we need to configure the NVIDIA driver. NVIDIA drivers are software for NVIDIA graphics cards that control the features and performance of NVIDIA graphics cards and make sure they work properly with the operating system and other software.
First, we can enter the following code in the terminal.
nvidia-smi
Then, under normal circumstances, the situation as shown in the figure below should appear. If you have other situations at this time, it means that either no NVIDIA driver is installed, or the NVIDIA driver is installed but there is a problem with the version of this driver. We can ignore it here, and everyone can continue to read down.
Next, we began to install the NVIDIA driver. Among them, here are 3
three different methods, but it is recommended that you use the last one.
2.1.1 Method 1 (not recommended)
In the first 1
method, we can directly enter the following code in the terminal.
sudo ubuntu-drivers autoinstall
Under normal circumstances, this code will automatically download or update the driver in our computer, and the NVIDIA driver will also be downloaded or updated along with it. However, after I tried this method, I found that it did not work. Therefore, this method should be related to the status of your computer, and it may not be 100% successful, so it is not recommended.
2.1.2 Method 2 (not recommended)
The first 2
method is to download directly from the official website of the NVIDIA driver; but this method is more troublesome, so I do not recommend it here.
First, we enter the official website of the NVIDIA driver (https://www.nvidia.cn/Download/index.aspx?lang=cn), and in the interface as shown in the figure below, according to the model of the graphics card in your computer, Computer system etc. to be selected.
Then, click the " Search " option, the most suitable NVIDIA driver will appear, and click " Download ".
Then, in the terminal, you can install the NVIDIA driver you just downloaded.
2.1.3 Method 3 (recommended)
The first 3
method is the most recommended method.
First, you enter the following code in the terminal.
ubuntu-drivers devices
Then, the interface as shown in the figure below will appear; among them, the NVIDIA driver version that appears is the most suitable version in our computer; you need to record this version number at this time, and you will need to use it later recommended
.
Next, we enter the following code in the terminal.
sudo apt install nvidia-driver-525
Among them, the last part of the above code 525
is the version number recorded in the figure above, and you can modify the above code according to your actual situation. After running the code, the situation as shown in the figure below will appear, that is, this version of the NVIDIA driver will start to download and install.
If everyone's subsequent download and installation are smooth, then everything will be fine for a long time; but sometimes, an error message as shown in the figure below will appear.
At this time, it shows that the original NVIDIA driver in our computer conflicts with the newly downloaded version, which makes the new version unable to be installed normally. At this point, we need to enter the following codes in sequence in the terminal, remember to enter one line at a time.
sudo apt-get purge nvidia*
sudo apt-get purge libnvidia*
sudo apt-get --purge remove nvidia-*
sudo dpkg --list | grep nvidia-*
In the above code, the first 3
line means to delete the original NVIDIA driver and its related content, and the last sentence is used to detect whether the original NVIDIA driver has been deleted. If you have the situation as shown in the figure below, that is, no prompt message appears after entering the last sentence of the above code, then it means that the original NVIDIA driver has been deleted.
At this point, we can execute the following code again.
ubuntu-drivers devices
But at this time, unlike the previous article, you may see that recommended
the NVIDIA driver version has changed. For example, here I am no longer the previous version 525
, but another version; but here we don’t care about this change. After that, you can still download 525
the version.
Next, we still run the following code.
sudo apt install nvidia-driver-525
Among them, the last part of the above code 525
is the version number here, you still have to remember to modify it. At this point, we can download and install the specified version of the NVIDIA driver normally.
At this point, we enter the following code in the terminal again.
nvidia-smi
Then, under normal circumstances, the situation as shown in the figure below should appear. Among them, you can pay attention to the upper right corner of the figure below, which means that the CUDA version is the highest support 12.0
, and the newer version will not support it-of course, what is this CUDA and how to configure it, we will mention it next, here is to pay attention to it first Just one click.
Another thing to note is that if the situation shown in the figure below appears after entering the aforementioned code, then it still means that there is a conflict between the original NVIDIA driver in our computer and the newly downloaded version, please re-execute the above Just delete the sentence code of the original NVIDIA driver in the computer 3
.
Then, we can also enter the following code.
nvidia-settings
If the situation shown in the figure below appears, that is, a new window named " NVIDIA X Server Settings " is opened, it means that there is no problem with our aforementioned configuration.
So far, we have completed the configuration of the NVIDIA driver.
2.2 CUDA configuration
Next, we configure CUDA ; CUDA is a parallel computing platform and programming model invented by NVIDIA .
First of all, we need to go to the official websitetensorflow
of the library (https://www.tensorflow.org/install/source), pull down to find the matching table of the library version and the corresponding CUDA and cuDNN version as shown in the figure below , and combine it with our own Python Version, choose to determine which version of the library you need, and further determine the version of your CUDA and cuDNN . Among them, as shown in the purple box in the figure below, since my Python version here is yes, I can only choose the version in the purple box; then, I want to use a new version of the library, so I choose to use the CUDA and cuDNN versions corresponding to the first line up.tensorflow
tensorflow
3.10
tensorflow
Then, we go to the official website of CUDA (https://developer.nvidia.com/cuda-downloads), firstly follow the method shown in the figure below, and select the corresponding content based on the model of your computer; among them, pay attention to the last option To choose .runfile (local)
Then, the website will automatically display the latest version of CUDA according to our selection . But it should be noted that the choice given to us on the website defaults to the latest version, and we need to determine the version we need according to the library version mentioned above tensorflow
and the corresponding CUDA and cuDNN version matching table. For example, as shown in the first purple box in the figure below , the CUDA version 3
given on the website is yes, but the version I need is yes, so I need to find the old version of CUDA through the " Archive of Previous CUDA Releases " option in the figure below .12.1.1
11.8
As shown in the figure below, we can find 11.8
the version of CUDA here , just click it.
Then, the installation method 11.8
of the version of CUDA will appear , we just enter the two lines of code displayed on the website at this time in the terminal.
Then, you can start installing CUDA . Among them, if you are installing, if the prompt shown in the figure below appears, it is generally caused by the old version of CUDA installed in the computer; but you don’t need to worry about it, just select the “ Continue ” option.
Then, everyone should pay attention, in the interface shown in the figure below, uncheck the cross in front of the Driver to cancel the installation of the NVIDIA driver, because we have already installed this driver before. Then, you can select " Install ".
Next, we can start to install CUDA , and the interface shown in the figure below will appear after the installation is complete.
So far, we have completed the installation of CUDA , but we need to further configure the corresponding environment variables. First, enter the following code in the terminal.
vim ~/.bashrc
This code means that we will open bashrc
this file and edit it to realize the configuration of environment variables. After running the above code, we will see an interface similar to the one shown below.
Then, we press i
the key to start bashrc
editing the file. By adjusting the position of the mouse, bashrc
add the following content at the end of the file.
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
At this point, we will get the situation shown in the figure below.
Next, we first press Esc
the key to exit the editing mode; next, enter :wq
, which means saving and exiting bashrc
this file. At this point, an interface like the one shown below should appear.
Next, we enter the two lines of code shown below in the terminal respectively.
source ~/.bashrc
nvcc --version
Among them, the first sentence means to update bashrc
the file, so that the environment variable we just modified will take effect immediately; the second sentence is the code to verify the installation of CUDA . If the interface shown in the figure below appears after running the above two sentences of code, it means that our CUDA configuration and environment variable configuration have been completed.
So far, we have completed this part of the configuration work.
2.3 cuDNN configuration
Next, we start configuring cuDNN . cuDNN is a library of GPU- accelerated deep neural network primitives that implement standard routines (such as forward and backward convolution, pooling layers, normalization, and activation layers) in a highly optimized manner. Here we still need to look at the matching table of the library version mentioned above tensorflow
and the corresponding CUDA and cuDNN version to clarify which version of cuDNN we need to download .
First, we enter the official website of cuDNN (https://developer.nvidia.com/rdp/cudnn-download); before downloading cuDNN , we need to register first, but the registration process is relatively fast and can be completed in a few minutes.
Then, we found the corresponding version of cuDNN in the website . It should be noted here that if the cuDNN version we need is not the latest, then we need to find the old version in the " Archived cuDNN Releases " option in the figure below.
8.6
I need a version of cuDNN here , so I need to find the download link of this version from the location shown in the picture above, and start the download.
After the download is complete, we first enter the download path through the following command in the terminal; of course, if your download path is different, you can modify the following code yourself.
cd ~/Downloads
Then, enter the following code; here you need to pay attention, this part of the following code , you need to combine the specific version number in the installation package8.x.x.x
obtained after downloading to modify. The function of this line of code is to start our local repository.
sudo dpkg -i cudnn-local-repo-${OS}-8.x.x.x_1.0-1_amd64.deb
Run the above code, as shown in the figure below.
Next, enter the code shown below line by line. Among them, for this part of the following code 8.x.x.x
, you still need to modify it according to the specific version number in the installation package obtained after downloading; at the same time, we need to modify this part according to the CUDA versionX.Y
selected earlier . For example, the CUDA version I downloaded earlier was yes, so this one is . The functions of these three lines of code are: import CUDA 's GPG key, refresh the metadata of the repository, and install the runtime library.11.8
X.Y
11.8
sudo cp /var/cudnn-local-repo-*/cudnn-local-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get install libcudnn8=8.x.x.x-1+cudaX.Y
As shown in the first and second lines of code in the figure below, it is the specific content of the third sentence of code I entered here.
Then, we continue to enter the following code in the terminal, and also remember to modify our version number. The function of this code is to install the developer library.
sudo apt-get install libcudnn8-dev=8.x.x.x-1+cudaX.Y
As shown in the figure below, it is the specific content I entered here.
Then, we continue to enter the following code in the terminal, and also remember to modify our version number. The function of this code is to install the code sample.
sudo apt-get install libcudnn8-samples=8.x.x.x-1+cudaX.Y
As shown in the figure below, it is the specific content I entered here.
The above is the specific process of cuDNN installation. Next, we need to verify whether it is installed correctly. This verification process is a little more troublesome, but the process is actually faster. We enter the following code line by line in the terminal.
cp -r /usr/src/cudnn_samples_v8/ $HOME
cd $HOME/cudnn_samples_v8/mnistCUDNN
sudo apt-get install libfreeimage3 libfreeimage-dev
make clean && make
./mnistCUDNN
If you run the above code and get the result shown in the figure below, and this word appears Test passed!
, it means that our cuDNN has also been configured.
So far, cuDNN has been successfully configured.
2.4 tensorflow library configuration
Next, we finally come to the last step, which is tensorflow
the configuration of the library.
We enter the following code in the terminal.
pip install tensorflow
Then, the situation as shown in the figure below will appear. Here you need to pay attention, please look at the words in the purple box in the picture below, if the library we started to download at this time tensorflow
is the version we need, then there is no problem; if it is a version that we cannot currently use (that is, with CUDA , cuDNN version does not match), then you can re-download tensorflow
the library by specifying the version.
After completing tensorflow
the library configuration, we enter the following code in Pythontensorflow
to check whether the current library supports GPU computing.
import tensorflow as tf
print(tf.config.list_physical_devices("GPU"))
Run the above code, if you get the words shown in the purple box in the figure below, it means that our tensorflow
library has been configured and GPU accelerated operations can be used.
So far, you're done.
Welcome to pay attention: Crazy learning GIS