How to install pytorch with conda

Here we will explain how to install pytorch with conda.


You can choose right version for your system from

Windows users should follow the tutorial from

Linux users can run the following script:

In terminal execute the following line:

    -O ~/
bash ~/ -b -p $HOME/miniconda

Mac users can also run the same script changing into

Adding conda to PATH (Mac and Linux)

Then add conda to $PATH by running

export PATH="$HOME/miniconda/bin:$PATH"

If you do not want to run it each time you start the system you can add this line to ~/.bashrc (or .zshrc). For example by calling

nano ~/.bashrc

(Mac .zshrc) and adding these lines at the end of the file. Then run

source ~/.bashrc

Update conda (all platforms)

conda init
conda update conda

Creating and activating environment

conda create -n conda3.6 python=3.6
source activate conda3.6

You can deactivate it with source deactive. More info on

Installing pytorch (with numpy, jupyter and matplotlib)

conda install numpy jupyter
conda install pytorch torchvision -c pytorch
conda install -c conda-forge matplotlib 

Install other useful packages

conda install pandas scikit-learn plotly
conda install -c conda-forge opencv seaborn

Run jupyter and test it

After activating environment run

jupyter notebook

When the web page opens, click on button “New”, choose “Python 3”.


Then copy the following into the cell and press Control+Enter. Change imgs/shelf.JPG to any image of your coice.

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
%matplotlib inline
import cv2

bgr_img = cv2.imread('imgs/shelf.JPG')
gray_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2GRAY).astype("float32")/255

plt.imshow(gray_img, cmap='gray')


Updated: 2020-02-02