This article provides step-by-step instructions on installing the OpenFOAM v2106 system package on a personal computer running on Ubuntu 21.04. The system package here means that you can now install OpenFOAM simply by using the apt install command as with other system-packaged Ubuntu applications — i.e. without the hassle of compiling from the source code.

The instructions given are for a Ubuntu 21.04 system, but it should also work for Ubuntu 20.04 and other Ubuntu-compatible distributions with Pop!_OS inclusive.

Tested environment

An x64-based laptop running on Ubuntu 21.04

  • Linux kernel version: 5.11.0-25-generic

An x64-based desktop running on Pop!_OS 21.04

  • Linux kernel version: 5.11.0-7620-generic

Preparation

1: Install the Open MPI package (and its dependencies) if not yet installed. Open MPI is an open source Message Passing Interface developed for high-performance computing.

$ sudo apt install openmpi-bin (1)
1 The leading prompt symbol $ indicates that you are typing at a terminal emulator (e.g. Ubuntu Terminal).

Check the installation by the accessibility to its mpiexec (or mpirun) executable:

$ which mpiexec
/usr/bin/mpiexec

2: Install ParaView by referring to the latest ParaView installation tutorial from Solvercube LearnPress (registration not required for free articles). Also check out channel videos from Solvercube Channel.

3: (Optional) In this article, we add a scenario that we would keep the previous stable release for a while due to, for example, ongoing projects.

The previous stable release used: OpenFOAM v2012

OpenFOAM repository for Ubuntu

Thanks to the Debian (science) maintainers' effort to improve the OpenFOAM packaging, you can now install OpenFOAM from the OpenFOAM repository for Ubuntu. To use the depository, you first need to add the signing key and the repository location to your system (required only once).

Install Curl (aka cURL or curl) by entering from a terminal as root (You may already have it installed):

$ sudo apt install curl

Being Curl ready, enter the following command to establish the repository:

$ curl -s https://dl.openfoam.com/add-debian-repo.sh | sudo bash
Detected distribution code-name: hirsute
Added /etc/apt/sources.list.d/openfoam.list
Importing openfoam gpg key... done
Overwrote /etc/apt/trusted.gpg.d/openfoam.gpg
Running apt-get update... done

The repository is setup! You can now install packages.

The system-packaged OpenFOAM comes in three (3) different compilations (i.e. sub-packages):

Nick name Package name Description

minimalist

openfoam2106

includes runtime only (ie, solvers, utilities, and libraries)

traditional

openfoam2106-dev

includes source files but without tutorials

everything

openfoam2106-default

includes everything

In this article, we will use the everything pack, and you can remove redundant data (e.g. tutorial files or source codes) anytime by deleting their containing folders.

(Optional) Previous stable release installation

You can skip this section if you either have the previous OpenFOAM already up and running or do not need an older version at all.

To install OpenFOAM v2012, enter from a terminal as root (i.e. using the sudo command):

$ sudo apt install openfoam2012-default

It is worth checking where the system installation puts your OpenFOAM files.

$ whereis openfoam
openfoam: /usr/lib/openfoam
$ ll /usr/lib/openfoam/
total 12
drwxr-xr-x   3 root root 4096 Jun 29 16:20 ./
drwxr-xr-x 119 root root 4096 Jun 29 16:20 ../
drwxr-xr-x  10 root root 4096 Jun 29 16:21 openfoam2012/

You may further check how the directories are structured — either with Files browser or using the tree command (To use the tree command, you need to install it by sudo apt install tree).

