DEV Community

Cover image for COBOL on Ubuntu: Installation, Setup, and First Steps
Javier Jimenez
Javier Jimenez

Posted on

COBOL on Ubuntu: Installation, Setup, and First Steps

Prerequisites – Installing Homebrew and ASDF on Ubuntu

Documentation (Official?)

Popular Frameworks / Toolkits

(COBOL doesn’t use modern frameworks; these are the most common ones)

  1. GnuCOBOL (OpenCOBOL) — standard open-source compiler.
  2. TinyCOBOL — simple and educational.
  3. COBOL-IT — commercial, mainframe-compatible.

🛠️ Installation on Ubuntu

sudo apt update
sudo apt install open-cobol
# or
sudo apt install gnucobol
Enter fullscreen mode Exit fullscreen mode

🍺 Installation with Homebrew

brew install gnu-cobol
Enter fullscreen mode Exit fullscreen mode

📦 Standard Package Manager

COBOL **does not have an official package manager* (it’s not a modular ecosystem like Node or PHP).*

🔧 Installation with ASDF

There is no official ASDF plugin for COBOL.


📝▶️ Create and Run a COBOL File

Important: In COBOL, the first 7 characters of each line must remain empty because they were originally reserved for sequence numbers used on punched cards. These numbers allowed cards to be physically reordered if shuffled. Even though they’re no longer needed today, this historical structure is preserved for compatibility, so these first eight characters are still treated as a special non-code area.
cobol.esIBM – Cobol for Linux

Col: 1 2 3 4 5 6 7 8
      |       | | |
      |       | | └──→ Code begins here
      |       | └────→ Indicator (comments, continuation…)
      └──────────────→ Sequence area (1–6)

000100*······Sample program
000200 IDENTIFICATION DIVISION.
000300 PROGRAM-ID.··HELLOWORLD.
000400 ENVIRONMENT DIVISION.
000500 DATA DIVISION.
000600 WORKING-STORAGE SECTION.
000700 01·MESSAGE·PIC·X(20)·VALUE·"HELLO·WORLD".
000800 PROCEDURE DIVISION.
000900     DISPLAY·MESSAGE.
001000     STOP·RUN.
Enter fullscreen mode Exit fullscreen mode

Note: There is a “free format” mode >>SOURCE FORMAT FREE that supposedly allows skipping the blank spaces, but it didn’t work for me.

Create file: touch hello.cob

      *COBOL SAMPLE PROGRAM TO VERIFY COBOL INSTALLATION
      *IMPORTANT - Reminder: COBOL code must begin at COLUMN 8.
      *Use '*' in column 7 for comments.
       IDENTIFICATION DIVISION.
       PROGRAM-ID. HELLO.
       PROCEDURE DIVISION.
           DISPLAY "COBOL is installed and working!".
           STOP RUN.
Enter fullscreen mode Exit fullscreen mode

Same file in free format (no comments):

>>SOURCE FORMAT FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
    DISPLAY "Hello COBOL World".
    STOP RUN.
Enter fullscreen mode Exit fullscreen mode

Compile:

cobc -x hello.cob -o hello
Enter fullscreen mode Exit fullscreen mode

Run:

./hello
Enter fullscreen mode Exit fullscreen mode

🟦 Basic COBOL Example

What It Does:

  1. Reads a text file.
  2. The file contains two numbers per line (e.g., 5 7).
  3. Multiplies them.
  4. Prints to console: "the result is = X"

Input file (datos.txt)

Example content:

125 745
313 246787
140 4467
Enter fullscreen mode Exit fullscreen mode

Mini COBOL Program

Create file: multiplicar.cob

      *-------------------------------------------------------------*
      * PROGRAM TO READ TWO VALUES, MULTIPLY THEM, AND DISPLAY THEM *
      *-------------------------------------------------------------*
       IDENTIFICATION DIVISION.
       PROGRAM-ID. MULTIPLY.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT FILEIN ASSIGN TO "datos.txt"
               ORGANIZATION IS LINE SEQUENTIAL.

       DATA DIVISION.
       FILE SECTION.
       FD FILEIN.
       01 RECORD-LINE                  PIC X(50).

       WORKING-STORAGE SECTION.
       01 NUM1                          PIC 9(9).
       01 NUM2                          PIC 9(9).
       01 RESULT                        PIC 9(18).
       01 END-FILE                      PIC X VALUE "N".

       PROCEDURE DIVISION.
       MAIN-LOGIC.
           OPEN INPUT FILEIN

           PERFORM UNTIL END-FILE = "S"
               READ FILEIN
                   AT END
                       MOVE "S" TO END-FILE
                   NOT AT END
                       PERFORM PROCESS-LINE
               END-READ
           END-PERFORM

           CLOSE FILEIN
           STOP RUN.

       PROCESS-LINE.
           UNSTRING RECORD-LINE
               DELIMITED BY SPACE
               INTO NUM1 NUM2

           COMPUTE RESULT = NUM1 * NUM2

           DISPLAY "the result is = " RESULT.

Enter fullscreen mode Exit fullscreen mode

📝▶️ How to Compile and Run

# Compile file:
cobc -x multiplicar.cob -o multiplicar
#Run compiled program
./multiplicar
Enter fullscreen mode Exit fullscreen mode

Expected output:

the result is = 35
the result is = 6
the result is = 40
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
oscarpincho profile image
Oscar Pincho

me voy a robar esto 😼