Installing FEniCS on macOS using conda

FEniCS is an open-source finite element library for solving partial differential equations (PDE) by finite element methods (FEM). The library has a Python front-end. Here I give some notes on how to install FEniCS using the very popular conda package management system. Although tested only on macOS, the instructions should work also for Linux, with minor adjustments.

Installing conda

First we need to install conda. Here we use the Miniconda distribution. If your already have conda installed on your system (either through Miniconda or the larger Anaconda distribution), you can skip this section.

First we download Miniconda. Open a terminal and execute the following commands:

> cd ~/Downloads
> curl -O

You can also chose to download from using your browser. If you’re on Linux, you chose the Linux version instead.

We can now run the installation script:

> bash ~/Downloads/

The script will ask you to read the license agreement and then to chose the installation folder. Throughout, I assume you’re using the default location, which is ~/miniconda3.
That’s it, conda is now installed!

Creating a new conda environment

It is a good idea to keep your FEniCS installation in a separate conda environment. To create a new environment for FEniCS, first activate the root environment:

> source ~/miniconda3/bin/activate root

Then create an environment called fenics by the following command

> conda create --name fenics

Next, we activate the new environment:

> source activate fenics

Adding preferred installation channel

The conda distribution of FEniCS is using the conda-forge installation channel. Therefore, this should be the preferred channel for your new fenics environment. To do so, we create a .condarc file for the environment:

> echo "channels:\n - conda-forge\n - defaults\n" > ~/miniconda3/envs/fenics/.condarc

This creates a .condarc file for the fenics environment, with conda-forge as the primary installation channel.

Installing FEniCS

We are now ready to install FEniCS. We start by updating conda itself:

> conda update conda

In addition to FEniCS, it wise to install a couple of other useful packages, such as matplotlib, numpy, scipy, and jupyter. In particular, jupyter allows you to run Python notebooks using your browser, which is an extremely nice front-end to Python. Anyway, here is the command to install the whole bunch:

> conda install numpy fenics scipy matplotlib jupyter

You will be asked a question about also installing additional dependencies. Just accept this and wait a while for the installation to finish. FEniCS is now installed on your computer! To try your installation, execute the following command:

> python -c "import fenics"

You should not get an error. For more info on getting started, see the FEniCS tutorial.

Recall that FEniCS is installed in a separate conda environment, so every time you open a new terminal, you need to activate the environment by executing

> source ~/miniconda3/bin/activate fenics

If you like, you can add this line to your .bashrc or .profile file, so that it is automatically run in new terminals.