Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Click on the aboveBeginner Learning Visual“, choose to add “Star” or “Pinned

Heavyweight content delivered promptlyDeep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Getting started with deep learning, many people have experienced the bitter journey from beginner to abandonment, with countless individuals failing at the first hurdle: environment configuration issues. As the saying goes, if the environment is not set up correctly, learning leads to tears.

If you are facing the pain of configuring the environment, whether you are a Windows user, Ubuntu user, or a die-hard Mac fan, this article is tailor-made for you. Next, we will discuss the deep learning environment configuration issues for Windows, Mac, and Ubuntu in turn.

1. Windows System Deep Learning Environment Configuration

System: Win10 64-bit Operating System

Installation Combination: Anaconda + PyTorch (GPU version) + GTX1060

Open Source Contributor: Wu Tianzhou, Inner Mongolia Agricultural University

1.1 Open Anaconda Prompt

1. conda create -n pytorch python=3.7.0: Create a virtual environment named pytorch and install python=3.7 for that environment.
2. activate pytorch: Activate the environment named pytorch

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

1.2 Determine the CUDA version supported by hardware

NVIDIA Control Panel – Help – System Information – Components

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

On May 19, 2020, at 16:46:31, I updated the graphics driver and saw that my CUDA supports up to version 11

1.3 Determine PyTorch version and torchvision version

Visit the PyTorch official website: https://pytorch.org/get-started/locally/

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Because the official source is too slow, here we use Tsinghua source for downloading

1.4 Download the corresponding installation package from the mirror

Tsinghua Mirror:

https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/win-64/

PyTorch:

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

torchvision:

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

1.5 Local Installation

Next, in the PyTorch environment, enter the following commands in sequence.

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Then return to the directory where the virtual environment is located and use conda install anaconda to install the basic packages required for the environment

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

1.6 Test

Code 1:

from __future__ import print_function
import torch
x = torch.rand(5, 3)
print(x)
The output will be a tensor similar to the following:

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Code 2:
import torch
torch.cuda.is_available()
Output: True

If there are no exceptions in the output of the above two code snippets, it indicates that the environment setup was successful.

1.7 Issues Encountered

  • Error creating virtual environment with the command below

conda create -n py37_torch131 python=3.7

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

[Solution] https://blog.csdn.net/weixin_42329133/article/details/102640763

The environment will be saved in the envs folder under the Anaconda directory

  • PackagesNotFoundError: The following packages are not available from current channels

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

[Solution]:
https://www.cnblogs.com/hellojiaojiao/p/10790273.html
  • Conda download is too slow

[Solution] https://mirror.tuna.tsinghua.edu.cn/help/anaconda/

I shout Tsinghua is awesome!

  • CUDA installation

Download historical versions of CUDA: https://developer.nvidia.com/cuda-toolkit-archive
To see which version of CUDA your NVIDIA supports, I have a GTX 1060, so I downloaded version 10.0 of CUDA

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

  • PyTorch installation

The official website does not have my combination, I am confused

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Here I directly (don’t forget to enter the environment you just created)
conda install pytorch torchvision cudatoolkit=10.0 -c pytorch
  • How to enter the virtual environment in Jupyter Notebook

python -m ipykernel install –name

1. Open Anaconda Prompt and enter conda env list to view existing environments

2. Enter activate name (name is the environment you want to switch to)

3. conda install ipykernel to install necessary plugins

4. python -m ipykernel install –name Name to add the environment to Jupyter (Name is the name displayed in Jupyter for this environment, customizable)

Delete kernel

jupyter kernelspec remove

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Error [Errno 13]

[Errno 13] Permission denied: ‘/usr/local/share/jupyter’

To do this, you need to add the –user option to generate the configuration file in the home directory of the current account.
python -m ipykernel install --user --name py27-caffe-notebook

Thus, the kernel addition is complete. To view existing kernels:

jupyter kernelspec list

Delete an existing kernel

jupyter kernelspec remove <kernel_name></kernel_name>

The above command only deletes the configuration file and does not uninstall the corresponding ipykernel of the virtual environment. Therefore, if you want to reinstall the kernel for the corresponding Python virtual environment, just activate the virtual environment, and then

python -m ipykernel install --name <kernel_name></kernel_name>
  • Conda installation fails halfway

