Setting Up a Spyder Environment with WSL
In this tutorial, we will be setting up a Python 3 and Spyder environment on the Windows Subsystem for Linux (WSL). While Python and the Spyder IDE is available to Windows, it is advantageous to run it from WSL due to the Linux terminal, easier compiler access and various Linux-exclusive packages, among other things.
Since WSL only provides a terminal for the Linux subsystem, it is not possible to run GUI (windowed) apps natively without a few tweaks (at least, for now). In this guide, we will be using an X11 client on Windows 10 to enable windowed Linux apps with WSL.
You can also use this tutorial to enable other GUI apps to run on WSL. You can skip the sections on installing Python and Spyder.
Windows Subsystem for Linux (WSL)
The Windows Subsystem for Linux provides a compatibility layer to run Linux programs on Windows. There are multiple Linux distributions (paid and free) to choose from such as Ubuntu, Debian, Fedora, CentOS, etc.
Requirements
The only requirement to install WSL is 64-bit Windows 10 (version 1607 and later). To check that you meet the requirement for WSL, go to Settings > System > About. After the installation of both WSL and the Python packages, the whole environment will take up around 5.5 GB of storage space.
Installing WSL
For this tutorial we will be installing Ubuntu 18.04 LTS as the Linux distribution of choice.
1. From search, type “features” and open “Turn Windows Features On or Off”
2. From the list, tick on “Windows Subsystem for Linux” and click ok. Restart the computer when prompted.
3. Open the Microsoft Store app, and search for “Ubuntu 18.04 LTS”
4. Open the Ubuntu 18.04 LTS app from the start menu to finish the setup. Finish setting up WSL by entering a username and password. Remember these as we will be using them for installing the necessary applications later.
5. Run the following commands on the terminal after successfully setting up Ubuntu:
sudo apt-get updatesudo apt-get upgrade
6. For a seamless experience, I suggest turning on the “Use Ctrl+Shift+C/V as Copy/Paste” option. Do this by right clicking on the Ubuntu app title bar, and clicking on Properties. The following prompt will appear:
Installing Python through Miniconda
- First, we will download the installer to our Linux system. Enter:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
2. Run the installer through this command:
bash Miniconda3-latest-Linux-x86_64.sh
3. Restart the Ubuntu terminal to effect the changes.
Note: Every time you open the Ubuntu app, it will always default to use python from Miniconda. If you want to use the python from the system, enter:
conda deactivate
4. Install matplotlib, spyder and scipy from conda. Run the following command:
conda install matplotlib spyder scipy
5. We will be installing additional packages to make Spyder work. Run the following commands:
sudo apt-get install libxcursor-devsudo apt-get install alsasudo apt install libegl1-mesa libegl1
6. After installing the above packages, we still need to perform a few steps to get Spyder (and other GUI apps) to run on our Linux subsystem. Follow the next steps.
Setting up X11 on Linux and Installing the Client on Windows
- Install the X11 client on Windows 10 by downloading VcXsrv from this link: https://sourceforge.net/projects/vcxsrv/ and running the installer on your system.
2. After installation, we will be now setting up the X11 environment on Linux. In the terminal, type:
nano ~/.bashrc
3. Scroll to the bottom part of the file by using the down arrow key. Copy the following text and paste it to the file (Ctrl+Shift+V) or you could just type it, if you prefer:
DISPLAY=:0export DISPLAYLIBGL_ALWAYS_INDIRECT=1Export LIBGL_ALWAYS_INDIRECT
4. After typing the text, press Ctrl-X, then press y and the enter key. This will save the .bashrc file. Restart the Ubuntu terminal to effect the changes. This step is important so you don’t have to export those bash variables everytime.
Launching Spyder (and enabling GUI apps)
- Everytime you open a GUI app in WSL, you must open the XLaunch app (from VcXsrv). You can launch it from the start menu or desktop.You must run XLaunch (and to the succeeding steps) everytime you restart your computer or close XLaunch.
- Select “Multiple windows” and set the display number to 0. Click Next. For the succeeding options, just leave them default (keep on clicking next). Finally, click on Finish. The app will be running in the background (check the taskbar icon).
3. We can now launch spyder from WSL. Open a WSL terminal, and run:
spyder
This will open the Spyder IDE.
Congratulations, you have successfully setup a Python 3 and Spyder environment for use with WSL. If you are still having trouble with running Spyder, you may be missing some additional packages. In this case, you can search for solutions online.