Tutorial: Installing NEURON 7.6 simulator with Python and MPI on Ubuntu 18.04 LTS

Tested working as of: 6/30/2019

NEURON simulator is a popular computational neuroscience tool for creating biophysically realistic models of neurons and neural networks. Many model building tasks can be accomplished using Python. However, the default NEURON installation will not allow scripting NEURON from an external Python file. The goal of this tutorial is to show the steps necessary to enable the use of NEURON simulator from an external Python script. When it works, it should look like the image below:


Using the standard installation, attempting to import the ‘neuron’ module will result in the following error:

> from neuron import h, gui
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named neuron

Below are the steps to get the above to work. Most of this is similar to the steps to compile the standard NEURON distribution, with many crucial extra pieces that were scattered around the NEURON site (without those pieces, you’ll encounter several errors). This tutorial puts all the small pieces together in one place. I’ve tested the steps below on several virgin Ubuntu machines.

Update: A pure script version of the steps below is described in the following followup tutorial: Using NEURON simulator with Python and Travis CI

Major Installation Steps

  1. Install required Ubuntu packages
  2. Install python (full or mini Anaconda)
  3. Compile NEURON’s IV and NEURON
  4. Setup environmental variables
  5. Install NEURON python package


  • Ubuntu Linux (16.04+): If you plan on running your models on clusters or other HPC systems, *nix will be unavoidable. Also, many comp-neuro packages and software have a tendency of being most heavily tested on *nix systems. Thus, in the long run using Linux results in fewer headaches for the users. I’m using a fresh Ubuntu 16.04 installation to test the steps below.
  • Note on using VM software: At least on VMware Workstation, there are issues if you install NEURON into a non-Linux drive location or into a mapped network drive type of location. Make sure you pick a location that is on the Linux drive.

Important NOTES

  • Make sure to use the folder structure described below. Many of the scripts and command lines assume this structure.
  • Use Anaconda to make your life easier, or have an existing Python 2.7+ installation that has working versions of these packages: scipy numpy matplotlib cython
  • Make note when ‘sudo’ is and is not used. Some of the commands below require admin/sudo permissions, while others do not. The commands below are written to be used as-is. A reader reported that under-using or over-using sudo resulted in errors/issues for them.

Install missing libraries

Before you can compile NEURON source files, you will need to install several missing packages:

sudo apt-get update

# Needed for x11
sudo apt-get install libx11-dev libxt-dev gnome-devel

# Needed for MPI support
sudo apt-get install mpich

# Needed by NEURON
sudo apt-get install libncurses-dev xfonts-100dpi

NOTE: Many NEURON examples that use MPI assume you are using MPICH. Make sure you uninstall any other MPI packages you have installed before installing MPICH. Otherwise, you may the have the “all processes have rank 0” problem.

IMPORTANT: after these packages are installed, make sure to logout and log back in. Without re-login you’ll get the font error below. If you run into issues running the above lines, try installing the packages one at a time (or see this SO answer).

Without these packages I got the following errors:

Install Anaconda Python

Anaconda-based installation has been the path with the fewest problems later on, especially if you’ll be using the popular packages like numpy or matplotlib.

Follow the steps to install Anaconda on Linux or steps to install miniconda. I used the full Anaconda installation (2 GB+), but miniconda (<100MB) works too. Once installed, you should be able to run the following in terminal and see a list of packages:

conda list

Now install Python 2.7. I’ve had a very difficult time trying to install NEURON with Python 3+ and eventually moved on to other endeavors. (I have heard that NEURON versions after ~7.5 work with python 3.5+)

conda create -n p27 python=2.7

Once installed run the following to activate the new environment:

source activate p27

I use this environment most of the time for model development, so I also put the activation in my .bashrc file, so the environment loads each time I open a new terminal:

gedit ~/.bashrc

Then paste “source activate p27” at the end of the file and close. Restart your terminal window or run ‘source ~/.bashrc’ and you should see the new environment active (e.g.):

(p27) justas@ubuntu:~$

