MATLAB & GNU Octave Module 1 – Start here: MATLAB, Octave and a working setup
Install GNU Octave 11.3 and run your first script
Install GNU Octave 11.3 on Windows, macOS or Linux, check its version and BLAS, run scripts from a terminal, read exit codes and control startup files.
What you will learn
- Install Octave 11.3 on Windows, macOS or Linux and confirm the version and BLAS in use
- Start the GUI or the command line and run a script file and a one-line command
- Use exit codes so that other programs and continuous integration can run your scripts
- Configure startup files and know when to skip them
Before you start
What you’ll learn · Before you start
What you will learn
- Install Octave 11.3 on Windows, macOS or Linux and confirm the version and BLAS in use
- Start the GUI or the command line and run a script file and a one-line command
- Use exit codes so that other programs and continuous integration can run your scripts
- Configure startup files and know when to skip them
Before you start
On this page
A script that computes the stress in a 12 mm steel rod pulled by 15 kN should print 132.6 MPa on your computer, on a classmate’s and on the server that checks your coursework. Getting there takes an Octave installation, a way to run the file, and two habits that keep runs repeatable: reading the exit status, and knowing which startup files ran first.
Below you install Octave 11.3 on Windows, macOS or Linux, check its version and maths library, run a script from a terminal as a file and as a line of code, use exit codes so that other programs can tell whether a script worked, and see how one line in a startup file changes what a script prints.
Windows uses an installer, macOS and Linux a package manager
Find the row for your system. Each route installs Octave 11.3.0, the version every example of this track ran on, or a distribution’s own build, which may be older:
| Your system | How to install Octave |
|---|---|
| Windows, 64-bit | octave-11.3.0-w64-installer.exe from octave.org/download, about 540 MB |
| macOS | Homebrew: brew install octave; MacPorts: sudo port install octave |
| Debian or Ubuntu | sudo apt install octave |
| Fedora | sudo dnf install octave |
| Any Linux | Flathub: flatpak install flathub org.octave.Octave |
Windows. Run the installer and follow its steps; it creates the shortcuts that start Octave. The download page
also offers the same build as a .zip or .7z archive, and builds marked w64-64, which pay off only for arrays of
more than about two billion elements on computers with more than about 32 GB of memory. 32-bit Windows is no longer
supported.
macOS. octave.org offers no Mac installer of its own: the Octave wiki recommends package manager: A program that installs, updates and removes software with one command from a repository of packages, such as Homebrew on macOS or apt on Ubuntu., Homebrew first, after the command-line developer tools (sudo xcode-select --install). Homebrew’s
octave formula depends on about thirty other formulae, among them OpenBLAS, Qt and FFTW, which bring their own, so
allow time and disk space for the first install.
Linux. Each distribution packages Octave itself, so its version can trail the latest release; when it is too
old, the wiki points to Flathub, whose org.octave.Octave is version 11.3.0, or to the container images of
gnuoctave/octave.
One Octave for the whole course
Install the same version as everyone you share code with, and check it as the next section shows. Two installations of different major versions can disagree on what a script accepts, as the news for each version lists.
Checkpoint
Check: Installing Octave on your system
Check the version and the maths library before anything else
A script that prints what it is running on answers the first questions anyone asks about a problem. This one runs in
Octave only, because MATLAB has neither OCTAVE_VERSION nor compare_versions, and it asks version('-blas') and
version('-lapack') for the maths libraries:
% check_setup.m: which Octave is this, and which maths libraries does it use?
% Octave-only: MATLAB has no OCTAVE_VERSION or compare_versions.
fprintf('Octave version: %s\n', OCTAVE_VERSION);
fprintf('BLAS: %s\n', version('-blas'));
fprintf('LAPACK: %s\n', version('-lapack'));
if compare_versions(OCTAVE_VERSION, '11.1.0', '>=')
disp('Ready: this is Octave 11.1.0 or later.');
else
disp('Too old: install Octave 11 for this track.');
exit(2);
end from session import run, version
version()
run("""
octave-cli check_setup.m
""") Output
# GNU Octave (aarch64-apple-darwin25.4.0) version 11.3.0 $ octave-cli check_setup.m Octave version: 11.3.0 BLAS: OpenBLAS (config: OpenBLAS 0.3.34 DYNAMIC_ARCH NO_AFFINITY USE_OPENMP vortexm4 MAX_THREADS=56) LAPACK: Linear Algebra PACKage Version 3.12.0 Ready: this is Octave 11.1.0 or later.
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 check_setup_session.py
The version line says 11.3.0, and compare_versions confirmed that it is at least 11.1.0. The
BLAS: Basic Linear Algebra Subprograms: the library of vector and matrix routines that Octave calls for matrix operations. version('-blas') names the one in use, such as OpenBLAS. line names the library that does the heavy work of matrix operations, OpenBLAS 0.3.34
here, and the LAPACK line the library for solving systems and other linear algebra built on it. On another computer
these lines may name other libraries or versions: they are what to compare first when the same script runs faster or
prints slightly different last digits elsewhere.
If the version were older than 11.1.0, the script would print a message and end with exit(2), a status that a
program running the check can read, as the section on exit codes shows.
Checking the version by its first character
v = OCTAVE_VERSION; v(1) >= '9' compares single characters, so it fails as soon as a version has two digits:
'1' is less than '9' although 11 is more than 9, and Octave 11.3 answers 0. compare_versions(OCTAVE_VERSION, '11.1.0', '>=') compares the numbers part by part.
Checkpoint
Check: Checking the version and BLAS
Three ways to run a script, and what Octave does with each
Octave is one program with two front doors. octave --gui opens its graphical window, with an editor and a command
window, and the Windows installer adds shortcuts for the window and for the command line. In a terminal, octave
starts the command line unless you add --gui, and octave-cli is the same interpreter without the graphical window,
made for terminals and scripts. Follow one run of the rod script through Octave:
The drawing shows the whole scene; its 7 steps are listed below.
Every step as text
- The command. You type octave-cli first_script.m in a terminal and press Enter. The file name tells Octave what to run.
- Octave starts. Octave loads its interpreter and sets up its search path. This takes a moment every time, so related calculations belong in one script, not one start each.
- Startup files. Before your script, Octave runs its startup files, if they exist, the installation's site files first, then ~/.octaverc in your home folder, then .octaverc in the current folder.
- Your script. The script runs from its first line to its last. first_script.m prints the cross-section, 113.1 mm^2, and the stress, 132.6 MPa.
- Status 0. No line raised an error, so Octave exits with status 0, which tells the terminal, or a program that started Octave, that the run succeeded.
- After an error. Had a line raised an error, Octave would have printed it and exited with status 1. A script can also end Octave on purpose with exit(n), and the status is then n.
- No startup files. With --no-init-all, Octave skips every startup file and goes straight from starting to your script, so the run depends on no one's settings.
Here is the script. It converts millimetres to metres, computes the area of the cross-section and divides the force by it:
% first_script.m: the axial stress in a steel rod pulled by a force.
force_kN = 15; % pulling force, kilonewtons
diameter_mm = 12; % rod diameter, millimetres
area_m2 = pi * (diameter_mm / 1000)^2 / 4;
stress_MPa = force_kN * 1e3 / area_m2 / 1e6;
fprintf('Cross-section: %.1f mm^2\n', area_m2 * 1e6);
fprintf('Axial stress: %.1f MPa\n', stress_MPa); The same script, run from a terminal as a file and as code, and a one-line calculation:
from session import run, version
version()
run("""
octave-cli first_script.m
octave-cli --eval "first_script"
octave-cli -e "disp(pi * 0.012^2 / 4)"
""") Output
# GNU Octave (aarch64-apple-darwin25.4.0) version 11.3.0 $ octave-cli first_script.m Cross-section: 113.1 mm^2 Axial stress: 132.6 MPa $ octave-cli --eval "first_script" Cross-section: 113.1 mm^2 Axial stress: 132.6 MPa $ octave-cli -e "disp(pi * 0.012^2 / 4)" 1.1310e-04
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 first_script_session.py
octave-cli first_script.mruns the file and exits when it ends.octave-cli --eval "first_script"evaluates a line of code and exits. A script’s name, without.m, runs the script of that name in the current folder.octave-cli -e "…"is the short form of--eval, new in Octave 10, here with a one-line calculation of the area: 1.1310e-04 square metres.
Both runs of the script printed 132.6 MPa, and the one-line area is the script’s 113.1 mm^2, written in square
metres. octave --no-gui first_script.m runs the same file through the octave program, without its window.
Options new in Octave 10
Octave 10 added -e, and the --no-init-all and --init-trace options that the section on startup files uses. On
an older Octave, such as a distribution’s own package, write --eval for -e, and -f, also spelled --norc, for
--no-init-all.
Checkpoint
Check: Running a script from a terminal
Exit codes tell other programs whether a script worked
A person reads the output; a program reads the exit status: The number a program hands back when it ends: 0 for success, any other number for a failure. Shells and automated jobs read it to decide what to do next., the number every program
returns when it ends: 0 for success, anything else for failure. rod_check.m stops with an error when the stress is
above a limit, 250 MPa in this example:
% rod_check.m: stops with an error when the stress in the rod is too high.
% Set force_kN before it runs: octave-cli --eval "force_kN = 15; rod_check"
diameter_mm = 12;
limit_MPa = 250; % the allowed stress in this example
stress_MPa = force_kN * 1e3 / (pi * (diameter_mm / 1000)^2 / 4) / 1e6;
fprintf('Force %g kN: stress %.1f MPa\n', force_kN, stress_MPa);
if stress_MPa > limit_MPa
error('Stress %.1f MPa is above the %d MPa limit', stress_MPa, limit_MPa);
end
disp('Within the limit'); % ask_rods.m: asks a question, which an automated run cannot answer.
n = input('How many rods? ');
fprintf('Checking %d rods\n', n); from session import run, version
version()
run("""
octave-cli --eval "force_kN = 15; rod_check"
!octave-cli --eval "force_kN = 40; rod_check"
octave-cli --eval "force_kN = 40; rod_check" || echo "rod_check failed: stopping here"
!octave-cli --eval "exit(3)"
!octave-cli ask_rods.m
""") Output
# GNU Octave (aarch64-apple-darwin25.4.0) version 11.3.0
$ octave-cli --eval "force_kN = 15; rod_check"
Force 15 kN: stress 132.6 MPa
Within the limit
$ octave-cli --eval "force_kN = 40; rod_check"
Force 40 kN: stress 353.7 MPa
error: Stress 353.7 MPa is above the 250 MPa limit
error: called from
rod_check at line 8 column 3
[exit status 1]
$ octave-cli --eval "force_kN = 40; rod_check" || echo "rod_check failed: stopping here"
Force 40 kN: stress 353.7 MPa
error: Stress 353.7 MPa is above the 250 MPa limit
error: called from
rod_check at line 8 column 3
rod_check failed: stopping here
$ octave-cli --eval "exit(3)"
[exit status 3]
$ octave-cli ask_rods.m
How many rods? error: input: reading user-input failed!
error: called from
ask_rods at line 2 column 1
[exit status 1]
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 exit_codes_session.py
At 15 kN the check passed and the status was 0, so the session shows none. At 40 kN the stress, 353.7 MPa, broke the
limit: error printed the message and where it happened, and Octave exited with status 1. The third command is how
other programs use that: in a shell, || runs what follows only when the command before it fails. exit(3) ends
Octave with the status 3, which lets a script report different kinds of failure.
The last run shows a trap. input waits for someone to type an answer, and when no one can, as in an automated run
with its input closed, Octave stops with reading user-input failed! and status 1. That is the right outcome: a
script for continuous integration: Running a project's checks automatically on every change, on a service or server, so that a check ending with a non-zero exit status stops the change. should never ask questions, and a closed
input, such as < /dev/null after the command in a macOS or Linux shell, makes such a script fail at once instead of
waiting.
MATLAB works the same way when it is started with -batch: MathWorks documents that it exits with status 0 when the
code runs and with a non-zero status when it fails. In PowerShell the status of the last program is in
$LASTEXITCODE, and in a macOS or Linux shell in $?.
Key idea
An uncaught error makes octave-cli exit with status 1 and exit(n) with status n, so a script signals failure
without anyone reading its output.
Checkpoint
Check: Exit codes
Startup files run before your script, so checks skip them
The flow above showed Octave running startup files before your script. A startup file: A file of commands that Octave runs every time it starts, before anything else, such as ~/.octaverc or a .octaverc in the current folder. MATLAB's is startup.m.
is a script Octave runs every time it starts, and a single line in one changes what every script prints. This
session writes format long into a .octaverc in the current folder and runs the same two-line script three times:
% show_area.m: the cross-section of a 12 mm rod, in square metres.
area_m2 = pi * 0.012^2 / 4 from session import run, version
version()
run("""
printf 'format long\\n' > .octaverc
octave-cli show_area.m
octave-cli --no-init-all show_area.m
octave-cli --init-trace show_area.m
""") Output
# GNU Octave (aarch64-apple-darwin25.4.0) version 11.3.0 $ printf 'format long\n' > .octaverc $ octave-cli show_area.m area_m2 = 1.130973355292325e-04 $ octave-cli --no-init-all show_area.m area_m2 = 1.1310e-04 $ octave-cli --init-trace show_area.m executing commands from /opt/homebrew/Cellar/octave/11.3.0_1/share/octave/site/m/startup/octaverc ... done. executing commands from /opt/homebrew/Cellar/octave/11.3.0_1/share/octave/11.3.0/m/startup/octaverc ... done. executing commands from .octaverc ... done. area_m2 = 1.130973355292325e-04
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 startup_session.py
With the startup file, the area showed 16 significant digits, 1.130973355292325e-04. With --no-init-all, which
skips every startup file, it showed the default five, 1.1310e-04. The value is the same; only its display changed,
yet a classmate without your .octaverc sees other output, and a check that compares printed text fails.
--init-trace, new in Octave 10, lists the files that ran: the two site files of the installation, then .octaverc.
Octave reads, in order: the site files of the installation, a per-user file in your configuration folder,
~/.octaverc in your home folder, .octaverc in the current folder, and a startup.m for MATLAB-style setups,
though its manual prefers ~/.octaverc. MATLAB runs a startup.m that it finds on its search path, and MathWorks
advises against putting cd commands in it. Three options control the Octave files: --no-init-all skips them all,
--no-init-user only yours, --no-init-site only the installation’s.
Putting a script's settings in your startup file
format long, addpath or cd in ~/.octaverc make your scripts work on your computer only. Put what a script
needs into the script, keep startup files for personal comfort, and run every check with --no-init-all.
Checkpoint
Check: Startup files
Practise: set up Octave and check it
Five steps on your own computer turn this lesson into a working setup; take them in this order:
- Install Octave for your system as the first section shows, and start
octave-clionce to see it work. - Save
check_setup.mand runoctave-cli check_setup.m. It should print your version and end with the Ready line. - Save
first_script.mand run it as a file, with--evaland, if you have Octave 10 or later, with-e. Each way should print 132.6 MPa. - Save
rod_check.m, runoctave-cli --eval "force_kN = 40; rod_check", thenecho $?in macOS or Linux, orecho $LASTEXITCODEin PowerShell, and confirm the status is 1. - Put
format longinto a.octavercin a test folder, runshow_area.mwith and without--no-init-allthere, and delete the file afterwards.
Then test the five ideas of the page, from installing to startup files. The session picks your weakest idea first, shows one question at a time and tells you straight away why an answer is right or what it missed:
Practice that adapts to you
Practise this lesson
15 questions about 5 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.
Interview questions
Warm-up: how do you find out which Octave version and which BLAS a computer uses?
Run OCTAVE_VERSION for the version, or octave-cli --version in a terminal, and version('-blas') and
version('-lapack') for the maths libraries: on the computer that ran this track’s examples they named Octave 11.3.0,
OpenBLAS 0.3.34 and LAPACK 3.12.0. To check a minimum version in a script, use compare_versions(OCTAVE_VERSION, '11.1.0', '>='), which compares the numbers part by part, never the text. Put these lines in a small script such as
check_setup.m and run it first on any new computer: when a result differs between two machines, different versions
or libraries are the first thing to rule out. OCTAVE_VERSION and compare_versions are Octave-only; in MATLAB,
version returns its own version text, such as 26.2.0.3341813 (R2026b).
Model answers open with any pass: ₹79 for 1 week ($2.99), one payment that never renews.
-
Mid to senior
A continuous-integration job runs your checks with octave-cli --eval "run_checks" | tee log.txt, and it passed although one check printed an error. What went wrong, and how do you make a failing check fail the job?
-
Fresher to mid
The same script prints 1.130973355292325e-04 on your laptop and 1.1310e-04 on a classmate's, and a third laptop differs in the last digit of a long calculation. Explain each difference and how you make the outputs comparable.
Key takeaways
- Windows users install Octave 11.3 with the installer from octave.org, macOS users with Homebrew or MacPorts, and
Linux users with their distribution’s package or Flathub’s
org.octave.Octavewhen that is too old. OCTAVE_VERSION,version('-blas')andcompare_versionstell you which Octave and which maths libraries a computer uses, and they are the first things to compare when two computers disagree.octave-cli file.mruns a script file,octave-cli --eval "code"or, from Octave 10,-eruns a line of code, andoctave --guiopens the graphical window.- An uncaught error makes Octave exit with status 1 and
exit(n)with status n, so shells and continuous integration can tell success from failure; a script that callsinputfails at once when its input is closed. - Startup files such as
.octavercrun before every script and can change what it prints, so checks run with--no-init-alland scripts carry their own settings.
Terms in this lesson
- BLAS
- Basic Linear Algebra Subprograms: the library of vector and matrix routines that Octave calls for matrix operations. version('-blas') names the one in use, such as OpenBLAS.
- continuous integration
- Running a project's checks automatically on every change, on a service or server, so that a check ending with a non-zero exit status stops the change.
- exit status
- The number a program hands back when it ends: 0 for success, any other number for a failure. Shells and automated jobs read it to decide what to do next.
- package manager
- A program that installs, updates and removes software with one command from a repository of packages, such as Homebrew on macOS or apt on Ubuntu.
- startup file
- A file of commands that Octave runs every time it starts, before anything else, such as ~/.octaverc or a .octaverc in the current folder. MATLAB's is startup.m.
References
- Download GNU Octave (The Octave Project Developers)
- Octave for Microsoft Windows (Octave wiki) (The Octave Project Developers)
- Octave for macOS (Octave wiki) (The Octave Project Developers)
- Octave for GNU/Linux (Octave wiki) (The Octave Project Developers)
- octave: Homebrew formula and its dependencies (Homebrew)
- GNU Octave on Flathub (org.octave.Octave) (Flathub)
- Docker images of GNU Octave (gnuoctave/octave) (The Octave Project Developers)
- GNU Octave manual: Running Octave (The Octave Project Developers)
- GNU Octave manual: Command Line Options (The Octave Project Developers)
- GNU Octave manual: Startup Files (The Octave Project Developers)
- News for Octave 10 (new command-line options) (The Octave Project Developers)
- matlab (Linux): start MATLAB program from Linux system prompt (MathWorks)
- startup: user-defined startup script for MATLAB (MathWorks)
- Shell Command Language: special parameter ? (POSIX.1-2024) (The Open Group and IEEE)
- about_Automatic_Variables ($LASTEXITCODE) (Microsoft)
Related tools
Report a problem with this lesson
Kept only in this browser. Your Learn progress