Installing PyMC3 and Theano on Windows 10* using WSL

Jul 10, 2020 PyMC3 Theano WSL

I really, really like the PyMC3 package for proabilistic programming in Python. It seems to hit a sweet spot of allowing near endless customisation of bespoke Bayesian models, while maintaining relatively concise and readable syntax. It samples super fast, and the PyMC3 community are great.

The problem is that installing it successfully on Windows 10 can be a serious headache. There seems to be various issues successfully installing the g++ compiler and connecting it with Theano, and/or issues with multiprocessing making sampling incredibly slow (if it runs successfully at all) and limited to one core. Trying to install it last week, I felt I’d somehow managed to replicate every Windows-related install error reported across GitHub issues, StackOverflow, and the PyMC3 Discourse!

The solution for me was to install PyMC3 within Ubuntu using Windows Subsystem for Linux (WSL), thereby bypassing weird errors and conflicts within Windows. I’m not overly tech-savvy, so maybe there is a nice way to have an entirely Windows-based solution. I tried so many different ways and for the life of me I couldn’t do it!

Here’s the process I used, step by step:

1. Install WSL and Ubuntu

This is a relatively straight-forward process in itself following Microsoft’s own guide, so I won’t bother repeating it. Feel free to go with whatever distribution of Linux you prefer, but I stuck with Ubuntu and it’s working great.

If you’re able, go ahead and upgrade to WSL2. On my machine some Windows requirements for the ugrade weren’t available quite yet, so I’m just running WSL1. I skipped the part of their instructions involving this upgrade.

2. Install g++ and miniconda

Now you’ll want to open up Ubuntu and install some pre-requisites: g++ and miniconda. I’m not installing a full distribution of Anaconda because it’s horribly bloated. Go in either order you like:

3. Create a fresh environment and install your packages

Strictly speaking, you could just start installing PyMC3 (in the base Miniconda environment) right away.

But I’ve found virtual environments really useful if I mess up my install process or find my package management getting out of hand. You can just delete everything and start again!

4. Optional Steps for Jupyter Notebooks

For data analysis, I mostly work out of Jupyter Notebooks mostly (hence including it in the instructions above). I wanted to get Jupyter working nicely too.

Full credit here (and above) should go to Emily Kauffman, Prayson Wilfred Daniel & Michael C. Grant in this GitHub Gist - I’m basically just reproducing their instructions.

4a. Set up your browser to open Jupyter automatically

In Windows, typing ‘jupyter notebook’ will automatically open it up in your default browser. If you want the same behaviour your Ubuntu-based conda virtual environment, you’ll want to:

Done! Running ‘jupyter notebook’ in your virtual environment within miniconda should automatically launch it in your browser.

If you’ve got an existing folder where you store your Jupyter Notebooks in Windows, creating a link between this and Ubuntu is really helpful.

You should now see a folder called Jupyter_Notebooks (or whatever you called it) when Jupyter opens!