Install Pytorch for GPU and CPU using Conda on Windows

Aug 5, 2021 11:13 · 284 words · 2 minute read

Why virtual environment is needed?

It is very common in deep learning that each project has different dependency requirements, which makes environment management important. Conda can help us set multiple virtual environments and keep them independent from each other. Thus, all of your ML projects can run smoothly.

Set up virtual environment for pytorch using conda

  1. Install CUDA and cuDNN

    This step is only necessary for those who has a GPU. Before downloading CUDA and cuDNN from NVIDIA, check the version requirements on PyTorch website.

  2. Install Anaconda or Miniconda depends on your taste.

    Go to anaconda or miniconda’s website and download the .exe installer for your system. Rember to check the set conda as enviroment viable box.

  3. Install jupyter

    conda install -y jupyter
    
  4. Create the enviroment named torch

    conda create --name torch python=3.8
    # created a conda enviroment named 'torch'
    # If you use cpu only, you need to change it to python 3.7
    conda create --name torch python=3.7
    

    After finished, reopen the terminal

  5. Activate enviroment

    conda activate torch
    conda install nb_conda
    # Add conda env to jupyter notebook
    python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
    
  6. Install Pytorch

    You can go check Pytorch’s website to see all the installation options.

    • CPU and GPU:

      conda install pytorch torchvision torchaudio cudatoolkit=11.1 -c pytorch -c conda-forge
      
    • CPU only:

      conda install pytorch torchvision torchaudio cpuonly -c pytorch
      
  7. Install other libraries

    conda install -c conda-forge opencv
    dependencies:
        - jupyter
        - scikit-learn
        - scipy
        - pandas
        - pandas-datareader
        - matplotlib
        - pillow
        - tqdm
        - requests
        - h5py
        - pyyaml
        - flask
        - boto3
        - pip:
            - bayesian-optimization
            - gym
            - kaggle
    

    Or, if there is any requirements.txt, just use

    pip install -r requirements.txt
    

Reference:

https://github.com/jeffheaton/t81_558_deep_learning/blob/master/install/pytorch-install-jul-2020.ipynb

https://pytorch.org/get-started/locally/

comments powered by Disqus