Raylib Basic Setup

We’re going to be going over the basics of getting Raylib setup and running on a Raspberry Pi Zero W. Many of these steps can be used to setup the library on any of the earlier Pi models, but we’ll be focusing on the Zero W specifically.

Step 1: Installing Pi OS

This is a fairly straightforward process for the experienced, and even the inexperienced will find it largely painless. First, you need an empty micro SD card, or at the very least, one whose contents you won’t miss. Once you have that, you will want to acquire a copy of Raspberry Pi Imager. This software can be run on Windows, Mac OSX, or Linux, and it will assist in downloading the various versions of Raspberry Pi OS, and flashing them to your micro SD card. There are other options for this part of the installation process, but Raspberry Pi Imager is one of the easiest and simplest.

Insert your micro SD card in your computer, and then start the Imager program. Click on the “Storage” button, and select your micro SD card. Then click on the “Operating System” button, and select “Raspberry Pi OS (other)”. From this list select “Raspberry Pi OS Lite (32-bit)” The 64-bit version won’t work with the Zero W, so it’s got to be 32-bit. The Zero W also has way less power, so we’re intentionally going with the command-line only Lite version.

Now click on the “Write” button. The program will ask you if you are sure you want to erase the contents of your micro SD card. If you are precious about any data on that card, you might want to back it up on a hard drive first. Otherwise, go ahead and click “Yes.” The Imager program will now write the Operating System to your micro SD card. It will likely take several minutes for the process to finish. Once it has, pull your micro SD card out of your computer, and pop it into the slot on your Raspberry Pi Zero W. We’re now ready to fire up the Pi.

When you start up your Zero W with the freshly installed OS, it will ask you to decide on a few options. The first option it will present you with is which keyboard you are using. The default set it presents you with is the UK keyboard set. If you live in the UK, then goody for you, just select whichever option fits and you’re good to go. If, like me, you don’t live in the UK, definitely select the “Other” option. It will present you with an extensive list of other regions. Pick whichever one applies to you and then pick whichever keyboard option matches the keyboard you are currently using.

Next, you will be prompted to input a username and password. This is vanilla for most linux installs, and isn’t all that big a deal. You can get super secure if you want, or just go with something quick and easy to remember, it depends on what you’re planning to do with this computer. For personal development, I’d recommend quick and easy. For a public display that you intend to present other people, something more secure might be appropriate.

Congratulations, your Pi OS Lite is installed, and you have a command prompt to work with


Step 2: Changes

A stock Lite install is nice, but it doesn’t do very much on its own. Thankfully, this is a Zero W, so it’s time for us to connect this thing to the internet. Type the following command…

sudo raspi-config

This will open up the standard interface for making system changes. It’s handy for a lot of things, but it also makes connecting to your wifi network a lot easier. Select “System Options,” and then select “Wireless LAN.” It will ask you which country the Pi is going to be used in, and then it will ask you for your SSID and password. Sweet, now you should be connected to your local wifi, which should get you internet access.

Now it’s time to run a step that you should basically do for any fresh linux install. Type the following command…

sudo apt-get update

This will likely take a minute or two to run, but it will go to the public repository and update your Zero W with the latest version information of all the software available. Once it finishes, type this command…

sudo apt-get upgrade

This will upgrade the existing software installed on your computer to the latest versions available. The public repositories get updated regularly, so it doesn’t hurt to run these commands after making a fresh OS install.

Note: Not For Bookworm

The following section for modifying the /boot/config.txt file for HDMI settings is only applicable to older versions of Raspberry Pi OS. The recent Bookworm version of the OS does not require these changes. If you have installed Bookworm or later on your Pi Zero, feel free to skip ahead to Step #3

Now to tend to a particular task. One thing I discovered when playing around with raylib was that the default settings on the Pi Zero limit the refresh rate of programs targeting a resolution of 1080p to 30hz. So, if you try to run a raylib program at 1080p, the framerate will be capped at 30 fps, no matter what you try to do. This isn’t an issue with raylib itself, it is a default setting on the OS Lite. Type the following command…

sudo nano /boot/config.txt

This opens up the nano command-line text editor, and opens the boot config file within it. There should be two lines in the middle of the file that look like this…

#hdmi_group=1
#hdmi_mode=1

Move your cursor to this section, and make the following changes.

hdmi_group=2
#hdmi_mode=1

We removed the pound sign in front of the hdmi_group property, and changed the value to 2. Now hit Ctrl+S to save, and then Ctrl+X to exit nano.

What exactly did this do? The default hdmi_group setting targets the Zero W at displaying on televisions, and the 1080p settings available in that group don’t support 60hz, only 30hz. The “2” group for hdmi is targeted at monitors, and fully supports 1080p60hz.

If you were planning on only using 720p or below screens, you could probably have skipped this part, hdmi_group=1 does have support for 720p60hz. But 1080p screens are extremely common these days, and I hate having my options limited. If I’m going to run things at framerates lower than 60 fps, it should be by my own choice, not some limitation imposed by the OS.

If you made the change to the boot config, go ahead and re-start your Zero W. This generally involves pulling the power cable out, and then plugging it back in. That particular change won’t take effect until the system reboots.


Step 3: Dependencies

One of the nice things about Raylib is that it has very little in the way of dependencies. In terms of the actual programs it produces, it has no dependencies. But that doesn’t mean we can dive directly into compiling it. While it has no runtime dependencies, it does have a few software development libraries that it needs us to install first. Type in the following command…

sudo apt-get install libdrm-dev libegl1-mesa-dev libgles2-mesa-dev libgbm-dev

This will install several development libraries that are needed for compiling the DRM version of raylib that will run on OS Lite.