Download the file locally, navigate to the directory of the file, and use the command

conda install --offline <package_name></package_name>
2. Mac Deep Learning Environment Configuration
Installation Combination: Anaconda + PyTorch (GPU version)
Open Source Contributor: Ma Zengou, University of London

2.1 Install Anaconda

There are two ways to install Anaconda, here we only introduce the most intuitive one – macOS graphical install.
At https://www.anaconda.com/products/individual, under Anaconda Installers, choose the “64-Bit Graphical Installer (442)” under Python 3.7. After downloading

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Click to enter the pkg installation package and follow the prompts to complete the installation. The default installation location is ~/opt. For command line installation, please refer to: https://docs.anaconda.com/anaconda/install/mac-os/

2.2 Confirm Download Status

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

In the Mac Terminal, enter

python --version

Ensure that the installed Python is version 3.x. In the Terminal, enter

jupyter notebook
A webpage will pop up, allowing you to enter the notebook.

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Click Quit in the upper right corner of the webpage, or return to the Terminal and use command + c to exit the notebook.

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

2.2.1 Common Issues

If multiple Anaconda versions are downloaded on the computer, conflicts may occur during operation. In the Terminal, enter

cd ~

Return to the home directory and enter

cat .bash_profile

If you can only see one version of Anaconda, there is no problem. If there are multiple versions, conflicts may occur when downloading packages. Use vim, nano, or other text editors to delete the old version of Anaconda’s

export PATH= ...

Delete it.

2.3 Creating Virtual Environments and Downloading Packages

Using conda to create virtual environments and download corresponding packages is a simple task.

2.3.1 Graphic

Click Anaconda-Navigator, and you can see the applications you have downloaded. The top left corner “Applications on” should point to “base (root)”. Click “Environments” on the left to see the virtual environments and corresponding packages you have created. Click “Create” in the bottom left corner to create a new virtual environment.

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Enter the environment name and the version of Python, then click create to create.

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Then on the Home page, ensure that the top left corner points to the environment name you just created. In this environment, install jupyter notebook, noting that the original notebook installed is in base and cannot be shared.

Return to the Environments section, and you can see all the packages in this environment. Select All in the upper left corner, and enter the name of the package you want to download

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Then select it for download

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

2.3.2 Command Line

Using the command line to complete the above operations is also very straightforward. This time we take PyTorch as an example. In the Terminal, enter

conda create --name env_name

This will create a virtual environment called “env_name”. Enter

conda env list

to see all created virtual environments, where the one with * is the current environment. Enter

conda activate env_name

to enter the environment

conda deactivate

to exit the current virtual environment and return to base.

2.3.3 Download PyTorch

Generally, Macs do not support CUDA. Go to https://pytorch.org/ to see

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

The PyTorch official website shows the download command that should be used for the current device. In this case, mine is
conda install pytorch torchvision -c pytorch

Copy it, paste it into the Terminal, and run it to start downloading.

After downloading, enter in the Terminal

python3

Then import the two packages just downloaded to confirm completion

import torch
import torchvision
print(torch.__version__)
print(torchvision.__version__)
If the import and output are normal, the configuration is complete! Enter

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

quit()

And it’s done.

2.4 Additional Situations

If you download directly with conda in China, it may be very slow. You can switch download sources for acceleration. The method for switching sources can refer to the materials in the Linux section: https://zhuanlan.zhihu.com/p/87123943.
During conda downloads, you may often encounter the “Solving environment: failed…” issue. If the virtual environment is normally created, this issue should not be a problem. If it still occurs, you can create another virtual environment.
Conda has a large community, and it is very likely that someone has encountered and answered the issues you face; Google will be a great solution.
3. Ubuntu Deep Learning Environment Configuration
Installation Combination: Anaconda + PyTorch (CPU version) or PyTorch (GPU version)
Open Source Contributor: Chen Xinda, North China Electric Power University
3.1 Anaconda Installation
Anaconda and Python versions correspond, so you need to choose to install the appropriate Python 2.7 or Python 3.7 version, or other versions according to your needs.
Download link: https://www.anaconda.com/download/#linux

Click on the 64-Bit (x86) Installer (522 MB) below to download the 64-bit version.

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

The downloaded file name is: Anaconda3-2020.02-Linux-x86_64.sh.

