Instructions for compiling and installing NGSolve (and netgen) from source under WSL

The following sequence of posts will describe how to get NGSolve compiled and installed from source on Ubuntu (version 22.04) running under WSL (Windows Subsystem for Linux). Specifically, this is for WSL2.

I assume you are installing on your own windows machine. Thus, you should be able to run the “sudo” command (super user do). These instructions should also be useful for installing to a pure Linux Ubuntu. If you cannot run “sudo”, then some modification will be needed (I will try to point this out).

Note: this will take several posts to explain everything.

Preliminary steps:

  • install Windows Powershell (it’s useful to have anyway).
  • install Windows Terminal, which is used for running the Ubuntu system.
  • read up on the WSL system, e.g.
    Install WSL | Microsoft Learn

Get WSL installed on your machine. This is done by opening Powershell (with admin rights) and entering:
wsl --update

Next, install Ubuntu:
wsl --install -d Ubuntu
(This will ask you to create a username and password.)

As of this writing, this will install Ubuntu 22.04. You can also specify (some) previous version of Ubuntu.

Next, use “Windows Terminal” (with admin rights) to start the Ubuntu shell. You can then type:
lsb_release -a

to see the Ubuntu version.

Note: you might want to start with a fresh Ubuntu install at some point. In some cases, you may need to completely uninstall the WSL. If you do this, to re-install, run wsl --update again. Then, reinstall Ubuntu.

Before moving on, let’s make sure Ubuntu is fully up to date. Run this:
sudo apt update && sudo apt full-upgrade

After that, restart the Ubuntu shell and run:
sudo do-release-upgrade

A final note for this post: your Ubuntu distribution should be stored in:
\\wsl.localhost\Ubuntu\
(typing this in the file explorer should bring it up.)

and your home directory is:
\\wsl.localhost\Ubuntu\home\username

Next, run this:
sudo apt-add-repository universe
(generally good to have.)

Note: you can see a list of installed apps with:
apt list --installed

The purpose of this post if show how to get visualizations and GUIs to work with WSL. This is needed if you want to use NETGEN.
DISCLAIMER: this is highly system dependent. The instructions in this post work for me, but may require modification for you.

In addition, you need to add this line to your .bashrc script file (located in your Ubuntu home directory):
export DISPLAY=:0

Note: after modifying .bashrc, you might need to update the file privileges. Run this in your home dir after modifying the file:
chmod u=rwx,g=rwx,o=rwx .bashrc
(It’s possible you don’t need to do this.)

Go ahead and restart the Ubuntu shell.

Now run this:
sudo apt install mesa-utils
(this is for testing graphics.)

Next, run:
glxinfo

If that works, then it should output a lot of info. Else, it will give some kind of error.

If it worked, then run:
glxgears
(this should open a smaller window with some 3-D gears turning. They should not be turning slowly, but somewhat quickly.)

If you run into trouble, you may need to have XWindows installed on your system. When you’ve done this, there should be a XLaunch and Xming icon on your desktop. Running XLaunch should enable graphics from WSL-Ubuntu.

In my own experience, sometimes I have needed XWindows running, and other times the glxgears worked without it. I don’t really understand why.

If it still doesn’t work, then you might need to add this line to your .bashrc file:
export LIBGL_ALWAYS_INDIRECT=1
(put this after the export DISPLAY=:0 line)

I have had mixed experience with this. Sometimes I need that line, other times it seems unnecessary or causes an error. So I comment it out. Unfortunately, this is system dependent so I can’t help much here.

You may also need to change some settings in “NVIDIA Control Panel”. I didn’t have to, but your setup may require it.

The end goal of this post is to be able to run glxinfo and glxgears successfully. If those work, then NETGEN should work.

We now start installing some dependencies for NGSolve. Run this:
sudo apt-get update && sudo apt-get -y install python3 python3-distutils python3-tk libpython3-dev libxmu-dev tk-dev tcl-dev cmake git g++ libglu1-mesa-dev liblapacke-dev openmpi-bin libopenmpi-dev

If you want OpenCascade, then run:
sudo apt-get update && sudo apt-get -y install libocct-data-exchange-dev libocct-draw-dev occt-misc libtbb-dev libxi-dev

NOTE: if you are installing on a true Linux Ubuntu (not WSL), where you cannot use “sudo”, then you will need to get a sysadmin to install the above stuff. Some of it should already be installed, and the sysadmin shouldn’t object to installing the others.

