Link Search Menu Expand Document

C/C++ Jupyter Notebook using xeus-cling - Windows WSL Setup

Jun 27, 2021 jupyter-notebook cpp

Jupyter notebook supports C/C++ using the project. xeus-cling is a Jupyter kernel for C++ based on the C++ interpreter cling and the native implementation of the Jupyter protocol xeus.

But this has been built mainly for Linux and OS X platforms and doesnt support Windows Jupyter notebook.

The below steps helps to setup the environment on Windows using WSL

1. Enable Ubuntu on WSL

If you have not used WSL before on windows, please follow the steps from Microsoft here

Install Ubuntu for Windows from Windows Store. I’m using Ubuntu 18.04.

Other supported Linux flavours can be used. I have not yet tried though.

2. Install Miniconda

Install miniconda from https://conda.io/miniconda.html

Please download Linux version for your 32/64 bit system accordingly.

I downloaded Miniconda3-latest-Linux-x86_64.sh for my 64-bit WIndows.

Install miniconda

$ ./Miniconda3-latest-Linux-x86_64.sh

3. Conda, Jupyter Notebook, Xeus-Cling setups

Init and update conda. Init will add the required bootstrapping in your bashrc

conda init
conda update conda

Create a environment for cpp

conda create -n cpp

Activate the environment

conda activate cpp

Install Jupyter notebook, xeus-cling and required cpp extensions

conda install notebook
conda install -c conda-forge xeus-cling
conda install -c conda-forge jupyter_contrib_nbextensions
conda install -c conda-forge jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user

4. Write Notebooks

Now that everything is setup, choose a directory for your notebooks

I choose ~/cpp-books

cd ~/cpp-books
conda activate cpp

Run Jupyter notebook with --no-browser option as we are in WSL and paths are not valid directly to open a page in windows.

jupyter notebook --no-browser

5. Open Windows Browser fix

Generate a editable default config for Jupyter

jupyter notebook --generate-config
echo c.NotebookApp.use_redirect_file = False >> ~/.jupyter/jupyter_notebook_config.py

Set your desired Windows Browser variable like

export BROWSER="/mnt/c/Program Files/Mozilla Firefox/firefox.exe" 

or

export BROWSER="/mnt/c/Program Files/Google/Chrome/Application/chrome.exe"

With above config and BROWSER env variable set, no need to add --no-browser option. Just run

jupyter notebook

6. Create a Windows Desktop Shortcut

Inside WSL create a script file to set the environment and run Notebook

$ cat ~/note

#!/bin/bash                                                                                                             
cd ~/cpp-books
source ~/miniconda3/etc/profile.d/conda.sh 
conda activate cpp
export BROWSER="/mnt/c/Program Files/Mozilla Firefox/firefox.exe"                                                       
jupyter notebook

Change paths accordingly

Now from Windows Desktop, Right click and Create a new Shortcut.

Set the location to

C:\Windows\System32\wsl.exe bash -c "source /home/<user>/note"

Now you can open the shortcut to view your notebooks on Windows browser

Additionally, in the shortcut properties change to start minimised.


Comments






Johannes: September 15th, 2021 11:57

Thanks a lot for this very nice step-by-step tutorial!

I got a little into trouble because a first tried to generate the “~/note” file from within Windows (I thought this is faster… ). However, Windows and Linux are using different endOfLine-encodings. Notepad++ was fortunately able to convert them.