Installing PyMC3 and Theano on Windows 10* using 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:
- Download g++: This one’s easy. Go for:
sudo apt-get install g++
- Download and install Miniconda: Download the latest version of miniconda, install it, and then immediately remove the download in 3 lines using:
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!
- Create and activate a conda virtual environment: I’ve called my virtual environment pm3, but call yours whatever you want.
conda create -n pm3 python
conda activate pm3
- Install packages via conda: mkl will impove PyMC3’s performance immensely so do make sure to grab this in advance. The remaining packages (NumPy, SciPy and Jupyter) are just useful to have installed beforehand, but you can probably skip them at this point if you’d like.
conda install mkl-service numpy scipy jupyter
conda install -c conda-forge pymc3
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:
- Define the browser environment variable: Make sure this is the path to your browser of choice. I use Firefox, so mine was:
export BROWSER="/mnt/c/Program Files/Mozilla Firefox/firefox.exe"
- Set the redirect method: Basically just adjusting Jupyter’s configurations here to redirect effectively:
jupyter notebook --generate-config
echo c.NotebookApp.use_redirect_file = False >> ~/.jupyter/jupyter_notebook_config.py
Done! Running ‘jupyter notebook’ in your virtual environment within miniconda should automatically launch it in your browser.
4b. Create a symlink for your Windows Jupyter Folder within Ubuntu
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.
- Navigate to Home Directory and set up the link: Replace the path to your own folder (e.g. ‘/Users/Your_name/Whatever’) here, and name the folder whatever you like:
ln -s /mnt/c/Users/Angus/Documents/Python/Jupyter_Notebooks/ Jupyter_Notebooks
You should now see a folder called Jupyter_Notebooks (or whatever you called it) when Jupyter opens!