MainframeCOBOL

Free COBOL Compiler: Complete Setup Guide for Linux, Windows & macOS (2026)

TT
TopicTrick Team
Free COBOL Compiler: Complete Setup Guide for Linux, Windows & macOS (2026)

Getting a COBOL development environment running is simpler than it was five years ago. GnuCOBOL installs in minutes on any Linux, macOS, or Windows machine, and IBM now offers free access to a real z/OS mainframe through Z Xplore. This guide walks through every option from the fastest online tools to a professional local setup, with working code examples you can run immediately.

What you'll learn in this guide:

  • Installing GnuCOBOL on Linux, macOS, and Windows (WSL2) step by step
  • Compiling and running your first COBOL programs from the command line
  • A complete file I/O example — reading a sequential file and producing a report
  • Setting up VS Code with IBM Z Open Editor for professional COBOL editing
  • Getting free access to a real IBM z/OS mainframe via IBM Z Xplore
  • Fixing the most common GnuCOBOL errors beginners encounter

Option 1: Browser-Based — Zero Setup, Run Now

If you want to test a COBOL snippet immediately, two browser tools work without any installation:

  • JDoodle — jdoodle.com/execute-cobol-online — paste code, click Execute
  • OneCompiler — onecompiler.com/cobol — cleaner interface, syntax highlighting, stdin support

Both use GnuCOBOL under the hood. Limitations: no filesystem access, no DB2 or CICS, execution time limits. Suitable for testing examples from tutorials but not for building real programs.


Option 2: GnuCOBOL on Linux — Recommended for Serious Learning

GnuCOBOL compiles COBOL source to C, then compiles the C to a native binary. The result is a real executable with full access to the filesystem, environment variables, and standard I/O — everything you need to practise file handling, table processing, and batch programs.

Install on Ubuntu / Debian

bash
sudo apt-get update
sudo apt-get install gnucobol

Install on Fedora / RHEL / CentOS

bash
sudo dnf install gnucobol

Install on Arch Linux

bash
sudo pacman -S gnucobol

Verify the Installation

bash
cobc --version

You should see output like GnuCOBOL 3.1.2 or later. If the command is not found, try cobc-3 or check your package manager installed to a non-standard path.

Your First Compile

Create a file called hello.cob:

cobol
       IDENTIFICATION DIVISION.
       PROGRAM-ID. HELLO.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-MESSAGE    PIC X(30) VALUE 'Hello from COBOL on Linux!'.

       PROCEDURE DIVISION.
       MAIN-PARA.
           DISPLAY WS-MESSAGE
           STOP RUN.

Compile and run:

bash
cobc -x hello.cob -o hello
./hello

Output: Hello from COBOL on Linux!

The -x flag produces a standalone executable. Without it, GnuCOBOL produces an object file for linking into a larger program.


Option 3: GnuCOBOL on Windows via WSL2

The cleanest way to run COBOL on Windows is through WSL2 (Windows Subsystem for Linux 2), which gives you a real Ubuntu environment inside Windows without a virtual machine.

Step 1 — Install WSL2

Open PowerShell as Administrator and run:

powershell
wsl --install

This installs WSL2 with Ubuntu by default. Restart your machine when prompted.

Step 2 — Open Ubuntu

After restarting, open Ubuntu from the Start menu (or type wsl in a terminal). On first launch, create a username and password.

Step 3 — Install GnuCOBOL

Inside the Ubuntu terminal:

bash
sudo apt-get update && sudo apt-get install gnucobol -y
cobc --version

Step 4 — Create and compile a program

bash
mkdir ~/cobol-projects && cd ~/cobol-projects
nano hello.cob

Paste the hello.cob example from above, save (Ctrl+O, Enter, Ctrl+X), then:

bash
cobc -x hello.cob -o hello && ./hello

Accessing Windows files from WSL2: Your Windows C: drive is mounted at /mnt/c/. If your COBOL files are at C:\Users\you\cobol, access them from WSL2 at /mnt/c/Users/you/cobol.


Option 4: GnuCOBOL on macOS

bash
brew install gnucobol
cobc --version

If you do not have Homebrew: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Compilation and execution work identically to Linux.


GnuCOBOL Compiler Flags Reference

FlagPurpose
-xProduce standalone executable
-o filenameSet output filename
-freeFree-format source (no column restrictions)
-std=ibmEnable IBM COBOL dialect extensions
-std=cobol2014Strict COBOL 2014 standard
-WallShow all warnings
-debugRuntime debug support (boundary checks, etc.)
-gInclude debug symbols for gdb
-vVerbose — show intermediate C compilation steps

