Compiling the code from OpenFOAM source code on Windows Subsystem for Linux

  • 2020/12/01
  • 日本ESI

Recommendation: Please update your windows version to the latest one or at least version 19.03 in order to run parallel with OpenMPI library from the system version.

Step 1. Enable WSL 1. Open PowerShell as Administrator and run:

$> dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart


Then reboot the computer (in this tutorial, we skip to install WSL2 that required higher windows 10 build version) to apply the setting.

Step 2. After rebooting the windows, let us install Linux Distro Packages.

Here, we can choose to install Ubuntu through Microsoft Software Center or PowerShell.

Installing Ubuntu versions From Microsoft Software Center.

Open the Microsoft Store and select your favorite Linux distribution.


Notes: In case you get error of 0x80070005 as the below image when installing Ubuntu through software center. Try to install through PowerShell as described in step 3, If you do not encounter that error, please skip step 3 and go over to step 5


Step 3: In case of cannot installing Ubuntu through Microsoft Center, following the steps below

We can download Ubuntu distro through PowerShell or direct link as our preference.

  • Downloading Ubuntu distro by using PowerShell

Open PowerShell with administrator and run one of three commands below to download Ubuntu 2004:

$> Invoke-WebRequest -Uri -OutFile Ubuntu.appx -UseBasicParsing

In case we want to download whether Ubuntu 18.04 LTS, type the command below into the PowerShell

$> Invoke-WebRequest -Uri -OutFile Ubuntu.appx -UseBasicParsing 

With Ubuntu 16.04 LTS, type the command below into the PowerShell

$> Invoke-WebRequest -Uri -OutFile Ubuntu.appx –UseBasicParsing

  • Download Ubuntu distro by using direct link

Please click on one of three links below to download the Ubuntu version you prefer:

For Ubuntu 2004:

For Ubuntu 1804:

For Ubuntu 1604:

Step 4: Installing your distro after downloading your prefer packages

Navigating to the directory containing downloaded Ubuntu packages, then enter the below command:

$> Add-AppxPackage .\Ubuntu.appx

Step 5: Running your ubuntu bash

After installing your Ubuntu subsystem, click on Start start and type "Ubuntu" to search your Ubuntu distro and click on its symbol to start it


Some noticed points: During running Ubuntu bash, if we have encountered the error: WslRegisterDistribution failed with error: 0x80370102 and error 0x80070002.This NEEDS TO BE DISABLED for the WSL 2 to work correctly, apparently. Enter command below to set default WSL 1 on PowerShell then restart the desktop:

$> wsl --set-default-version 1

Step 6. Installing system requirements for Ubuntu subsystem windows

First Installing the dependencies by entering three 3 commands to your Ubuntu bash:

$>   sudo apt-get update


$>   sudo apt-get install build-essential flex bison cmake zlib1g-dev libboost-system-dev libboost-thread-dev libopenmpi-dev openmpi-bin gnuplot libreadline-dev libncurses-dev libxt-dev


$>   sudo apt-get install qt4-dev-tools libqt4-dev libqt4-opengl-dev freeglut3-dev libqtwebkit-dev


Next, Installing parallelisation OpenMPI library together with Scotch and GCAL libraries can be compiled from the ThirdParty directory or the system versions can be installed:

$>   sudo apt-get install libscotch-dev libcgal-dev


Further Information check:

To check version of MPI library installed, run: mpirun --version (for Ubuntu 16.04 it will be version 1.10.2)

To check compiler version installed, run: gcc --version (for Ubuntu 16.04 the system version will be 5.4.0)

System version of cmake is 3.5.2 for Ubuntu 16.04 (cmake --version)

System version of CGAL library is 4.7.4 for Ubuntu 16.04 (sudo apt-cache show libcgal-dev)

System version of scotch library is 5.1.12b for Ubuntu 16.04 (sudo apt-cache show libscotch-dev)

The GCC compiler is used as the default, with additional support for Intel ICC (from version 15.0.3) and LLVM Clang (from version 3.4)

Step 7: Build the code from source code in windows subsystem for Linux

All steps above are obligatory steps for compiling OpenFOAM source code. In this steps, we need to compile the source code only.

At first, download the source code version from the site:

Here, we choose to download OpenFoam-v2006.tgz and ThirtParty-v2006.tgz in this blog. Extract those files to user home directory, eg. $HOME/OpenFOAM in Ubuntu bash. Then Source the OpenFOAM environment by executing the command below:

$> source ~/OpenFOAM/OpenFOAM-v2006/etc/bashrc
Compiling OpenFOAM
  • Test the system readiness (optional, not supported for cross-compilation)

$> foamSystemCheck

  • Manually navigate to~/OpenFOAM/OpenFOAM-v2006 or just type the command below.

$> foam

  • Compile OpenFOAM

$> ./Allmake -j -s -q -l

Finally, we have finished OpenFOAM compilation, let's take a first calculation in your Windows Subsystem for Linux. Remembering that we need to use Paraview windows version to visualize our results.