vscode terminal installation pytorch environment full process white version (linux+windows universal version)

1. Determine the operating system and cuda version

  • Prerequisite knowledge: Install anaconda or miniconda for python virtual environment management. Miniconda is recommended. (The advantage is that you can install multiple python virtual environments on one host that do not affect each other, and then activate its corresponding python virtual environment 1 when running project 1, and activate its corresponding python virtual environment 2 when running project 2) . Of course, if you are 100% a novice, it is not a big problem to ignore these for now, but I strongly recommend that you spend some time to learn it. After all, you will still have to learn it in about a month at most.
  • Confirm the operating system: This is very simple, there are mainly three categories, windows, linux (Ubuntu, centos, etc.), macos. The underlying gcc version of different versions of the system may be different, which may affect compatibility issues in the next step, but generally this will not be encountered.
  • Confirm the cuda version installed in the system: enter in the terminal nvidia-smiand press Enter. For example, as shown below, the version number is 11.8:
    View cuda version

2. Determine whether the versions between python pytorch cuda are compatible

Before formally configuring the environment, you need to check compatibility. (This step can be omitted later when you are familiar with the host or server where you need to configure the environment)

  • For python and pytorch, I did not find a compatibility table, but generally speaking, the old python corresponds to the older pytorch, and the new one corresponds to the newer one, which is basically no problem, python3.7-3.10, pytorch1.10-2.0.

  • Then the most important thing is the version compatibility issue between pytorch and system cuda. Generally speaking, the cuda version corresponding to the pytorch you install should be less than or equal to the system cuda version (preferably equal to). No more, just this one most important principle. Let's take an example to illustrate: For example, after the previous step, you know that the cuda version of your system is 11.8, then the cuda version corresponding to the pytorch you installed must be less than or equal to 11.8, preferably 11.8. For details, see step 4 to install pytorch.

  • In addition, there is also the compatibility issue between pytorch and gcc. The probability of encountering this problem is small. If you encounter this problem, you only need to update the gcc version and will not go into details.

3. Create a basic python virtual environment

After completing the first two steps, use the following command to create a new conda environment, specify the name as farmer, and use python version 3.9.

Insert image description here

4.Install pytorch

After completing the third step, we continue. Let me further explain what the third step of creating a python virtual environment is equivalent to doing? At this time, it is actually equivalent to treating the operating system as a python community. The community has a property manager, such as python3.7, but you have created a new independent small villa in the system. The housekeeper is python3.9, and then in this small room Installing various packages (adding various items, performing various decorations) in it will not affect the entire community. If you feel bad one day, you can just dismantle the villa. In the same way, you can also create different villas and python housekeepers for different projects. Install different packages. Everyone will not affect each other, nor will it affect the original community and housekeepers. (This is roughly what it means)

First activate the virtual environment in the terminal , the command is conda activate farmer(你在上一步中指定的虚拟环境的名字), then you can install various packages, which is equivalent to adding various items to the villa. The so-called installation of pytorch actually means installing a few packages. It is essentially the same as using pip to install numpy. The specific subsequent steps are as follows:

After clarifying the judgments in the first and second steps, and creating and activating the virtual environment, enter the pytorch official website , as shown below, select the pytorch version, operating system type, installation method (select conda), language, cuda version number, and copy the red box Enter the command in the vscode terminal (similar to Linux and Windows terminals) and press Enter to install the GPU version of pytorch for the current python virtual environment. Generally, it will be about 600-1000MB. Wait until the installation is completed and you are done.
Insert image description here

If you want to select a specific pytorch version: click on previous versions of pytorch, taking 1.12.0 as an example, just look at conda, but don’t look at wheel. Then in the same way, confirm your operating system, confirm the cuda version, copy the corresponding command to the terminal, press Enter and wait for the download to complete.

There is a problem that needs attention here: for example, we checked earlier that the cuda of our computer is version 11.8, but we do not see the corresponding version 11.8 here. What should we do? At this time, you usually only need to choose a similar version, such as 11.6 or 11.7, and there is a high probability that there will be no problem. The other option is to choose the classic version (cuda10.2 and 11.3 are compatible with most pytorch versions). At this time, there is a high probability that you will choose 11.3 for 11.8.

Insert image description here
The terminal example is as follows. Once again, activate the environment first, then copy and enter the previously mentioned command, press Enter and enter y, and wait for the installation to complete:

Insert image description here
Insert image description here

After completion, you can enter in the terminal conda listto view all packages in the current python virtual environment and verify whether pytorch is successfully installed. As shown below, you can see the python version and cuda version corresponding to several installed packages.

Insert image description here

5. Summary reminder

  • After understanding the ideas of this tutorial, you can easily learn the installation ideas in other situations. It is not limited to whether the editor is vscode or pycharm, nor is it limited to whether the operating system is linux or windows. It’s not limited to whether you operate in the system’s default terminal or the terminal in vscode or pycharm. It doesn’t matter, you can do it.
  • The best step is to follow the above ideas step by step clearly, especially for novices. Once you are familiar with it, you can quickly skip the first and second steps. Be sure to activate the corresponding environment before installing pytorch, otherwise it will be installed directly in the system's default base environment, which will cause unnecessary trouble later. The author also stepped on a lot of pitfalls, and no one taught him step by step to get familiar with it. I hope everyone can avoid making some detours!

Guess you like

Origin blog.csdn.net/weixin_44496128/article/details/132122520