How to simulate circuits with Ngspice on Ubuntu – Guide for Beginners

By | November 19, 2022


Ngspice is the most popular free circuit simulator. Its available for most platforms including Linux. Whether you are an engineer or just want to play with electronics as a hobby, Ngspice can be a useful tool to simulate circuits and analyse their behavior before actually building them in the lab.

This quick tutorial shows you how to setup and use Ngspice on Ubuntu. The ngspice packages are available in the Ubuntu repository so you need to do any compilation.

Just install using the apt-get command and you are ready to go.

Install Ngspice

Install ngspice using the apt-get command.

$ sudo apt-get install ngspice

Its should install in a few minutes.
Next check the details of the installed version:

$ ngspice -v
ngspice compiled from ngspice revision 26
Written originally by Berkeley University
Currently maintained by the NGSpice Project

Copyright (C) 1985-1996,  The Regents of the University of California
Copyright (C) 1999-2011,  The NGSpice Project

The above is how it showed up on my system. Yours might be a little different.

To learn more about the latest ngspice version available, check out their official site at:

Start interactive terminal

Ngspice can be used as a stand alone command on the terminal or as an interactive shell. We shall be using the interactive shell. Inside the interactive shell we can do things like loading circuits, simulating them, and seeing the output in plots.

Running the ngspice command launches the shell with a welcome message.

$ ngspice
** ngspice-26 : Circuit level simulation program
** The U. C. Berkeley CAD Group
** Copyright 1985-1994, Regents of the University of California.
** Please get your ngspice manual from
** Please file your bug-reports at
** Creation Date: Tue Oct 28 15:16:02 UTC 2014
ngspice 2 ->

Now that we have started the shell, its time to create and test our first circuit. Keep reading!

1. Create a circuit file

Lets take this simple circuit for example. It has a dc voltage source ( a simple battery ) and a resistor connected with it. It can't get any simpler.

The numbers 0 and 1 represent the node numbers. A node is a point which connects 2 or more components together. Be careful with node numbers. No 2 nodes can have the same number. Doing so would mean they are the same nodes.

     |                  |
     |                  |
     +                  |
Vin = 10V DC            R = 5 ohm
     +                  |
     |                  |
     |                  |
     0 (Gnd)

To represent the above circuit in spice, we write the following piece of code. Type it out in a simple text file and save as ""

* A simple circuit with a battery and resistance
Vin 1 0 dc 10
R1 1 0 5
.tran .05 1 uic

You may ask, why the filename extension ".net" ?
Because the circuit code is called a "net list" in spice terminology. You could use any extension for the file, but most popular extensions are .net and .cir

It doesn't matter!

:: First line is a comment - Its required !

The first line is a comment. Lines starting with an asterisk mark are treated as comments by spice.
It is necessary to have a comment line at the starting of the file. Spice needs that! It is treated as the title of the circuit.

!! If you do not add a comment line, then the first circuit line shall be treated as the title and you shall get funky outputs.

:: The circuit

The Vin line specifies the voltage.

Note - When specifying the voltage nodes, the positive node must be specified first. If you specify the voltage as "Vin 0 1 dc 10", then spice will treat it as a negative voltage source. 0 shall be ground and 1 shall be 10volts below it.

However the node order is insignificant for passive components like resistors and capacitors. The resistor can be specified as "R1 1 0 5" or "R1 0 1 5". Its the same, as long as the nodes are correct.

:: Node 0 or GND must be there in the circuit

You must have a node named 0. The 0 node is absolutely necessary to be present in the circuit and it shall be treated as "Ground" by Spice. So make sure to have a node named 0 at the ground point.

If you do not have a 0 node in your circuit specification, you would see errors like these when trying to run the circuit program

Warning: singular matrix:  check nodes 1 and 1

You can also use gnd or GND in place of 0.
The following syntax will also work

Vin 1 GND DC 10

Spice will automatically replace gnd or GND with 0.

:: We are doing a TRANSIENT analysis in this circuit

Transient analysis is the the behaviour of the circuit over a period of time. This is what we would mostly do.

The .tran line indicates that we want to do a transient (time based) analysis of the circuit upto the time point of 1 second in increments of .05 seconds.

When doing a very basic analysis of the circuit to find out what would the current and voltage be at various places in the circuit at any time, we do a "transient" analysis inside of spice.

We tell spice to find the circuit behavior between 2 time points and spice calculates the data which can be used to draw a plot.

In the above code, we are analysing the circuit behavior between 0s and 1s and the calculation shall be done in increments of .05 seconds.

2. Load the circuit file in spice

Now that we have written our first circuit program, its time to run it inside ngspice. Load the circuit file using the source command like this:

ngspice 2 -> source

Circuit: * a simple r circuit

ngspice 2 ->

3. Run the circuit file - Simulation

Now we shall simulate the circuit program written above. The ngspice interactive shell has a "run" command to make spice do the simulation and calculate values at different points in time.

ngspice 2 -> run
Doing analysis at TEMP = 27.000000 and TNOM = 27.000000

Initial Transient Solution

Node                                   Voltage
----                                   -------
1                                            0
vin#branch                                   0

No. of Data Rows : 62
ngspice 2 ->

Now the simulation is complete and all the date is ready for analysis.

4. Check the Plot-able variables

To plot the voltage use the plot interactive command. First use the "display" command to see what all can be plotted.

ngspice 2 -> display
Here are the vectors currently active:

Title: * a simple r circuit
Name: tran3 (Transient Analysis)
Date: Tue Jan 24 15:51:46  2017

    V(1)                : voltage, real, 62 long
    time                : time, real, 62 long [default scale]
    vin#branch          : current, real, 62 long
