How to create a virtual environment for PyTorch

PyTorch — Getting Started

The purpose of this blog post is to outline the steps to create a new PyTorch virtual environment

Firstly, Download and install Anaconda (choose the latest Python version).
Login to Anaconda prompt and create a .yml file (pytorch_env.yml) with below configuration

name: pytorch_gpu
channels:
 — defaults
 — pytorch
dependencies:
 — numpy=1.16.2
 — pandas=0.24.2
 — matplotlib=3.0.3
 — pillow=5.4.1
 — pip=19.0
 — plotly=3.7.0
 — scikit-learn=0.20.3
 — seaborn=0.9.0
 — python=3.7.3
 — jupyter=1.0.0

Next, create a virtual environment using the .yml file, like so

conda env create -f pytorch_env.yml
Collecting package metadata (repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
 current version: 4.7.12
 latest version: 4.8.3
Please update conda by running
$ conda update -n base -c defaults conda
Downloading and Extracting Packages

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate pytorch_gpu
#
# To deactivate an active environment, use
#
#     $ conda deactivate

Activate the newly created conda environment

#Get the list of available conda environments
(base)C:> conda env list
# conda environments:
#
base                  *  C:UsersdivyaAnaconda3
kaggle                   C:UsersdivyaAnaconda3envskaggle
nlp                      C:UsersdivyaAnaconda3envsnlp
pytorch_gpu               C:UsersdivyaAnaconda3envspytorch_gpu
#Activate conda environment
(base)C:> conda activate pytorch_gpu
(pytorch_gpu)C:>

Check CUDA Version using below command

C:Users>nvidia-smi
CUDA version on my Machine is - CUDA Version: 10.1

Then, go to PyTorch’s site and find the get started locally section.
Specify the appropriate configuration options for your particular environment.

Run the presented command in the terminal to install PyTorch.

conda install pytorch torchvision cudatoolkit=10.1 -c pytorch

Finally, verify the installation by invoking a jupyter notebook

The primary intention of writing this was for my future reference, I hope this also helps others to save some time when they want to get started with PyTorch. Thankyou!

Leave a comment