cd to the directory where Anaconda3-2020.02-Linux-x86_64.sh is located:

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Execute bash Anaconda3-2020.02-Linux-x86_64.sh to start the installation:
Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu
Keep pressing Enter until the following interface appears, then enter yes:
Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu
Here, press Enter to install to the default path, or enter a custom path after >>>
Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu
Wait for the installation progress bar to finish, then the following prompt will appear. Enter yes to add to the environment variable, no to not add to the environment variable. Here, we will enter no as an example
Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Next, manually add the environment variable. First, cd to ~, then edit the .bashrc file: sudo vim .bashrc

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Add the following lines at the bottom (note the space after .):
# Distinguish between anaconda python and system built-in python
alias python3="/usr/bin/python3.5"
alias python2="/usr/bin/python2.7"
. /home/cxd/anaconda3/etc/profile.d/conda.sh
Then press esc + : + wq! to save

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Enter source .bashrc to execute the modified initialization document

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu
Then enter conda env list to test if the environment variable is set successfully:
Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu
Try using the built-in Python command you just set: python2, python3
Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu
If it ends here, everyone will definitely suffer when installing packages~ Here, we need to change the Anaconda source (add Tsinghua source):
Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu
Then we create a virtual environment named pytorch and find the following error:
Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu
The reason is that we did not clear the sources left over from the previous installation. Enter sudo vim .condarc, modify the content of this file (remember to delete the default line):
Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu
Then enter source .condarc and create the virtual environment again:
Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

3.2 PyTorch CPU Version Installation

Open the PyTorch official website: https://pytorch.org/

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu
Activate the virtual environment you just created: conda activate pytorch. Before installation, add the following source:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
Then enter the following code:
conda install pytorch torchvision cpuonly -c pytorch

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

After the installation, test whether it was completed:
import torch
print(torch.__version__)
The output should be as follows to indicate successful installation:

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

3.3 PyTorch GPU Installation

3.3.1 GPU Driver Installation

  • Check Graphics Card Type

Execute the command: ‘ ubuntu-drivers devices’

== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd00001C8Dsv00001028sd0000086Fbc03sc02i00
vendor   : NVIDIA Corporation
model    : GP107M [GeForce GTX 1050 Mobile]
driver   : nvidia-driver-390 - distro non-free
driver   : nvidia-driver-435 - distro non-free
driver   : nvidia-driver-440 - distro non-free recommended
driver   : xserver-xorg-video-nouveau - distro free builtin

== /sys/devices/pci0000:00/0000:00:14.3 ==
modalias : pci:v00008086d0000A370sv00008086sd000042A4bc02sc80i00
vendor   : Intel Corporation
model    : Wireless-AC 9560 [Jefferson Peak]
manual_install: True
driver   : backport-iwlwifi-dkms - distro free

You can see here that there is a device which is GTX 1050. The recommended driver is 440.

  • Install Driver

Install all recommended drivers

sudo ubuntu-drivers autoinstall

To install one driver

sudo apt install nvidia-440

3.3.2 Install CUDA

CUDA installation requires the appropriate graphics card driver. Below is the version correspondence between the driver and CUDA

Table 1. CUDA Toolkit and Compatible Driver Versions
CUDA Toolkit    Linux x86_64 Driver Version Windows x86_64 Driver Version
CUDA 10.2.89    >= 440.33   >= 441.22
CUDA 10.1 (10.1.105 general release, and updates)   >= 418.39   >= 418.96
CUDA 10.0.130   >= 410.48   >= 411.31
CUDA 9.2 (9.2.148 Update 1) >= 396.37   >= 398.26
CUDA 9.2 (9.2.88)   >= 396.26   >= 397.44
CUDA 9.1 (9.1.85)   >= 390.46   >= 391.29
CUDA 9.0 (9.0.76)   >= 384.81   >= 385.54
CUDA 8.0 (8.0.61 GA2)   >= 375.26   >= 376.51
CUDA 8.0 (8.0.44)   >= 367.48   >= 369.30
CUDA 7.5 (7.5.16)   >= 352.31   >= 353.66
CUDA 7.0 (7.0.28)   >= 346.46   >= 347.62

CUDA download link: http://suo.im/6dY8rL