ngspice 2 ->

Ngspice tells us that we can plot the voltage V(1) which is the voltage at Node 1, V(2) and the current through vin branch.

5. Plotting current (I) with ngspice

In the above example if you try to plot the current by plotting vin#branch, you will see that the current is being shown is negative. This is confusing. However that is how spice works.

Note: Plotting the current across a component in ngspice is a bit tricky.

The voltage is measured at a certain node. However currents cannot be measured at a node (since current entering any point it equal to the current leaving that point).

However current is measured through components.
Spice by default measures current across voltage sources and certain components like inductor

:: Fake voltage source technique

To measure the current across a resistor add a fake 0 volt dc source in series and then spice shall calculate the current flowing through this voltage source which shall be equal to the current through the resistor itself!

Then spice will allow to plot the current across the voltage source on graph.

Plotting a GUI graph


1. View the current circuit net using the listing command. The "listing" command shall display the circuit code as it is in the loaded file. There shall be however few extra lines.

ngspice 2 -> listing
        * first circuit with a resistor

     2 : .global gnd
     3 : r1 1 0 5
     4 : vdc 1 0 10
     5 : .tran .05 1 uic
     7 : .end
ngspice 2 ->

1. You can skip the ".tran" statement in the circuit code and use the tran command from interactive terminal.

* Simple circuit
R1 1 0 5
Vdc 1 0 10

Run the transient analysis with the tran interactive command -

ngspice 2 -> tran .05 1 uic
Doing analysis at TEMP = 27.000000 and TNOM = 27.000000

Initial Transient Solution

Node                                   Voltage
----                                   -------
1                                            0
vdc#branch                                   0

No. of Data Rows : 62
ngspice 2 ->

This way you can make the circuit file look neat and clean.

2. Use the .control statement in the code to run interactive commands inside the circuit code.

* Spice netlister for gnetlist
R1 1 0 5
Vdc 1 0 10
.tran .05 1 uic
plot v(1)

The above circuit program will run and plot immediately as you load it using the source command

ngspice 2 -> source

The .endc marks the end of the control block. Instead of the run command inside the control block, you could instead write the tran command inside the control block which would also cause the simulation to run.

tran .05 1 uic
plot v(1)

3. Run the interactive "help" command to launch the help window.

ngspice 2 -> help

RC circuit with DC source

The previous circuit was too simple with a flat voltage and current plot. Now lets try plotting a different circuit that has a capacitor in it.

Here is what the graph would look like -

RC circuit with AC source - See the Phase shift!!

Now that we have done 2 circuits with a dc voltage source, lets try a different one with an ac voltage source. AC source is a varying voltage with changing amplitude and direction.

Here is the circuit code

* Simple RC circuit with AC source

* since voltage source with peak voltage 1V and frequency 200 kHz
V1 n1 0 sin(0 1 200kHz)

R1 n1 n2 10k
C1 n2 0 1nF


Save the file as
Note that there is no .tran statement. We shall use the tran command instead.

Run it and plot the voltage and current

ngspice 1 -> run
Doing analysis at TEMP = 27.000000 and TNOM = 27.000000

Warning: v1: no DC value, transient time 0 value used

Now issue the tran command to calculate the time based behavior.

tran 0.05us 100us

We want to plot the output of the circuit from 0s to 100us to be calculated in increments of .05 us.

ngspice 1 -> tran 0.05us 100us
Doing analysis at TEMP = 27.000000 and TNOM = 27.000000

Warning: v1: no DC value, transient time 0 value used

Initial Transient Solution

Node                                   Voltage
----                                   -------
n1                                           0
n2                                           0
v1#branch                                    0

No. of Data Rows : 2008
ngspice 1 ->

To simulate a low pass RC filter with the above circuit, check out this tutorial -

Have a look at the following links for more examples:

Drawing circuits with Geda

Geda is a powerful circuit design tool for Linux. It can be used to design circuits and then you can create netlist files from these geda circuits.

$ sudo apt-get install geda

Run from the applications menu on your desktop, or from the terminal like this -

$ gschem

Geda saves the circuits as sch files that can be converted to ngspice net files using the gnetlist command like this

$ gnetlist -g spice -o first.sch

Next you can use the .net file to simulate with ngspice or other tools like gspiceui

Simulating with Gspiceui

Gspiceui is a gui tool that can be used to simulate .net files and view the output or wave form. Once you have created your circuit ".net" file you can open it inside Gspiceui and run the simulation with various settings. Gspiceui can also directly import Geda .sch files and convert.

It has a menu option to reload the file quickly, if you are editing it in a text editor side by side. Just save the circuit file in the text editor and click Reload inside of gSpiceUI. And click Simulate and run again.

Other tools

Besides geda and gspiceui, there are other free tools for linux like Kicad that can be used to draw and simulate circuits. We shall be talking about those in upcoming articles.

To try out ngspice programs inside your web browser, check out this site -

You can type in your netlist and view the plots in the browser, without having to install ngspice. It does not however save your netlist.

For a more powerful web based ngspice emulator check out partsim. It allows you to draw circuit visually and run them, and also save them and share them online.

References - The official Ngspice manual and guide you need to read up before using ngspice for the first time. However that's not compulsary! - Explanation of the different kinds of analysis spice can perform on a ciruit. - A practical guide explaining things that you need to careful about when simulating with spice. - Examples of circuit netlists - Ngspice tutorial - SPICE - A Brief Tutorial

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

Leave a Reply

Your email address will not be published. Required fields are marked *