(updated Mar 26, 2017 to fix latest seed/bootstrap compiler)
I have long wanted a recent version of Lazarus for Raspberry Pi. I’ve looked at recompiling it, but the exact instructions have been hard to come by. Until last week! I accidently stumbled into Tweaking4All’s blog showing exactly how to do it:
His instructions start from a brand new RPI Model 2 with a blank SD card and get you to a running installation. I’ve followed the procedure 3 times now and it has worked w/o problem.
Before going any further, I should mention this to any Lazarus / Free Pascal newcomer: if you have never used Lazarus before and just want to play with it, don’t follow these procedures. They are long and complicated. Instead, just install the old version of the compiler using these instructions. The old version of the compiler is very stable and most likely has all the features a newcomer will want.
If you want to seriously write Pascal code for the RPI, THEN these instructions are worth the extra trouble!
I’m going to summarize the steps as I did them. I also start with the assumption you already have a running RPI. I am using a Model 2 B with a USB HDD (you won’t need one, but it make everything run snappier).
My instructions have been modified to produce a compiler that will produce ARMV6 code which will allow your programs to run on both Model 1 and 2. If you are going to only use your code on model 2’s, change the OPT on all of the make commands to remove the -CpARMV6 and -OpARMV6 switches.
These are terse instructions and assume you know what you are doing. If you’ve not done this before, I suggest you use the original site. Once you are familar with the procedure, these instructions are easy to copy/paste straight into a terminal window.
Prepare the RPI
It is best to use SSH, not VNC to access the RPI as you want to reduce memory /CPU consumption as much as possible. For the same reason, make sure Firefox is NOT running.
I make sure the O/S is up to date and the Raspbian supplied version of Lazarus / Free Pascal has been removed.
sudo su apt-get update apt-get upgrade apt-get remove lazarus apt-get remove fpc apt-get autoremove reboot
Everything I do from this point forward (until I mention othersize) is done as root. Therefore, rather than constantly have to remember to type sudo, I will simply type
to become root.
Increase Swap File Size
The default swap file size is 256MB. It appears that trying to recompile the Free Pascal compiler will not work unless you increase the swapsize to 1GB. Once you have built the compiler, you can go back to a swap file of 256MB.
Make sure that you have enough disk space to do this:
rpi/~:df -h Filesystem Size Used Avail Use% Mounted on rootfs 97G 6.1G 87G 7% / /dev/root 97G 6.1G 87G 7% / devtmpfs 460M 0 460M 0% /dev tmpfs 93M 248K 93M 1% /run tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 186M 0 186M 0% /run/shm /dev/mmcblk0p1 56M 19M 38M 34% /boot
On my RPI, you can see that root has 87G available. I have plenty of room to increase the swap file size AND recompile the compiler.
Edit the config file
and it will contain only the line:
Save the config file and restart swapping:
/etc/init.d/dphys-swapfile stop /etc/init.d/dphys-swapfile start
ls /var and verify /var/swap is now 1GB:
rpi/~:ls -lh /var total 1001M drwxr-xr-x 2 root root 4.0K Jun 13 18:02 agentx/ drwxr-xr-x 2 root root 4.0K Jun 13 06:25 backups/ drwxr-xr-x 12 root root 4.0K Jan 21 2014 cache/ drwxr-xr-x 43 root root 4.0K Apr 27 14:47 lib/ drwxrwsr-x 2 root uucp 4.0K Dec 25 2012 local/ lrwxrwxrwx 1 root root 9 Feb 8 2013 lock -> /run/lock/ drwxr-xr-x 11 root root 4.0K Jun 13 18:02 log/ drwxrwsr-x 2 root mail 4.0K Feb 8 2013 mail/ drwxr-xr-x 2 root root 4.0K Feb 8 2013 opt/ lrwxrwxrwx 1 root root 4 Feb 8 2013 run -> /run/ drwxr-xr-x 4 root root 4.0K Feb 8 2013 spool/ -rw------- 1 root root 1000M Jun 12 11:17 swap drwxrwxrwt 2 root root 4.0K May 21 08:55 tmp/ drwxrwxr-x 2 root danh 4.0K Jan 21 2014 www/
apt-get install -y subversion
Install the Seed/Bootstrap Pascal Compiler
As of Mar 2017, the bootstrap compiler is located here:
You will want the fpc-3.0.2.arm-linux-eabihf-raspberry.tar compiler.
Copy the compiler onto your system:
mkdir /usr/local/fpc cd /usr/local/fpc wget https://sourceforge.net/projects/freepascal/files/Linux/3.0.2/fpc-3.0.2.arm-linux-eabihf-raspberry.tar/download tar xvf fpc-2.6.4.arm-linux.tar cd fpc-2.6.4.arm-linux
You are ready to install the compiler. You MUST ANSWER /usr/local to the first question! To all other questions, just press enter to accept the default.
./install.sh #ANSWER 1st Q with /usr/local!!!! Rest are default.
Install Latest Version of Free Pascal
These instructions are going to install the most recent version of Free Pascal which is found at http://svn.freepascal.org/svn/fpc/trunk.
If you want to install an older version, take a look at http://svn.freepascal.org/svn/fpc/branches to decide the version.
NOTE: I was unable to compile version 2.6.4. In researching the problem, I found someone state that ARM systems are not supported until version 3.0.1. As of Mar 2017, you minimally need FPC version 3.0.0 to compile Lazarus.
If you decide to use an older version you will alter the svn command from what I have below:
cd /usr/local/fpc svn co http://svn.freepascal.org/svn/fpc/trunk source cd source # each of the following are fairly slow and may abort with errors # I copy and paste them one at a time: make all OPT="-dFPC_ARMHF -CpARMV6 -OpARMV6" make install OPT="-dFPC_ARMHF -CpARMV6 -OpARMV6" PREFIX=/usr/local make install sourceinstall OPT="-dFPC_ARMHF -CpARMV6 -OpARMV6" PREFIX=/usr/local
Now you need to determine the installed version of FPC and create a sym link to that version.
To see the versions of pascal installed, type:
rpi/~:ls /usr/local/lib/fpc 2.6.4/ 3.1.1/ lexyacc/
I’m going to be using 3.1.1. The 2.6.4 version is the seed compiler. I suspect it would not be a good idea to use it as your production compiler.
Remove the old sym link and create a new one to the 3.1.1 compiler:
rm -f /usr/local/bin/ppcarm ln -sf /usr/local/lib/fpc/3.1.1/ppcarm /usr/local/bin/ppcarm
Finally, verify that when you invoke the fpc compiler, you get the version you expect:
Install Additional Packages
apt-get install libx11-dev libgdk-pixbuf2.0-dev libcairo2-dev gir1.2-coglpango-1.0 libpangox-1.0-dev xorg-dev libgtk2.0-dev libpango1.0-dev
Download and Compile Lazarus
As with FPC, this will download the latest version. You can find old versions at http://svn.freepascal.org/svn/lazarus/tags. If you choose to use one, update the svn command below.
mkdir /usr/local/lazarus cd /usr/local/lazarus svn co http://svn.freepascal.org/svn/lazarus/trunk source cd source #copy/paste the following one at a time to verify they #both work without error make all OPT="-dFPC_ARMHF -CpARMV6 -OpARMV6" make install OPT="-dFPC_ARMHF -CpARMV6 -OpARMV6" PREFIX=/usr/local
Remove Lazarus Source (you don’t want to do this for FPC, as Lazarus needs it)
rm -rf /usr/local/lazarus
Fixing the GCC Library path
As set up so far, the compiler will run, but linking will return a warning indicating crtbegin.o and crtend.o could not be found. The program still links OK, but I don’t like warnings like this.
To correct the problem, I located crtbegin.o using
sudo find / -name crtbegin.o /usr/lib/gcc/arm-linux-gnueabihf/4.6/crtbegin.o
It is in /usr/lib/gcc/arm-linux-gnueabihf/4.6.
This library needs to be added to the /etc/fpc.cfg file.
sudo vi /etc/fpc.cfg
If you look thru this file you will find a comment for the library, but no path:
# path to the gcclib
You just need to add the path found above:
# path to the gcclib -Fl/usr/lib/gcc/arm-linux-gnueabihf/4.6
Ready to Run Lazarus
You are ready to run Lazarus. First, exit so you are no longer root. Use whoami to verify you are back on your normal user (pi maybe?).
I’m assuming if you are trying to do all of this yourself, you understand Lazarus runs in XWindows and you will need to be running XWindows either directly via the HDMI connection or by using tightVNC to access the RPI. If none of this makes sense, see the source article. It will explain this much better.
To start lazarus, click on <menu> | Programming | Lazarus.
If you had Lazarus installed already, you will most likely see this Upgrade Window. Just click Upgrade:
Chances are pretty good that you will get the ‘Configure Lazarus IDE’ window next:
I use the following settings for each tab. If the setting is correct, you should see ‘OK’ in the 2nd window.
Lazarus Tab: /usr/local/share/lazarus/
Compiler Tab: fpc
FPC Sources Tab: /usr/local/fpc/source
Make Tab: make
Debugger Tab: /usr/bin/gdb
Click on Start IDE.
I like to do a simple Hello World test:
Lastly, if you changed the swap file size, go back and set it back to the original size and reboot the RPI.