$ tree /usr/lib/openfoam/openfoam2012/ -d -L 2
/usr/lib/openfoam/openfoam2012/
├── applications
│   ├── solvers
│   ├── test
│   ├── tools
│   └── utilities
├── bin
│   └── tools
├── etc
│   ├── caseDicts
│   ├── codeTemplates
│   ├── config.csh
│   ├── config.sh
│   ├── templates
│   └── thermoData
├── META-INFO
├── platforms
│   ├── linux64GccDPInt32Opt
│   └── tools
├── src
│   ├── atmosphericModels
│   ├── combustionModels
│   ├── conversion
│   ├── dummyThirdParty
│   ├── dynamicFaMesh
│   ├── dynamicFvMesh
│   ├── dynamicMesh
│   ├── engine
│   ├── faOptions
│   ├── fileFormats
│   ├── finiteArea
│   ├── finiteVolume
│   ├── functionObjects
│   ├── fvAgglomerationMethods
│   ├── fvMotionSolver
│   ├── fvOptions
│   ├── genericPatchFields
│   ├── lagrangian
│   ├── lumpedPointMotion
│   ├── mesh
│   ├── meshTools
│   ├── ODE
│   ├── OpenFOAM
│   ├── optimisation
│   ├── OSspecific
│   ├── overset
│   ├── parallel
│   ├── phaseSystemModels
│   ├── Pstream
│   ├── randomProcesses
│   ├── regionFaModels
│   ├── regionModels
│   ├── renumber
│   ├── rigidBodyDynamics
│   ├── rigidBodyMeshMotion
│   ├── sampling
│   ├── semiPermeableBaffle
│   ├── sixDoFRigidBodyMotion
│   ├── sixDoFRigidBodyState
│   ├── surfMesh
│   ├── thermophysicalModels
│   ├── topoChangerFvMesh
│   ├── transportModels
│   ├── TurbulenceModels
│   └── waveModels
├── tutorials
│   ├── basic
│   ├── combustion
│   ├── compressible
│   ├── discreteMethods
│   ├── DNS
│   ├── electromagnetics
│   ├── financial
│   ├── finiteArea
│   ├── heatTransfer
│   ├── incompressible
│   ├── IO
│   ├── lagrangian
│   ├── mesh
│   ├── modules
│   ├── multiphase
│   ├── preProcessing
│   ├── resources
│   ├── stressAnalysis
│   └── verificationAndValidation
└── wmake
    ├── etc
    ├── makefiles
    ├── rules
    ├── scripts
    └── src

90 directories

Now, open your '~/.bashrc' file using a text editor (e.g. Gedit or SciTE, etc.)

$ gedit ~/.bashrc

and add the following line to the file:

source /usr/lib/openfoam/openfoam2012/etc/bashrc

Save and exit, and then reread the '~/.bashrc' configuration using the source command:

$ source ~/.bashrc

Check if a typical OpenFOAM application such as simpleFoam is accessible through the corresponding platform directory (e.g. 'linux64GccDPInt32Opt').

$ which simpleFoam
/usr/lib/openfoam/openfoam2012/platforms/linux64GccDPInt32Opt/bin/simpleFoam

Current stable release installation

To install OpenFOAM v2106, enter from a terminal as root:

$ sudo apt install openfoam2106-default

Have a check where the system installation puts your OpenFOAM files:

$ whereis openfoam
openfoam: /usr/lib/openfoam

You may further check how the directories are structured — either with Files browser or using the tree command, to the first directory level

$ tree /usr/lib/openfoam/openfoam2106/ -d -L 1
/usr/lib/openfoam/openfoam2106/
├── applications
├── bin
├── etc
├── META-INFO
├── platforms
├── src
├── tutorials
└── wmake

8 directories

then, to the second level if you wish:

$ tree /usr/lib/openfoam/openfoam2106/ -d -L 2
/usr/lib/openfoam/openfoam2106/
├── applications
│   ├── solvers
│   ├── test
│   ├── tools
│   └── utilities
├── bin
│   └── tools
├── etc
│   ├── caseDicts
│   ├── codeTemplates
│   ├── config.csh
│   ├── config.sh
│   ├── templates
│   └── thermoData
├── META-INFO
├── platforms
│   ├── linux64GccDPInt32Opt
│   └── tools
├── src
│   ├── atmosphericModels
│   ├── combustionModels
│   ├── conversion
│   ├── dummyThirdParty
│   ├── dynamicFaMesh
│   ├── dynamicFvMesh
│   ├── dynamicMesh
│   ├── engine
│   ├── faOptions
│   ├── fileFormats
│   ├── finiteArea
│   ├── finiteVolume
│   ├── functionObjects
│   ├── fvAgglomerationMethods
│   ├── fvMotionSolver
│   ├── fvOptions
│   ├── genericPatchFields
│   ├── lagrangian
│   ├── lumpedPointMotion
│   ├── mesh
│   ├── meshTools
│   ├── ODE
│   ├── OpenFOAM
│   ├── optimisation
│   ├── OSspecific
│   ├── overset
│   ├── parallel
│   ├── phaseSystemModels
│   ├── Pstream
│   ├── randomProcesses
│   ├── regionFaModels
│   ├── regionModels
│   ├── renumber
│   ├── rigidBodyDynamics
│   ├── rigidBodyMeshMotion
│   ├── sampling
│   ├── semiPermeableBaffle
│   ├── sixDoFRigidBodyMotion
│   ├── sixDoFRigidBodyState
│   ├── surfMesh
│   ├── thermophysicalModels
│   ├── topoChangerFvMesh
│   ├── transportModels
│   ├── TurbulenceModels
│   └── waveModels
├── tutorials
│   ├── basic
│   ├── combustion
│   ├── compressible
│   ├── discreteMethods
│   ├── DNS
│   ├── electromagnetics
│   ├── financial
│   ├── finiteArea
│   ├── heatTransfer
│   ├── incompressible
│   ├── IO
│   ├── lagrangian
│   ├── mesh
│   ├── modules
│   ├── multiphase
│   ├── preProcessing
│   ├── resources
│   ├── stressAnalysis
│   └── verificationAndValidation
└── wmake
    ├── etc
    ├── makefiles
    ├── rules
    ├── scripts
    └── src

90 directories

Now, open your '~/.bashrc' file using a text editor

$ gedit ~/.bashrc

and add the following line to the file:

source /usr/lib/openfoam/openfoam2106/etc/bashrc

Save and exit, and then reread the '~/.bashrc' configuration using the source command:

$ source ~/.bashrc

Check if a typical OpenFOAM application such as simpleFoam is now accessible:

$ which simpleFoam
/usr/lib/openfoam/openfoam2106/platforms/linux64GccDPInt32Opt/bin/simpleFoam

(Optional) Switching between different releases

This section is applicable if you have installed both the previous and the current stable releases of OpenFOAM and want to switch between them on demand.

For example, your local package folder contains both OpenFOAM v2012 and OpenFOAM v2106.

$ tree /usr/lib/openfoam/ -d -L 2
/usr/lib/openfoam/
├── openfoam2012
│   ├── applications
│   ├── bin
│   ├── etc
│   ├── META-INFO
│   ├── platforms
│   ├── src
│   ├── tutorials
│   └── wmake
└── openfoam2106
    ├── applications
    ├── bin
    ├── etc
    ├── META-INFO
    ├── platforms
    ├── src
    ├── tutorials
    └── wmake

18 directories

Open your '~/.bashrc' file to modify the path content for OpenFOAM by referring to the below example.

# OpenFOAM ##
##source /usr/lib/openfoam/openfoam2012/etc/bashrc
source /usr/lib/openfoam/openfoam2106/etc/bashrc

function ofUse() {
  thisVersion=$1
  currVersion=$(which simpleFoam | grep -Po '(?<=openfoam)\d\d\d\d')
  if [ $thisVersion = $currVersion ]; then
    echo ";-) You are using OpenFOAM $thisVersion"
  elif [ $thisVersion = 2012 ]; then
    source /usr/lib/openfoam/openfoam2012/etc/bashrc
    echo ":-D Now using OpenFOAM 2012"
    which simpleFoam
  elif [ $thisVersion = 2106 ]; then
    source /usr/lib/openfoam/openfoam2106/etc/bashrc
    echo ":-D Now using OpenFOAM 2106"
    which simpleFoam
  else
    echo ":-O Available versions are: 2012 2106"
  fi
}

Save and exit, and then reread the '~/.bashrc' configuration using the source command:

$ source ~/.bashrc

The 'source' line above the 'ofUse()' function for setting the OpenFOAM version as you log in. Check if the default version is correctly linked:

$ which simpleFoam
/usr/lib/openfoam/openfoam2106/platforms/linux64GccDPInt32Opt/bin/simpleFoam

