Installing FEniCS to run with Jupyter Notebook

So, I have some PDEs that I need to solve numerically, and came across the FEniCS package. Problem is, the thing just did not want to work on my Mac. I tried several ways, including I even tried to do the same on an Ubuntu installation, with no luck. Although I could import dolfinx successfully in the command line once the environment had been activated, I had no joy importing it in Jupyter Notebook or VSCode.

During the installation, I saw that there were several inconsistencies in my Anaconda installation, so I tried to remove them with

conda install anaconda

This took ages (more than 40 hours), so prepare yourself for a long haul. After this, I installed Notebook and then tried

conda create -n fenicsx-env
conda activate fenicsx-env 
conda install -c conda-forge fenics-dolfinx mpich pyvista

Still no luck when trying

python3 -c "import dolfinx"

(In a previous installation this was actually successful, but I still couldn’t import it from Notebook.)

In, I saw that someone had some success by launching the environment in Anaconda Navigator, which I now attempted, and which gave me some errors on start up. To fix this, I tried

conda update anaconda-navigator
conda update navigator-updateranaconda-navigator --reset

which I found at Note that this can also take a bloody long time. It also does not seem to work… In the meantime, I was getting desperate and started throwing commands around. I tried (from

conda create -n fenicsproject -c conda-forge python=3.11 jupyter fenics

(this will depend on your version of Python, of course). Now, I activated the environment and tried the import:

conda activate fenicsprojectpython3 -c "import fenics"

This ran without any errors, which at least gave me hope. Next, I opened Notebook and tried

from fenics import *

Success! For the first time, I did not get a “No module…” type error. Now I have to see if everything works as advertised. By the way, what I would have tried next would have been to do the install with Docker (which is actually recommended in the FEniCS manual). I should also mention that I did a pip3 install, which did not work. I will still see if this method is successful on Ubuntu as well, and let you know.