Next, we need to get the intel oneAPI MKL installed. To do that, there is some preliminary stuff that needs to run that is described here (look under “Pre-installation Steps”):
https://www.intel.com/content/www/us/en/docs/oneapi/installation-guide-linux/2023-0/apt.html

Once you have setup the repository, do this:
sudo apt update

Now, do this:
sudo apt install intel-oneapi-mkl-devel

Next, we need pip and virtual environments. Run this:
sudo apt install python3-pip python3-venv

For doing parallel stuff, we need to install:
sudo apt install python3-ipyparallel

This can be tested by running:
ipcluster start

Note: some systems may require pip3 instead of pip. On my system, pip defaults to pip3.

Next, run:
pip install numpy scipy termcolor matplotlib
python3 -m pip install mpi4py

You can test the last package with:
mpiexec -n 4 python3 -m mpi4py.bench helloworld

For simplicity, we will not create a virtual environment to install NGSolve into (but this may be useful for you).

Note: the python3 version I am running is 3.10.6.

We are now ready to get NGSolve and compile it.

First, we unmount the C drive by running this:
sudo umount /mnt/c

We do this as a safeguard. It is conceivable that the install scripts may get confused by other python installations (such as Conda). I don’t for sure if that is the case here; this is a known issue with installing Firedrake. But don’t worry, we’ll remount the C drive later.

Next, get the source by running these lines:
export BASEDIR=~/ngsuite
mkdir -p $BASEDIR
cd $BASEDIR
git clone ``https://github.com/NGSolve/ngsolve.git`` ngsolve-src

Then to fetch the dependencies (Netgen), we must tell git to load the submodules:
cd $BASEDIR/ngsolve-src
git submodule update --init --recursive

Building from the source:

run this
mkdir $BASEDIR/ngsolve-build $BASEDIR/ngsolve-install

now change into the directory for builds and call cmake with a link to the source directory:
cd $BASEDIR/ngsolve-build
cmake -DUSE_MPI=ON -DUSE_MKL=ON -DUSE_OCC=ON -DUSE_MUMPS=ON -DUSE_UMFPACK=ON -DCMAKE_INSTALL_PREFIX=${BASEDIR}/ngsolve-install ${BASEDIR}/ngsolve-src

Note: do not add any flags for MKL (like the root directory). If you got oneAPI MKL installed, then the cmake script will find everything it needs.

Now, build it with:
make
Note: you can also run: make -j4 (gives it 4 processors to compile with, so is a little faster.)

This will take awhile. When it finishes, run:
make install

Note: make sure BASEDIR is set as above.

To finish the installation, run the following commands:
export NETGENDIR="${BASEDIR}/ngsolve-install/bin"
export PATH=$NETGENDIR:$PATH

export PYTHONPATH=$NETGENDIR/../`python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1,0,''))"`

You should put these commands into your .bashrc script.

Running it

You should now be able to type:
netgen
at the prompt and see the GUI load up.

You can test your installation with various demos. For example, type the following at the prompt:
cd ${BASEDIR}/ngsolve-install/share/ngsolve/py_tutorials/intro
python3 navierstokes.py

You can also run it with the GUI by typing:
netgen navierstokes.py

NOTE: At this time, I get a Pardiso error when running with NETGEN.

Lastly, we now remount the C drive:
sudo mount -t drvfs 'C:\' /mnt/c -o metadata

Keep it up to date

To update Netgen/NGSolve go to the source directory (${BASEDIR}/ngsolve-src) and fetch the latest sources.
git pull
git submodule update --recursive --init
Note: the second line is needed to update all dependencies provided as git submodules (such as Netgen).

Then, go to the build directory (${BASEDIR}/ngsolve-build) and build/install again:
make
(or run make -j4)
make install

Using Jupyter Notebook

First, install it by:
sudo apt update
sudo apt install jupyter
pip install --upgrade ipykernel
pip install webgui_jupyter_widgets
jupyter nbextension install --user --py widgetsnbextension
jupyter nbextension enable --user --py widgetsnbextension
jupyter nbextension install --user --py webgui_jupyter_widgets
jupyter nbextension enable --user --py webgui_jupyter_widgets

Then, you can test it by going to this directory:
cd $BASEDIR/ngsolve-src/docs/i-tutorials
and run:
jupyter notebook

You can then go to one of the example notebooks.

Note: if you did not install OCC, then you may need to include an extra command for the visualization. For example:
from netgen.geom2d import unit_square