Now check if the core part of the function switches between versions as expected:

$ ofUse 2012
:-D Now using OpenFOAM 2012
/usr/lib/openfoam/openfoam2012/platforms/linux64GccDPInt32Opt/bin/simpleFoam
$ ofUse 2106
:-D Now using OpenFOAM 2106
/usr/lib/openfoam/openfoam2106/platforms/linux64GccDPInt32Opt/bin/simpleFoam

The first and the last conditional blocks are for handling exceptions.

$ ofUse 2106
;-) You are using OpenFOAM 2106
$ ofUse 2006
:-O Available versions are: 2012 2106

TIP. After migrating from one version to another successfully, you may remove the obsolete release with the usual apt commands. For example, entering

$ sudo apt autoremove openfoam2012-default

will clear the system-packaged installation of OpenFOAM v2012.

Testing OpenFOAM

What follows are basic test routines for checking OpenFOAM installation.

Test routine 1

Open a terminal, copy & paste the whole command block to it; Press Enter after pasting if you are using Ubuntu 21.04:

mkdir -p ~/temp/test/openfoam
cd ~/temp/test/openfoam/
cp -r $FOAM_TUTORIALS/incompressible/simpleFoam/pitzDailyExptInlet/ .
cd pitzDailyExptInlet/
blockMesh | tee log.blockMesh
simpleFoam | tee log.simpleFoam
(1)
1 An extra blank line is intended for the last command line to be parsed on pasting.

The solution will converge at about 783 iterations taking about 13 seconds of clock time on a typical Intel Core i7 laptop.

 :

Time = 783

smoothSolver:  Solving for Ux, Initial residual = 0.000144719, Final residual = 1.00012e-05, No Iterations 2
smoothSolver:  Solving for Uy, Initial residual = 0.000997044, Final residual = 7.16323e-05, No Iterations 2
GAMG:  Solving for p, Initial residual = 0.00324997, Final residual = 0.000290901, No Iterations 7
time step continuity errors : sum local = 0.00136169, global = 9.19604e-05, cumulative = -1.44762
smoothSolver:  Solving for epsilon, Initial residual = 0.000207944, Final residual = 1.31933e-05, No Iterations 2
smoothSolver:  Solving for k, Initial residual = 0.00051041, Final residual = 3.78922e-05, No Iterations 2
ExecutionTime = 12.93 s  ClockTime = 13 s


SIMPLE solution converged in 783 iterations

 :

To post-process the result, run ParaView by entering from the OpenFOAM case directory:

$ touch pv.foam
$ paraview --data=pv.foam &
shot0101

Test routine 2

Below is for testing a parallel run involving more frequent data writing to disk. Change the value for the numproc parameter if you have less than four (4) threads available.

numproc=4
mkdir -p ~/temp/test/openfoam
cd ~/temp/test/openfoam/
cp -r $FOAM_TUTORIALS/incompressible/pimpleFoam/RAS/oscillatingInletACMI2D/ .
cd oscillatingInletACMI2D/
blockMesh | tee log.blockMesh
cp -r 0.orig/ 0/
sed s/"numberOfSubdomains"/"numberOfSubdomains  ${numproc};\/\/"/ ./system/decomposeParDict > temp.$$
mv temp.$$ ./system/decomposeParDict
decomposePar | tee log.decomposePar
time mpirun -np ${numproc} pimpleFoam -parallel > log.pimpleFoam
(1)
1 An extra blank line is intended for the last command line to be parsed on pasting.

Note that I ran the parallel run syntax in the foreground with the time command attached, so wait until the console returns with an execution time report.

If having a somewhat mysterious Invalid MIT-MAGIC-COOKIE-1 key message, please ignore it for the time being (This issue is gone in Ubuntu 21.04).

This run would take about 117 seconds of clock time using four (4) processors on a typical Intel i7 laptop.

real	1m56.705suser	7m39.171ssys	0m4.689s

To view the result from a decomposed run, you first need to reconstruct the partitioned data

$ reconstructPar | tee log.reconstructPar

and then run ParaView:

$ touch pv.foam
$ paraview --data=pv.foam &
shot0102