Now update pip and install packages you’ll likely need in the future:

pip install --upgrade pip
pip install scipy numpy matplotlib cython mpi4py neuronpy

Now we’re ready to start installing NEURON.

Download NEURON source files

Go to NEURON site and get the nrn- and iv- tarballs of the standard distribution:

Here I’ll be installing NEURON 7.6:

Save these to the Downloads folder, use the Ubuntu file manager to extract them (right-click > “Extract here”), rename the extracted folders to “nrn” and “iv“. The iv folder should have the src folder and the nrn folder should have bin and src folders, among other files.


Now move these folders into a home folder called “neuron”. Select both iv and nrn folders, right-click > “Move To…”, click “Home”, then click the upper-right create folder icon, name it “neuron, and select. Your folder structure should look like this:


Compile IV and NEURON

Compile IV

We’ll compile the IV part first, then NRN afterwards. Open terminal, go to the iv folder:

cd ~/neuron/iv

Then type in these commands, one by one, looking for any errors (warnings usually OK to ignore), to configure and install IV:

./configure --prefix=`pwd`
make install

If you don’t see any errors, proceed to the next step. If you get errors make sure you’ve installed the above libraries. In general, search the NEURON forum for any error messages you encounter.

Compile NEURON

Now go to the ~/neuron/nrn folder:

cd ../nrn

Find out the location of your python binary with this command (make sure you have the Anaconda Python 2.7+ environment active “source activate p27” first):

which python

You should see something like: /home/justas/anaconda2/envs/p27/bin/python. Copy this string and use it in the next command to configure NEURON source make files:

./configure --prefix=`pwd` --with-iv=$HOME/neuron/iv --with-paranrn --with-nrnpython=/home/justas/anaconda2/envs/p27/bin/python

Then run these lines, one at a time (these take a few minutes to run):

make install

If the above succeed without errors (warnings are again ok), test if NEURON was installed by running the demo program. First, find the program:

find . -name neurondemo

You should see something like this:


Copy the line that has your CPU architecture and run it, e.g:


Pick one of the demos. My favorite is “Stochastic Single Channels: HH” , with say 10 channels option selected. It demonstrates how unreliable individual channels are at generating action potentials — it is only when there are >1000 channels that the AP onsets become relatively reliable. Which means cells must be large enough to be able to accommodate that many channels, which restricts which organisms can have action potential generating nervous systems (see this paper for more)

If you get the demo to work, you should see something like this:


Set up environment variables

To make sure all of this still works after you restart your computer, set the appropriate environment variables. Go to ~/neuron folder:

cd ~/neuron

Create a new file “nrnenv” there:

gedit nrnenv

And paste the following contents. NOTE: Replace “x86_64” with your CPU architecture (check the folder of neurondemo above)

export IV=$HOME/neuron/ivexport N=$HOME/neuron/nrn

# for this concrete example, we assume hostcpu is x86_64
export CPU=x86_64
export PATH="$IV/$CPU/bin:$N/$CPU/bin:$PATH"

Save the file, and then open the ~/.bashrc file:

gedit ~/.bashrc

Scroll to the bottom, add these lines to the end, and save:

# Set up NEURON variables
source $HOME/neuron/nrnenv

Restart the terminal and test that the environment variables have been set up:

echo $CPU

You should see your CPU architecture displayed.

Now test if NEURON can be started with the Python interpreter:

nrngui -python

You should see something like this:


This means that NEURON can be started with Python as its interpreter. However, we still want to be able to start NEURON from an external Python script. To do that, a Python package “neuron” has to be installed to the Python environment’s site-packages folder.

Install NEURON python package

In terminal go to the following location:

cd ~/neuron/nrn/src/nrnpython/

Then, install the NEURON Python package with:

python setup.py install

If you don’t see any errors, test that the package was installed correctly with:


And once Python loads, type in:

from neuron import h, gui

If everything worked, you should see something like:


At this point you can use neuron in your external Python scripts by using the “from neuron import h, gui” line.

From here on, I would recommend to look over the following:

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s