Skip to main content

Hello World (first programs) · Beginner · 16 min

Hello World in Ada: GNAT, gnatmake and range types

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · GNAT 14.3 (Ubuntu gnat package) · Alire 2.1.1

You will install the GNAT Ada compiler on Ubuntu 26.04, build and run Hello World with gnatmake, and declare a day-of-the-month range type that stops a bad number with Constraint_Error.

On this page

Ada is a compiled language designed for programs that must not fail quietly. It was created for the US Department of Defense in the late 1970s, named after Ada Lovelace, and first standardised in 1983; the current standard is Ada 2022. Its keywords read like English (begin, end, is, loop), and its compiler refuses to mix types that C or Python would quietly convert, such as a plain whole number and a day of the month.

That strictness is why Ada runs flight control, rail signalling, air traffic management, defence and satellite software, where a wrong number can cost lives or a mission. SPARK, a subset of Ada, goes further and lets tools prove mathematically that some errors can never happen. You will not need SPARK here, but it is the reason many safety teams pick Ada.

The free Ada compiler is GNAT, part of GCC. You install it from Ubuntu's own packages, build Hello World with gnatmake, see Ada's rule that a file is named after the unit inside it, and then write a type that only accepts the numbers 1 to 31. You work in a throwaway folder, ~/hello-ada-demo, and delete it at the end.

Step 1 · Install GNAT

On Ubuntu 26.04, the gnat package installs GNAT 14 and the tools that go with it. It lives in the universe repository, which Ubuntu enables by default:

Bash
sudo apt update
sudo apt install -y gnat
gnatmake --version

On Windows and macOS, use Alire instead (see the Alire step below): it downloads a GNAT build for your system, so you do not have to find one yourself. On Windows you can also install WSL with Ubuntu and follow these commands as written.

Check it worked

The first line of the gnatmake --version output starts with GNATMAKE 14., followed by the patch number Ubuntu ships (14.3 at the time of writing). If you see gnatmake: command not found, read the last lines of the apt install output for an error and run it again.

Step 2 · Write hello.adb

Make the demo folder and create the source file. Ada source files for a program body end in .adb (Ada body):

Bash
mkdir -p ~/hello-ada-demo
cd ~/hello-ada-demo
cat > hello.adb <<'ADA'
with Ada.Text_IO;

procedure Hello is
begin
   Ada.Text_IO.Put_Line ("Hello, world!");
end Hello;
ADA

You can type the same six lines into a text editor and save them as hello.adb instead. Line by line:

  • with Ada.Text_IO; makes the standard text input and output package available to this file. Ada has no hidden imports: every package a unit uses is named in a with clause at the top.
  • procedure Hello is declares a procedure called Hello. The main program in Ada is an ordinary procedure with no parameters; there is no special name like main. Anything you declare for the procedure (variables, types) goes between is and begin. Here there is nothing.
  • begin starts the statements that run.
  • Ada.Text_IO.Put_Line ("Hello, world!"); calls Put_Line from the Ada.Text_IO package. It prints the string and then a line break. Strings use double quotes, and every statement ends with a semicolon.
  • end Hello; closes the procedure. Repeating the name is optional, but it is the usual style, and the compiler checks that it matches, which helps when a file holds long nested blocks.

Ada ignores letter case in names, so put_line and PUT_LINE mean the same thing. The usual style is Mixed_Case with underscores for names and lower case for keywords, and three spaces of indentation.

Check it worked

cat hello.adb prints the six lines, starting with with Ada.Text_IO; and ending with end Hello;.

Step 3 · Build with gnatmake and run

Build the program and run it:

Bash
cd ~/hello-ada-demo
gnatmake hello.adb
./hello
ls

gnatmake prints the three programs it runs, something like this:

Text
x86_64-linux-gnu-gcc-14 -c hello.adb
x86_64-linux-gnu-gnatbind-14 -x hello.ali
x86_64-linux-gnu-gnatlink-14 hello.ali

Each line is one stage:

  • Compile. GCC compiles hello.adb to machine code in hello.o, and writes hello.ali, an Ada Library Information file. The .ali file records which units this one depends on and the exact versions of their sources.
  • Bind. gnatbind reads the .ali files and checks that every unit in the program was compiled against the same versions of the units it uses. If you edit a package and forget to rebuild a file that uses it, the binder refuses to continue. It also works out the order in which packages must be set up before the main procedure starts.
  • Link. gnatlink joins the object files with the Ada run-time library into the program hello.

Run gnatmake hello.adb a second time. It checks the .ali file against the source, sees nothing has changed, and does no work.