For IBM-dialect programs from courses or textbooks, always add -std=ibm to your compile command.


A Complete File I/O Example

Once you have GnuCOBOL installed, this example demonstrates sequential file reading and report writing — the core of real COBOL batch programming.

Step 1 — Create the input file

bash
cat > employees.dat << 'EOF'
001John Smith          DEVELOPER  085000
002Sarah Jones         ARCHITECT  125000
003Mike Patel          ANALYST    072000
004Lisa Wong           MANAGER    110000
005Ahmed Hassan        DEVELOPER  091000
EOF

Step 2 — Create the COBOL program

cobol
       IDENTIFICATION DIVISION.
       PROGRAM-ID. EMPREPORT.
       AUTHOR. TOPICTRICK.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT EMPLOYEE-FILE ASSIGN TO 'employees.dat'
               ORGANIZATION IS LINE SEQUENTIAL.
           SELECT REPORT-FILE   ASSIGN TO 'report.txt'
               ORGANIZATION IS LINE SEQUENTIAL.

       DATA DIVISION.
       FILE SECTION.
       FD  EMPLOYEE-FILE.
       01  EMP-RECORD.
           05  EMP-ID            PIC 9(3).
           05  EMP-NAME          PIC X(20).
           05  EMP-ROLE          PIC X(10).
           05  EMP-SALARY        PIC 9(6).

       FD  REPORT-FILE.
       01  REPORT-LINE           PIC X(60).

       WORKING-STORAGE SECTION.
       01  WS-EOF-FLAG           PIC X VALUE 'N'.
           88  END-OF-FILE       VALUE 'Y'.
       01  WS-TOTAL-SALARY       PIC 9(9) VALUE ZERO.
       01  WS-RECORD-COUNT       PIC 9(4) VALUE ZERO.
       01  WS-AVG-SALARY         PIC 9(6) VALUE ZERO.
       01  WS-REPORT-LINE.
           05  WS-RL-ID          PIC 9(3).
           05  FILLER            PIC X VALUE SPACE.
           05  WS-RL-NAME        PIC X(20).
           05  FILLER            PIC X VALUE SPACE.
           05  WS-RL-ROLE        PIC X(10).
           05  FILLER            PIC X VALUE SPACE.
           05  WS-RL-SALARY      PIC $ZZ9,999.

       PROCEDURE DIVISION.
       MAIN-PARA.
           OPEN INPUT  EMPLOYEE-FILE
           OPEN OUTPUT REPORT-FILE

           MOVE '--- EMPLOYEE SALARY REPORT ---' TO REPORT-LINE
           WRITE REPORT-LINE
           MOVE SPACES TO REPORT-LINE
           WRITE REPORT-LINE

           PERFORM READ-EMPLOYEES UNTIL END-OF-FILE

           COMPUTE WS-AVG-SALARY = WS-TOTAL-SALARY / WS-RECORD-COUNT
           MOVE SPACES TO REPORT-LINE
           WRITE REPORT-LINE
           STRING 'Total employees: ' WS-RECORD-COUNT
                  DELIMITED SIZE INTO REPORT-LINE
           WRITE REPORT-LINE
           STRING 'Average salary : $' WS-AVG-SALARY
                  DELIMITED SIZE INTO REPORT-LINE
           WRITE REPORT-LINE

           CLOSE EMPLOYEE-FILE
           CLOSE REPORT-FILE
           DISPLAY 'Report written to report.txt'
           STOP RUN.

       READ-EMPLOYEES.
           READ EMPLOYEE-FILE
               AT END MOVE 'Y' TO WS-EOF-FLAG
               NOT AT END
                   ADD 1 TO WS-RECORD-COUNT
                   ADD EMP-SALARY TO WS-TOTAL-SALARY
                   MOVE EMP-ID     TO WS-RL-ID
                   MOVE EMP-NAME   TO WS-RL-NAME
                   MOVE EMP-ROLE   TO WS-RL-ROLE
                   MOVE EMP-SALARY TO WS-RL-SALARY
                   MOVE WS-REPORT-LINE TO REPORT-LINE
                   WRITE REPORT-LINE
           END-READ.

Step 3 — Compile and run

bash
cobc -x empreport.cob -o empreport
./empreport
cat report.txt

