Contents
On Windows systems, one can install PyTorch in a virtual machine (such as VirtualBox) running, say, Ubuntu. Alternatively, Windows 10 users may wish to use the Windows Subsystem for Linux (WSL).
Update April 21: Microsoft is retooling its WSL subsystem so that it includes GUI support. The upgraded version is referred to as WSLg. This is good news since, with GUI support, one will not have to install (and remember to start) an separate X server. WSLg is currently available only for Windows 10 Insider Preview editions. Installation/usage details can be found here: github.com/microsoft/wslg When WSLg is more widely available running smoothly, Simmons will update this page.
Relevant only if your laptop or personal machine has a GPU:
- Using a virtual machine adds a layer of software between the, say, Python you run in your Linux distribution in your virtual machine and the hardware that interfaces your GPU. Unless things have significantly improved by the time you are reading this, it can be prohibitively difficult to tunnel through that layer of software. In other words, mere mortals should not expect to get PyTorch running in Linux a virtual machine to see their GPU.
- As of summer '20, CUDA support is evidently available for WSL 2. But this is DirectX based.
- See here for an apparent way to install and run PyTorch natively on windows. (Simmons has not verified that this works.)
- To painlessly use your GPU with Pytorch, Simmons' current recommendation is still to split your hard-drive and run bare-metal Linux.
Install Windows10's Ubuntu using the WSL
- Follow the instructions here.
- Pick the latest 04 release of Ubuntu (currently 20.04).
- Next, let's update your Ubuntu.
- Fire up Ubuntu by clicking on its icon in your Windows start menu.
- You might be asked to create a username and password for your WSL. Pick a simple username and a password you'll not easily forget.
- At the command prompt, enter
sudo apt update
and answer Y(es) at any prompt.sudo apt upgrade
sudo apt dist-upgrade
- Now let's install/update a few packages. At the command prompt, enter, in turn:
sudo apt install git
(git might already be installed).sudo apt install libblas-dev liblapack-dev
sudo apt install python3
(should already be installed)
Enhance your Linux under Windows experience
You can install an X server on your Windows 10, and a superior terminal:
- First install a higher quality terminal by typing
sudo apt install xfce4-terminal
at the Bash command prompt in the Ubuntu window. - Next we need to set a few environment variables. Let us do this in such a way
that these variables will be set automatically in the future. We will put these
commands in your .bashrc file which is run whenever you start a shell).
If you installed WSL (not WSL 2) then run the following at your Ubuntu command line::
If you installed WSL2 then, instead of the line above, run:cd; echo "export DISPLAY=:0" >> ~/.bashrcecho "alias myterminal='xfce4-terminal 2>/dev/null &'" >> ~/.bashrccd; echo "export DISPLAY=\$(cat /etc/resolv.conf | grep nameserver | awk '{print \$2}'):0" >> ~/.bashrcecho "export LIBGL_ALWAYS_INDIRECT=1" >> ~/.bashrcecho "alias myterminal='xfce4-terminal 2>/dev/null &'" >> ~/.bashrc - Run the following command at your Ubuntu command-line:source ~/.bashrc
- Go here and download and install Vcxsrv as you would any Windows application.
Important: when installing VcXsrv, allow it through the firewall for both Private and Public networks (yes, this is little risky). - Then launch the X server by clicking on the XLaunch icon on your desktop.
- When you launch the X server, you will be presented with 4 screens.
If you installed WSL (not WSL2) then simply accept the defaults on the first 3 screens, and click Save configuration on the 4th page (and accept the default filename when saving).
If you installed WSL2 then simply accept the defaults on the first two pages, but click Disable access control on the 3rd page, and click Save configuration on the 4th page. - Right-click on the XLaunch desktop icon, click properities, edit Target so that it contains
"C:\Program Files\XcXsrv\xlaunch.exe" -run C:\Users\YOURUSERNAME\Documents\config.xlaunch
, and click apply. - Your startup workflow looks like this:
- Double click on the XLaunch icon to launch the X-Server.
- Double click on the Ubuntu icon to start your WSL terminal session.
- In the terminal window, issue the command myterminal. An xfce4 terminal will pop up.
- Work in the new terminal window that popped up. In fact, you can exit our of the old window if you wish, or continue using it as a second shell session.
(Optional) For more on Linux'izing your WSL see
See also this part of our page on Command-line Basics.
Install PyTorch
To install PyTorch in your Ubuntu installation (see the QUICK START LOCALLY section of pytorch.org for an up-to-date version of the last command below — be sure to first select Stable, Linux, pip, Python, and None):
sudo apt install python3-pip/usr/bin/pip3 install torch==1.6.0+cpu torchvision==0.7.0+cpu -f https://download.pytorch.org/whl/torch_stable.html --user
One runs one's program called, say, my-pytorch-program.py by entering python3 my-pytorch-program.py
at the shell prompt.