Post-installation configuration

OpenFOAM script for ParaView

While OpenFOAM defines a solution case via a hierarchy of dictionary structure, ParaView requires a single data file or an index file containing the list of multiple data files for loading the data set for visualisation. To work around this, you have to create a dummy case file with the file extension of '.foam' (e.g. 'my.foam', 'pv.foam', etc.) from under the case root directory as illustrated in the above test scripts.

touch pv.foam
paraview --data=pv.foam &

Because you will use this command pair frequently in OpenFOAM modelling, you have better put this into an executable script.

1: Either using a text editor or short-circuiting via the echo 'blah blah' > file trick, create a script file with a name, say, 'ofpvfoam'.

echo '#!/usr/bin/bash
touch pv.foam
paraview --data=pv.foam &
' > ofpvfoam
(1)
1 An extra blank line is intended for the last command line to be parsed on pasting.

2: Make it "executable" using the chmod +x command

$ chmod +x ofpvfoam

and then place it under your custom script archive (e.g. '~/scripts/').

$ mkdir ~/scripts
$ mv ofpvfoam ~/scripts/

3: Now, open your '~/.bashrc' file to add the path to your custom scripts by referring to the below example.

# Path to Hawk's custom scripts ##
export PATH=/home/hawk/scripts:${PATH}

Save and exit, and then reread the '~/.bashrc' configuration using the source command:

$ source ~/.bashrc

4: Check if the above script works as expected by entering the name of the script file — from the case root directory of the previous test routines.

$ ofpvfoam
shot0103

TIP. If you run this script from anywhere else but an OpenFOAM case root directory, ParaView will run but issue error messages. You may extend the executable script by adding a conditional else block to remedy this.

#!/usr/bin/bash
# launches paraview from a case root directory
if [ -d constant ]; then
  touch pv.foam
  paraview --data=pv.foam &
else
  paraview &
fi

Notes on openfoam-selector

For organisations requiring tighter quality assurance on software version control, OpenFOAM now provides a system administrator tool called openfoam-selector. This utility selects a specific version to use both system-wide and for individual users from the system-installed OpenFOAM repositories and makes it available from the next full login.

$ openfoam-selector --help
/usr/bin/openfoam-selector options:

Options for OPENFOAM versions:

--register <name>     Register an OPENFOAM version with the central
                      openfoam-selector database.  Requires use of the
                      --source-dir option.
--source-dir <dir>    Used with --register, indicating that <dir> is
                      where the etc/bashrc file can be found.
--unregister <name>   Remove an OPENFOAM version list from the
                      central openfoam-selector database.

Options for system administrators:

--system              When used with the --set and --unset options,
                      act on the system-wide defaults (vs. the
                      per-user defaults).  When used with --query, only
                      show the site-wide default (if there is one).
--user                When used with the --set and --unset options,
                      act on the per-user defaults (vs. the
                      site-wide defaults).  When used with --query, only
                      show the per-user default (if there is one).

Options for users:

--list                Show a list of the currently registered OPENFOAM
                      versions.
--set <name>          Set <name> to be the default OPENFOAM selection.
--unset               Remove the default OPENFOAM selection.
--query               Shows the current default OPENFOAM selection.
--yes                 Assume the answer is "yes" to any question.
--no                  Assume the answer is "no" to any question.
--verbose             Be verbose about actions.
--silent              Print nothing (not even warnings or errors;
                      overrides --verbose)
--version             Display the version of /usr/bin/openfoam-selector.

Regardless of whether you are a user or an administrator, you would find openfoam-selector-menu more friendly to use.

$ openfoam-selector-menu
Current system default: <none>
Current user default:   <none>

    "u" and "s" modifiers can be added to numeric and "U"
    commands to specify "user" or "system-wide".

1. openfoam2012
2. openfoam2106
U. Unset default
Q. Quit

Selection (1-2[us], U[us], Q):

That said, using the openfoam-selector tool may seem overkill if you are the sole user of the machine (e.g. you are using your personal computer). The conventional 'etc/bashrc' sourcing method is swifter in that it allows you to switch between versions without restarting your login.