Install Pytorch for GPU and CPU using Conda on Ubuntu

Jun 17, 2021 10:21 · 267 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 Anaconda or Miniconda depends on your taste.

    Go to anaconda or miniconda’s website and download the .sh file for your system. Go into the terminal use “chmod +x filename” command to the .sh file then run it. Always click yes to finish installation.

  2. Install jupyter

    conda install -y jupyter

  3. 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

  4. 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)"
    
  5. 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 nvidia
      
    • CPU only:

      conda install pytorch torchvision torchaudio cpuonly -c pytorch
      
  6. 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