Now we need to acquire the source code for compiling. We will first need to install git. Type the following command…

sudo apt-get install git

Once git has finished installing, you will most likely want to create some basic directories. The command for that is…

mkdir NameOfDirectory

I like to include a Documents folder, or perhaps just a Development folder, and then separate folders for the different programming languages I like to code in. But it’s up to you. Whatever structure you prefer to keep your files organized is fine. You can even just checkout the git project in the current directory. It could make things messy in terms of organization, but it won’t hurt anything. Once you have your directories the way you like, navigate into whatever directory you want the raylib source code to live in, and type the following command…

git clone https://github.com/raysan5/raylib.git raylib

Once git has finished checking out the project, you will have all of the source files you need for compiling!


Step 4: Compiling

Having the source is not enough, now you’ll need to actually compile raylib on your system. Type the following…

cd raylib/src

This navigates you into the raylib source code folder. You now must decide whether you want to compile the project as a static library, a shared library, or both.

To compile a static library, type…

make PLATFORM=PLATFORM_DRM

To compile a shared library, type…

make PLATFORM=PLATFORM_DRM RAYLIB_LIBTYPE=SHARED

If you were compiling the static library, it should generate a file called “libraylib.a”. If you were compiling a shared library, the generated file will be called “libraylib.so” These are the library files, and they can be used to generate or run future raylib projects.

Experienced coders will likely be familiar with static and shared libraries. For anyone who isn’t, static libraries contain collections of code that you can build into other projects. If you reference a static library in another project, all of the code from the static library will be compiled into the executable, essentially becoming part of the project. A shared library, on the other hand, is a separate instance. When you reference a shared library, the functions within the library can be used at runtime, but they do not become part of the executable.

In slightly more practical terms, static libraries are commonly used for importing code, while shared libraries are commonly used for dynamic language binding. If you want to combine raylib into a C or C++ project you are working on, you would use a static library. If you want to reference raylib from a separate language, like C# or Python, you would use a shared library.

And that’s basically it! At this point, you have compiled raylib to run on your Raspberry Pi Zero W, and configured your Pi Zero W to handle running it properly. The rest is up to you.


Quick List of Steps

All of that was quite a bit to take in. Let’s summarize briefly.

  1. Install Pi OS Lite on Micro SD card.
  2. Configure Pi OS Lite on startup
  3. sudo raspi-config
  4. System Options – > Wireless LAN
  5. sudo apt-get update
  6. sudo apt-get upgrade
  7. sudo nano /boot/config.txt
  8. Uncomment #hdmi_group=1
  9. Switch hdmi_group=1 to hdmi_group=2
  10. sudo apt-get install libdrm-dev libegl1-mesa-dev libgles2-mesa-dev libgbm-dev
  11. sudo apt-get install git
  12. Make your directories (mkdir NameOfDirectory)
  13. Navigate to directory of choice
  14. git clone https://github.com/raysan5/raylib.git raylib
  15. cd raylib/src
  16. make PLATFORM=PLATFORM_DRM (for static)
  17. make PLATFORM=PLATFORM_DRM RAYLIB_LIBTYPE=SHARED (for shared)

That’s a bit more concise. Feel free to use this cheat-sheet if you need to repeat this process more than once.


Optional Step: Sharing

This step is optional, you can code and work directly on the Pi Zero if you want to. But a command-line interface is not generally considered the best modern development environment. A minimal Pi OS Lite environment is great for insuring that you get to use all of the system’s resources for just your program, but using it to make game assets and art is unrealistic.

To that end, we’re going to install some remote access so we can log into our Pi Zero from a different computer, and work on software for it from there. Building and running programs will still happen on the Zero, but the actually editing can happen elsewhere.

For convenience, we will be using Samba, a common file-sharing server. Type in the following command…

sudo apt-get install samba samba-common-bin

Once this has finished installing, we will have all the software on our Pi Zero to run a standard Samba server. But we do still need to change up the settings a bit. Type this command…

sudo nano /etc/samba/smb.conf

This will open up the Samba configuration file in our command-line text editor. Scroll down to the bottom of the file, and add the following lines of code…

[nameofshare]
path = /home/user/directorytoshare
writeable = Yes
create mask = 0777
directory mask = 0777
public = no

For this example, you would replace “nameofshare” with whatever you want your file-sharing specific name to be, and the “directorytoshare” with the name of whatever directory you created that you want to provide access to. Once you have all of this added to the file, hit Ctrl+X, and you can save and exit the file.

This will configure Samba to share the desired directory, but we still need a samba user profile, and a password. Type this command…

sudo smbpasswd -a username

Here, “username” will be whatever you want your Samba-specific username to be. I often use the same username that I used to set up the Raspberry Pi OS in the first place. But it can be anything. Just remember what it is for when you attempt to log in to Samba.

That does it for basic setup. Now just type in the following command to restart the Samba server with our altered settings…

sudo systemctl restart smbd

And that should do it. It should now be possible to log into your Raspberry Pi Zero remotely from a different computer. In Windows, you would right-click on your MyComputer icon in the file browser, and select “Map to Network Drive.” When it asked for the address to the drive, you would type in…

\\192.168.0.18\nameofshare

In this case, the string of numbers is the IP address of your Pi Zero on the local network, and “nameofshare” is whatever value you added in the square brackets in the smb.conf file we edited earlier. If you input the information correctly, Windows would prompt you to supply your username and password. Just give it the Samba username and password you already defined. That should open a file explorer window that provides you with direct access to your Raspberry Pi Zero’s filesystem.

If you need to check what the current IP address is for your Pi Zero, just type this command in…

hostname -I

It will print the numeric IP address that is presently assigned to your Pi Zero, and you can use that to map to it from other computers.