Lazarus is a Delphi-like rapid development environment for Free Pascal. It is open-source and runs on Windows, Linux, OS X and even on the Raspberry-Pi. Applications written with Lazarus can be re-compiled on each platform without modification (assuming you haven’t used any OS specific APIs). This means it is very easy to write cross-platform desktop applications on Linux.
To install Lazarus, you can either install the version in your distributions repository or download the .DEB or .RPM files from the project’s download page. On Ubuntu (and its variants) you can install Lazarus from the Ubuntu repository using:
or click here to install from the Ubuntu Software Center:
Note: The packages from the download pages will probably be newer than those in the repositories.
If you are familiar with the Delphi programming environment or with programming in Pascal, then using Lazarus should be relatively straight forward. This tutorial will assume that you have at least a passing knowledge of Pascal and of graphical application development. You can get a very basic introduction to Free Pascal in our Writing Pascal Programs on a Raspberry Pi Using FPC guide.
Once installed, you can start the program via the desktop or by running “lazarus” from the command line.
The IDE has four main windows. Along the top is the main Lazarus panel with access to the menu bar and a toolbar. To the left is the object inspector, and to the right is the source code editor. The form designer tends to float and can sometimes be difficult to locate. The quickest way to find it (if you can’t quite get your mouse onto it) is to click on the Windows menu and then on the name of the form (which is form1 by default).
Before starting any development, you need to save the project and the default source file. Click “File -> Save” and save the files in an appropriate directory. You can use the default names for this project or you can enter your own. It doesn’t really matter here as this will be a simple test project. For a real project, you will need to name the project and source files appropriately.
To start writing a very simple desktop application, you need to add some UI elements to the form. The example application we will build here has a text label which prompts you to enter your name and an edit box for the user input. Whenever the user enters a letter, a second text label will be updated with a greeting message for the user. It’s very simple and almost completely useless; however, it does demonstrate the very basic principles of writing a desktop application.
First click on the “TLabel” object in the Standard pane of the main Lazarus window. Its icon is the letters “Abc” with the “A” underlined. Now click somewhere towards the top-left of the form in the designer (form1). A label component will be added with the text “Label1.” In the Object Inspector, find the Caption property for Label1 and change it to “What is your name?”
Click on the “TEdit” component in the Standard pane and click on the form, just below the label added above. Resize it to cover about two-thirds of the form. Find the Text property of Edit1 and delete the string “Edit1,” leaving the property empty.
The final bit of UI design is to add another label below the edit box and change its Caption to “I will say hello when I know your name!”
Click on “Edit1” and select the “Events” pane in the Object Inspector. Double-click on the empty field next to “OnChange.” This will create a function in the source code called “TForm1.Edit1Change.” The cursor will be placed inside the function, ready for your to enter some code.
Between the “begin” and “end” enter the following line of code:
The code says that whenever the edit box (Edit1) is changed, the label will be changed to the string “Hello, ” concatenated with the text in the edit box. In other words, if I type “Gary” into the edit box, then Label2 will be set to “Hello, Gary.”
Click on the floppy disk icon (or use File->Save) to save the file and then run the application by clicking on the green triangle (or by pressing F9).
You can now experiment with all the other components that are provided which include buttons, list boxes, progress bars, common dialog boxes and much more.
The Free Pascal project has comprehensive developer documentation, along with a vibrant user community. Likewise, the Lazarus IDE also has extensive documentation and there are a set of forums that cover both the Free Pascal language and Lazarus.
If you have any questions about the example given above, please feel free to use the comments section below to ask any questions.
Our latest tutorials delivered straight to your inbox