This program reads every employee record sequentially, writes a formatted report line per record, then calculates and writes the total headcount and average salary. It demonstrates SELECT/FD, sequential READ with AT END, WRITE, COMPUTE, and STRING — the core file I/O verbs from the COBOL File Handling lesson.


Option 5: VS Code + IBM Z Open Editor

For a professional editing experience, pair GnuCOBOL with VS Code and IBM's free extension.

Setup

  1. Install Visual Studio Code
  2. Open Extensions (Ctrl+Shift+X), search IBM Z Open Editor, click Install
  3. Optionally also install COBOL Language Support (Broadcom) for additional linting

What you get

  • Syntax highlighting for COBOL, JCL, HLASM, and REXX
  • Code completion for COBOL keywords, division names, and paragraph names
  • Outline view — navigate divisions, sections, and paragraphs in a tree
  • Hover documentation for COBOL keywords
  • Diagnostic messages for common syntax problems (before you compile)

To compile from VS Code: open a terminal (Ctrl+) — if using WSL2, VS Code will connect to the WSL2 environment automatically when you open a folder from within WSL2. Run cobc -x yourfile.cob -o yourprog && ./yourprog` directly in that terminal.


Option 6: IBM Z Xplore — Real z/OS for Free

IBM Z Xplore is the most important free resource for mainframe COBOL learners. It provides browser-based access to a real z/OS system on genuine IBM Z hardware — meaning IBM Enterprise COBOL, JES2 for job submission, ISPF editor, VSAM, and DB2.

How to Sign Up

  1. Go to ibm.com/community/z-xplore and click Get Started Free
  2. Create a free IBM account (no credit card needed)
  3. Complete the short registration — you receive login credentials within minutes
  4. Access the system through the browser-based 3270 terminal emulator or the Zowe web interface

What You Can Do

  • Submit JCL batch jobs and view output in SDSF
  • Compile and run COBOL programs using the actual IGYCRCTL compiler
  • Create and access VSAM KSDS datasets
  • Run embedded SQL COBOL programs against a real DB2 instance
  • Use TSO/ISPF — the standard mainframe editing and navigation environment

Z Xplore is the right tool once you have learned basic COBOL syntax with GnuCOBOL and want to understand JCL, the compile-link-execute lifecycle, and mainframe-specific features. See the COBOL JCL: Compile, Link-Edit, and Run lesson for the full JCL walkthrough.


Common GnuCOBOL Errors and Fixes

error: PROGRAM-ID is required You have not included the IDENTIFICATION DIVISION or PROGRAM-ID paragraph. Both are required as the first division.

error: syntax error, unexpected word Usually a column alignment problem. Traditional COBOL requires code to start in column 8 (Area A for divisions/sections) or column 12 (Area B for statements). Check your indentation — if editing in a non-COBOL-aware editor, invisible tab characters can misalign code.

warning: subscript ... will cause overflow A table subscript exceeds the declared OCCURS size. Always validate subscripts before accessing table elements.

./hello: error while loading shared libraries: libcob.so.4 GnuCOBOL's runtime library is not on your library path. Fix: export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH or add it to your .bashrc.

Programs run but produce garbage numeric output A numeric PIC field was not initialised with a VALUE clause and contains non-numeric data. Always give VALUE ZERO to numeric working-storage fields.

error: FILE not found at runtime (not compile time) The file path in your ASSIGN TO clause does not match the actual location of the file. GnuCOBOL resolves the path relative to the working directory from which you run the executable, not the directory where the source code lives.


Choosing the Right Tool

GoalBest option
Test a snippet in 30 secondsJDoodle or OneCompiler
Work through a COBOL course locallyGnuCOBOL on Linux or WSL2
Professional editing with autocompleteGnuCOBOL + VS Code + IBM Z Open Editor
Learn JCL, VSAM, DB2, real z/OSIBM Z Xplore (free)
Full mainframe emulation at homeHercules + TK5 starter system

Continue Learning COBOL

With your compiler set up, the natural next step is the structured learning path in the COBOL Mastery Course — 20 modules from program structure through DB2 and CICS, designed to take you from first compile to production-ready skills.

If you are wondering whether the time investment is worth it, see the COBOL Developer Salary & Career Guide 2026 for current salary data and job market outlook.


Ready to Master COBOL?

This lesson is part of the COBOL Mastery Course — the complete reference from first program to production mainframe. 20 modules covering COBOL syntax, file handling, DB2, CICS, JCL, and modern features. Free, fresher to senior.

→ View the full COBOL Mastery Course