Installer Type can choose either of the first two. However, after obtaining the CUDA file, first check the gcc version. Below is an example using the first runfile (local) installation method.

  • Install GCC

Linux generally comes with GCC, let’s first check the version of GCC in your system

gcc --version

And the GCC dependency version for CUDA will be provided in the official installation guide

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

If the version does not match the dependency version of CUDA, install the version required by CUDA
sudo apt-get install gcc-7.0
sudo apt-get install g++-7.0

After installation, you need to switch the system GCC version

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 50
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 50

Select the required version

sudo update-alternatives --config gcc
  Select       Path          Priority  Status
------------------------------------------------------------* 0            /usr/bin/gcc-9   50        Automatic mode  1            /usr/bin/g++-9   50        Manual mode  2            /usr/bin/gcc-7   50        Manual mode
Just enter the number displayed in front.
  • Install CUDA

sudo sh cuda_<your_version>_linux.run</your_version>
  • Configure Environment Variables

sudo vim ~/.bashrc

Copy the following command into it

export PATH=/usr/local/cuda-10.2/bin${PATH:+:$PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

Check if the installation was successful

nvcc -V

3.3.3 Install cuDNN

https://developer.nvidia.com/cudnn Select the version corresponding to CUDA

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Then copy the include and lib64 folders of cuDNN to CUDA

sudo cp cuda/include/cudnn.h    /usr/local/cuda-10.2/include    # The name of the unzipped folder is cuda-10.2
sudo cp cuda/lib64/libcudnn*    /usr/local/cuda-10.2/lib64
sudo chmod a+r /usr/local/cuda-10.2/include/cudnn.h   /usr/local/cuda-10.2/lib64/libcudnn*

3.3.4 Install PyTorch-GPU

Deep Learning Environment Setup Guide for Windows, Mac, and Ubuntu

Conda installation:

# Choose the corresponding CUDA version
conda install pytorch torchvision cudatoolkit=10.2

pip installation:

pip install torch torchvision -i  https://pypi.mirrors.ustc.edu.cn/simple
4. Final Remarks
All deep learning environment installation guides conclude here, hoping to resolve the environment configuration challenges you face. For practical projects, you can combine the Alibaba Tianchi learning competition for hands-on practice.
Data Mining Learning Competition (Ongoing, 5832 participants)
https://tianchi.aliyun.com/competition/entrance/231784/forum
CV Practical Learning Competition (Ongoing, 1933 participants)
https://tianchi.aliyun.com/competition/entrance/531795/forum
NLP Practical Learning Competition (Ongoing, 573 participants)
https://tianchi.aliyun.com/competition/entrance/531810/forum
Download 1: OpenCV-Contrib Extension Module Chinese Version Tutorial
Reply "Extension Module Chinese Tutorial" in the "Beginner Learning Visual" public account backstage to download the first OpenCV extension module tutorial in Chinese, covering more than twenty chapters including extension module installation, SFM algorithms, stereo vision, target tracking, biological vision, super-resolution processing, etc.

Download 2: Python Vision Practical Projects 52 Lectures
Reply "Python Vision Practical Projects" in the "Beginner Learning Visual" public account backstage to download 31 visual practical projects including image segmentation, mask detection, lane line detection, vehicle counting, eyeliner addition, license plate recognition, character recognition, emotion detection, text content extraction, face recognition, etc., to help you quickly learn computer vision.

Download 3: OpenCV Practical Projects 20 Lectures
Reply "OpenCV Practical Projects 20 Lectures" in the "Beginner Learning Visual" public account backstage to download 20 practical projects based on OpenCV, achieving advanced learning in OpenCV.

Communication Group

Welcome to join the public account reader group to communicate with peers. Currently, there are WeChat groups for SLAM, 3D vision, sensors, autonomous driving, computational photography, detection, segmentation, recognition, medical imaging, GAN, algorithm competitions, etc. (will gradually be subdivided). Please scan the WeChat ID below to join the group, and note: "Nickname + School/Company + Research Direction", for example: "Zhang San + Shanghai Jiao Tong University + Visual SLAM". Please follow the format; otherwise, the request will not be approved. After successfully adding, you will be invited into the relevant WeChat group based on your research direction. Please do not send advertisements in the group; otherwise, you will be removed from the group. Thank you for your understanding~

Leave a Comment