Check it worked

./hello prints Hello, world!. ls shows four files: hello, hello.adb, hello.ali and hello.o. The second gnatmake run prints something like gnatmake: "hello" up to date.

Step 4 · See the file-name rule

GNAT finds units by file name. A procedure called Hello belongs in hello.adb: the unit name in lower case, with a dot in a package name becoming a hyphen (the package Budget.Reports lives in budget-reports.adb). Break the rule on purpose by saving the same code under another name:

Bash
cd ~/hello-ada-demo
cp hello.adb greeting.adb
gnatmake greeting.adb

GNAT reports file name does not match unit name, should be "hello.adb". In a single-file program you could get away with it, but once one unit withs another, GNAT looks for the other unit's file by its name, and a mismatched name means it cannot find it. Delete the copy:

Bash
cd ~/hello-ada-demo
rm -f greeting.adb greeting.ali greeting.o greeting naming.txt

Check it worked

You saw the message naming hello.adb as the file name GNAT expected, and ls shows only the four hello files again.

Step 5 · Declare a range type

Now the step that shows what makes Ada different. Write a program that asks for a day of the month, and give it a type that only allows 1 to 31:

Bash
cd ~/hello-ada-demo
cat > day.adb <<'ADA'
with Ada.Text_IO; use Ada.Text_IO;

procedure Day is
   type Day_Of_Month is range 1 .. 31;
   Today : Day_Of_Month;
