COBOL on a Mac

If you are thinking about trying out the COBOL programming language and you have a macOS computer, I will outline afew ways to get you started. The first option is to install the last stable version of GnuCOBOL and the second, is to install one of the upcoming versions. I personally used opton 1 initially myself but am now use option 2 to get and use a more recent version.

1. Use HomeBrew

This is the method I initially used to install GnuCOBOL onto my Mac. Firstly, you need to open the Terminal app and type the following command to install HomeBrew. HomeBrew is a package manager that helps you install software onto your Mac, its home page is here.

/bin/bash -c "$(curl -fsSL"

It is probably easier to copy and paste the above into Terminal.

Now that we have the package manager in place, we can install the COBOL language by typing the following into terminal.

brew install gnu-cobol

This will firstly update and upgrade any existing brew packages, install GnuCobol and any dependencies, e.g. Berkeley database.

Once that is all done, you now have GnuCOBOL version 2.2 installed in your Mac.

2. Compile from source

This sounds like alot more work, but the following is how to get one of the latest versions of GnuCOBOL (I am using version 3.0rc1), you simply download the latest version from the GnuCOBOL site) from here.

After downloading the file, we need to extract the source code from an xz compressed file. In Terminal, install the xz formulae.

brew install xz

To uncompress the download file, we firstly need to change directory to Downloads folder and then decompress the file. Type each of the following in Terminal (but depending on the name of your downloaded file, you may need to change gnucobol-3.1-rc1.tar.xz to the whatever is the name of your file. The file I downloaded was for version 3.1 release candidate 1).

cd $HOME/Downloads
tar -xf gnucobol-3.1-rc1.tar.xz

Once that is complete, we change into the gnucobol-3.1-rc1 folder (if you downloaded file had a different name, use that name instead), then you configure to create a configuration file containing details of the source building options, you make the build and you install that made build. Type the following one at a time in Terminal.

cd gnucobol-3.1-rc1
make install

After your install (it may take a little while to configure and build the compiler source code), type the following into Terminal.

cobc --version

The following should appear on your Terminal screen.

cobc (GnuCOBOL) 3.0-rc1.0
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <>
This is free software; see the source for copying conditions.  There is NO
Written by Keisuke Nishida, Roger While, Ron Norman, Simon Sobisch, Edward Hart
Built     Jun 23 2020 10:40:59
Packaged  Apr 22 2018 22:26:37 UTC
C version "4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.10.44.4)"

This indicates you are now using version 3.0, release candidate 1.0 of GnuCOBOL.

Next Steps

The next steps involve some reading starting with some of the GnuCOBOL documentation from here, as well as possibly acquiring a guide for learning or refreshing your knowledge on the language. I highly recommend this book called “Beginning COBOL for Programmers” by Michael Coughlan, available from here. I am not affiliated with the author or publisher.

Although Michael covers it in his book, below is a small COBOL program you can try with your new compiler. The source code can be enter into an editor (I will discuss these in a later post) so for now use nano. In Terminal, type the following.

nano ./test.cbl

Copy and paste the following code into nano.

000002 PROGRAM-ID. Test.
000004 A000-Main SECTION.
000005   DISPLAY "Hello World".
000006   STOP RUN.
000007 END PROGRAM Test.

It should look like the following;

To exit and save the file, press and hold CONTROL and X, type Y to save the changes and enter on the filename to write of test.cbl.

To compile and run the program, we issue the following commands in Terminal.

cobc -x ./test.cbl

This compiles the test COBOL program into an executable program and then we run the program with “./test”. The following should appear on your Terminal screen.

Hello World

And there is your first test COBOL program. In a later post, I will discuss setting up an editor and some tools to assist you in coding in COBOL.

%d bloggers like this: