The FREESURFER_HOME environment variable is not set

If you have a question or issue with MNE-Python, please include the following info:

  • MNE-Python version: 0.22.1
  • Python: 3.9.2
  • operating system: Mac OS 10.15.7

Please also provide relevant code snippets – ideally a minimal working example (MWE). To get the correct formatting, paste your Python code, then select it, and click on the Preformatted text toolbar button.

:point_right: :point_right: :point_right: Please edit or remove the above text before submitting your posting. :point_left: :point_left: :point_left:

Hi everyone,

I am very new to MNE, sorry for my naive question.
I am trying to do a beamformer on my MEG data. So far I have used FreeSurfer to do fully automatic reconstruction on the T1 data. But I do not see any BEM data in the output.
I tried to use the function mne.bem.make_watershed_bem(), but I got this error:

Traceback (most recent call last):

  File "<ipython-input-30-5a9ec101536f>", line 1, in <module>
    mne.bem.make_watershed_bem(subject)

  File "<decorator-gen-97>", line 24, in make_watershed_bem

  File "/Users/andrewchang/opt/anaconda3/envs/mne/lib/python3.9/site-packages/mne/bem.py", line 1103, in make_watershed_bem
    env, mri_dir, bem_dir = _prepare_env(subject, subjects_dir)

  File "/Users/andrewchang/opt/anaconda3/envs/mne/lib/python3.9/site-packages/mne/bem.py", line 1630, in _prepare_env
    fs_home = _check_freesurfer_home()

  File "/Users/andrewchang/opt/anaconda3/envs/mne/lib/python3.9/site-packages/mne/utils/check.py", line 694, in _check_freesurfer_home
    raise RuntimeError(

RuntimeError: The FREESURFER_HOME environment variable is not set.

Any advice would be much appreciated!

Hello @andrewchang123 and welcome to the forum! :partying_face:

FREESURFER_HOME should normally be set if FreeSurfer was installed correctly. How do you run your MNE-Python script?

If you open a new terminal window, do you get something like the following output?

Last login: Tue Apr 20 08:43:24 on ttys006
-------- freesurfer-Darwin-OSX-stable-pub-v6.0.0-2beb96c --------
Setting up environment for FreeSurfer/FS-FAST (and FSL)
FREESURFER_HOME   /Applications/freesurfer
FSFAST_HOME       /Applications/freesurfer/fsfast
FSF_OUTPUT_FORMAT nii.gz
SUBJECTS_DIR      /Applications/freesurfer/subjects
MNI_DIR           /Applications/freesurfer/mni

If not, you should run the following commands (assuming that FreeSurfer is installed in /Applications/freesurfer):

export FREESURFER_HOME=/Applications/freesurfer
source $FREESURFER_HOME/SetUpFreeSurfer.sh

Thank you very much for your reply @richard !
But the freesurfer message won’t automatically pop-out when I open a terminal window. I have to manually enter the commands every time to have the output.
Does it mean that the freesurfer was not properly installed?

export FREESURFER_HOME=/Applications/freesurfer/7.1.1
source $FREESURFER_HOME/SetUpFreeSurfer.sh 

-------- freesurfer-darwin-macOS-7.1.1-20200811-8b40551 --------
Setting up environment for FreeSurfer/FS-FAST (and FSL)
FREESURFER_HOME   /Applications/freesurfer/7.1.1
FSFAST_HOME       /Applications/freesurfer/7.1.1/fsfast
FSF_OUTPUT_FORMAT nii.gz
SUBJECTS_DIR      /Applications/freesurfer/7.1.1/subjects
MNI_DIR           /Applications/freesurfer/7.1.1/mni

I run the MNE-Python under Spyder under a conda environment “mne”.
I tried run the commends under both “base” and “mne” environment, but none of them work.

Which shell are you using? bash? In that case, add the following to your ~/.bashrc:

export FREESURFER_HOME=/Applications/freesurfer/7.1.1
export SUBJECTS_DIR=$FREESURFER_HOME/subjects
source $FREESURFER_HOME/SetUpFreeSurfer.sh

This should initialize FreeSurfer in every new shell / terminal window.

Hi @richard .
I did not realize that I was using zsh. Now I can switch that to bash.

Could you kindly show me how to add these comments to .bashrc?
Thank you very much. Sorry I am very new to terminal!

No need to, I’m using zsh myself (with oh my zsh!); I simply assumed you’re on bash because that was the default in macOS until recently.

You can just add the lines I posted above to your ~/.zshrc, before the lines added by Anaconda/Miniconda. My ~/.zshrc looks like this:

...
export FREESURFER_HOME=/Applications/freesurfer
export SUBJECTS_DIR=$FREESURFER_HOME/subjects
source $FREESURFER_HOME/SetUpFreeSurfer.sh

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/hoechenberger/miniforge3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
...
1 Like

Thank you @richard , now the freesurfer output message will pop out when I open the terminal under zsh shell!
But the same “The FREESURFER_HOME environment variable is not set.” error message is still there…

Will that has anything to do with running the MNE-Python under Spyder under a conda environment “mne”?

I guess it depends on how you start Spyder …
If you open a terminal, activate the mne conda environment, and then run spyder from the same command line, all should be good (at least in theory)

Thanks @richard it works now!! :smiley:
Not sure why it cannot work if I launch Spyder via Anaconda

For the record, execute these lines in the terminal

conda activate mne
spyder

Probably because the FreeSurfer initialization script is not run then.

Glad to hear it’s working now!

1 Like

I checked on Linux, and the spyder console is a bash console that has run .bashrc (I ran !env in spyder’s ipython console to look for env variables that I know are only defined if my .bashrc has been run). So you can check that (and also things like !echo $SHELL) to help you figure out if it’s possible to get it to work when launching spyder via GUI instead of from a terminal. But at least it works from a terminal :slight_smile:

2 Likes

Dear @richard and @drammock ,

I’d like to follow up because I ran into the same error while initializing the coregistration GUI. However, the solution here doesn’t seem to work for me… I wonder if it’s because of how I installed MNE python and FreeSurfer on my Windows 10 machine:

  1. I installed and ran FS 7.4.1 using a windows subsystem for Linux (WSL). It seems to work and I have created a bunch of subfolders (but no BEM folder) from each subject’s T1 scan. I was also able to visualize the .mgz file using orthoview(). Due to system restrictions I had to set the subject folder in the windows file system instead of the WSL.
  2. I used the installer to install MNE python. I tried opening Spyder from the Prompt that was installed along with MNE thinking that this might be equivalent to activating the mne conda environment. But the error remains.

Would you have any insight about how I can fix this problem? Thanks!!

Yaqing

To use MNE with FreeSurfer on Windows, you need to install both in WSL. You can use the Linux installer of MNE on WSL.

I hope this helps!

Best wishes,
Richard

1 Like

Thank you for the suggestion! It took me a while to resolve graphic and other issues with WSL but now it seems to be working. For the record, I now have MNE installed both in my WSL and in my main Windows system to do the following:

  1. make the BEM folder for each subject under Linux. For some reason the coregistration GUI fails to open.
  2. once the BEM folder has been made I go back to the windows system and continue with coregistration and following steps.

Yaqing

1 Like