begin
   Put ("Day of the month: ");
   Today := Day_Of_Month'Value (Get_Line);
   Put_Line ("You picked day" & Day_Of_Month'Image (Today));
end Day;
ADA
gnatmake day.adb
echo 18 | ./day

The new lines:

  • use Ada.Text_IO; lets you write Put_Line instead of Ada.Text_IO.Put_Line. The with makes the package available; the use lets you drop its name.
  • type Day_Of_Month is range 1 .. 31; creates a new whole-number type. It is not another name for Integer: it is a separate type whose values can only be 1 to 31.
  • Today : Day_Of_Month; declares a variable of that type. Declarations go between is and begin, and : means "is of type".
  • Put prints without a line break, so the answer appears on the same line as the question.
  • Get_Line reads one line from the keyboard and returns it as a String.
  • Day_Of_Month'Value (...) turns that text into a Day_Of_Month. The apostrophe introduces an attribute, a built-in property of a type. := is assignment.
  • Day_Of_Month'Image (Today) turns the number back into text. For a number that is not negative it adds a leading space where a minus sign would go, which is why there is no space after day in the string. & joins two strings.

Ada.Text_IO works with 8-bit characters. A name typed in Yoruba, Amharic or Arabic still prints back correctly, because the bytes pass through untouched, but a String's length counts bytes rather than letters. Ada has Wide_Wide_String and the Ada.Strings.UTF_Encoding packages for text where letters matter.

Check it worked

The last command prints Day of the month: You picked day 18. Run ./day on its own, type 7 and press Enter, and it prints You picked day 7.

Step 6 · Watch Constraint_Error stop a bad number

Feed the program a number outside the range, and then something that is not a number:

Bash
cd ~/hello-ada-demo
echo 40 | ./day || echo "exit code: $?"
echo abc | ./day || echo "exit code: $?"

Both runs stop with a message that starts raised CONSTRAINT_ERROR, followed by the reason: something like day.adb:8 range check failed for 40, and bad input for 'Value for abc. The program never stores 40 in Today. In C, the same mistake would leave a wrong value in memory and the program would carry on. Ada checks every assignment to a range type while the program runs, and GNAT keeps these checks on unless you switch them off.

The compiler catches what it can before the program runs, too. A literal value outside the range draws a warning at build time:

Bash
cd ~/hello-ada-demo
cat > static.adb <<'ADA'
procedure Static is
   type Day_Of_Month is range 1 .. 31;
   Pay_Day : Day_Of_Month := 32;
begin
   null;
end Static;
ADA
gnatmake static.adb

GNAT warns something like value not in range of type "Day_Of_Month" and Constraint_Error will be raised at run time. null; is a statement that does nothing; Ada requires at least one statement between begin and end.

Check it worked

The run with 40 shows raised CONSTRAINT_ERROR and a non-zero exit code (GNAT uses 1), and the program did not print You picked day. The static.adb build printed the Constraint_Error will be raised at run time warning.

Step 7 · Handle the error

A crash with a stack message is right for a bug, but bad typing is normal. Catch the exception and answer politely. Replace day.adb with this version:

Bash
cd ~/hello-ada-demo
cat > day.adb <<'ADA'
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Command_Line;

procedure Day is
   type Day_Of_Month is range 1 .. 31;
   Today : Day_Of_Month;
begin
   Put ("Day of the month: ");
   Today := Day_Of_Month'Value (Get_Line);
   Put_Line ("You picked day" & Day_Of_Month'Image (Today));
exception
   when Constraint_Error =>
      Put_Line ("Please type a whole number from 1 to 31.");
      Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
end Day;
ADA
gnatmake day.adb
echo 18 | ./day
echo 40 | ./day || echo "exit code: $?"
  • exception starts the handler section of the procedure. If any statement between begin and exception raises an exception, the program jumps here.
  • when Constraint_Error => handles that one exception. Others still stop the program, which is what you want for errors you did not expect.
  • Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure); sets the exit code the program returns when it ends, so a script that calls day can tell the input was rejected.

gnatmake rebuilds day because day.adb changed since the last build; it compares the source with what day.ali recorded.

Check it worked

echo 18 | ./day prints Day of the month: You picked day 18. echo 40 | ./day prints Day of the month: Please type a whole number from 1 to 31. and then exit code: 1, with no raised CONSTRAINT_ERROR message.

Step 8 · Recognise the errors beginners hit

Make two common mistakes on purpose so you know the messages.

A missing semicolon. Leave the semicolon off the last line:

Bash
cd ~/hello-ada-demo
mkdir -p broken && cd broken
printf 'with Ada.Text_IO;\nprocedure Hello is\nbegin\n   Ada.Text_IO.Put_Line ("Hello, world!");\nend Hello\n' > hello.adb
gnatmake hello.adb

GNAT points at the line and column and says something like hello.adb:5:10: error: missing ";".

Mixing types. Assign an Integer to a Day_Of_Month:

Bash
cd ~/hello-ada-demo/broken
cat > mix.adb <<'ADA'
procedure Mix is
   type Day_Of_Month is range 1 .. 31;
   Count : Integer := 5;
   Today : Day_Of_Month;
begin
   Today := Count;
end Mix;
ADA
gnatmake mix.adb

GNAT stops with something like expected type "Day_Of_Month" defined at line 2 and found type "Standard.Integer", even though 5 is a perfectly good day. Ada never converts between number types for you. Write the conversion yourself, Today := Day_Of_Month (Count);, and Ada checks the range when that line runs.

Three more to recognise. A message something like "Put_Line" is not visible, followed by lines pointing at a-textio.ads, means the package is withed but you left out use Ada.Text_IO; or the Ada.Text_IO. prefix. Something like "Put_Line" is undefined means the with Ada.Text_IO; clause is missing too. And something like bash: ./hello: No such file or directory after a failed build means gnatmake stopped at an error, so read its output from the first message down.

Check it worked

You have seen missing ";" and expected type "Day_Of_Month" in your own terminal, and ls ~/hello-ada-demo/broken shows no hello or mix program, because neither build finished.

Step 9 · Try Alire and clean up

Alire is Ada's project and package manager, and its alr command plays the role Cargo plays for Rust. The current release is Alire 2.1.1. It creates a project layout, downloads libraries from its index, and can install a GNAT toolchain for you, which is the easiest route on Windows and macOS. Download alr for your system from https://alire.ada.dev, put it on your path, and check it with alr version. A hand-installed alr gets no apt security updates: to upgrade, download the newer release from the same page, replace the old binary with it, and run alr version again to confirm. Then create a project:

Bash
alr init --bin hello_alr
cd hello_alr
alr run

alr init --bin creates a folder with alire.toml (the project description) and src/hello_alr.adb, a main procedure following the same file-name rule you saw earlier. The first alr run asks you to choose a GNAT toolchain to download if it has not found one, then builds and runs the program. Add a line such as Ada.Text_IO.Put_Line ("Hello from Alire"); between begin and end in src/hello_alr.adb, with with Ada.Text_IO; at the top, and run alr run again to see it.

When you are done, delete the demo folder:

Bash
cd ~
rm -rf ~/hello-ada-demo

Keep the gnat package if you will carry on with Ada. To remove it, run sudo apt remove gnat gnat-14.

Check it worked

ls ~/hello-ada-demo reports No such file or directory. If you tried Alire, alr run inside hello_alr printed your greeting.

Next steps

  • Compare Ada's strict types with another language from the same family of readable, structured languages: Hello World in Pascal.
  • See a modern language that also stops whole classes of errors at compile time: Hello World in Rust.
  • Meet another language still running systems that must not fail, in banks and governments: Hello World in COBOL.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.