Setting up a GitLab runner for MATLAB
Background
With the continuous method of software development, you continuously build, test, and deploy iterative code changes. This iterative process helps reduce the chance that you develop new code based on buggy or failed previous versions. With this method, you strive to have less human intervention or even no intervention at all, from the development of new code until its deployment.
Purpose of this guide
With this guide, you will create a Continuous Integration Pipeline on a repository within the TU Delft GitLab to use a MATLAB environment.
Prerequisites
- TU Delft NetID
- MATLAB account
- Basic knowledge of Linux (for setting up a server)
- Basic knowledge of Docker (for creating a custom MATLAB image)
- Know which MATLAB version and toolboxes you want to use in the CI/CD pipeline
Glossary of terms
CI/CD pipeline
A CI/CD pipeline automates your software delivery process. The pipeline builds code, runs tests (Continuous Intergation), and safely deploys a new version of the application (Continuous Delivery).
Docker
We use a Docker container to run the GitLab runner and initialise the CI/CD pipeline.
GitLab runner (from GitLab documentation)
Runners are the agents that run the CI/CD jobs that come from GitLab. When you register a runner, you are setting up communication between your GitLab instance and the machine where GitLab Runner is installed. Runners usually process jobs on the same machine where you installed GitLab Runner.
GitLab jobs
Pipeline configuration begins with jobs. Jobs are the most fundamental element of a .gitlab-ci.yml file. Each job is executed by a GitLab runner. See GitLab documentation for more info.
Steps
- Request a TU Delft Virtual Private Server
- Set up a GitLab runner
- Create a Docker image with a custom MATLAB installation
- Create a GitLab runner
- Register a GitLab runner
- Obtain a MATLAB license file
- Configure the CI/CD pipeline
- Add a job to test the pipeline
- Optional: Updating the MATLAB version
Step 1. Request a TU Delft VPS
If you want to work with the TU Delft GitLab instance and you want to implement CI/CD pipelines, then you need to install a GitLab runner on your own. Runners are the agents that run the CI/CD jobs that come from GitLab. Currently, the TU Delft instance does not provide this feature out-of-the-box. Therefore, we need a separate (virtual) server to run the GitLab runners and execute the jobs in the CI/CD pipeline.
The TU Delft offers Virtual Private Servers (VPS) for researchers through the TopDesk selfservice portal. If you don’t have a VPS already, please follow this guide to request a Virtual Private Server.
VPS requirements
- 50Gb disk space (the MATLAB installation in this guide requires ~10 Gb, but this depends on the size of the installed addons)
Step 2. Set up a GitLab runner
To set up a GitLab runner on the VPS, please follow this guide for setting up GitLab runners.
In summary, the steps are:
Install docker with
sudo apt install docker.ioVerify installation with
sudo docker --versionOptional: Move default storage location to larger drive
If the file space in the Docker Root directory is not adequate, we must relocate the Docker Root.
Procedure
To relocate the Docker root directory, complete the following steps as root or a user with “sudo all” privileges:
Stop the Docker services:
sudo systemctl stop docker sudo systemctl stop docker.socket sudo systemctl stop containerdCreate the necessary directory structure into which to move Docker root by running the following command. In this example, we will move the Docker root to
/data/docker:sudo mkdir -p /data/dockerMove the existing Docker data into the new directory structure:
sudo mv /var/lib/docker/* /data/docker/Edit the file
/etc/docker/daemon.json. If the file does not exist, create the file with the following command:sudo nano /etc/docker/daemon.jsonAdd the following content to the file and save the file:
{ "data-root": "/data/docker" }Restart the Docker services:
sudo systemctl start dockerAfter you run the command, all Docker services through dependency management will be started automatically.
Validate the new Docker root location
sudo docker info -f '{{.DockerRootDir}}'The output should be similar to the following, indicating that the Docker root directory has been successfully relocated:
/data/docker
Deploy the gitlab-runner with
sudo docker run -d --name gitlab-runner --restart always \ -v /srv/gitlab-runner/config:/etc/gitlab-runner \ -v /var/run/docker.sock:/var/run/docker.sock \ gitlab/gitlab-runner:latestVerify deployment with
sudo docker ps -a
Step 3. Create a Docker image containing a custom MATLAB installation
In order for a GitLab runner to execute MATLAB code, it needs to be able to access a container with MATLAB installed. The aim of this step is to create a Docker image with MATLAB installation that can be used by a GitLab runner. By building our own Docker image, we can specify the MATLAB version and customize the installed toolboxes.
This Dockerfile is based on MATLAB’s Dockerfile template. We will make the following modifications to this template:
- set
bashas the default run command (GitLab runners need to access a shell) - add additional MATLAB products with the flag
--products. In this example, we have added the Parallel Computing Toolbox and the Mapping Toolbox.
In your user folder on the VPS (/home/username), create a file called Dockerfile
sudo nano Dockerfileand copy the content below in the Dockerfile. This Dockerfile is based on MATLAB Dockerfile 2021.
Make sure to update the MATLAB release and installed addons to your requirements (see in bold). This example uses MATLAB r2025b and includes the Parallel Computing Toolbox and the Mapping Toolbox. For a list of available product names, see here.
To build a Docker image with the name matlab-gitlab and the version reference r2025b, run the following command in the folder containing the Dockerfile:
sudo docker build . -t matlab-gitlab:r2025bYou can verify the presence of the image with
sudo docker imagesThis image is now available locally on the VPS.
Step 4. Create the GitLab runner
You can find the required gitlab-ci token in your GitLab repository under Settings -> CI/CD -> Runners.
- Press: Create project runner
- Add a tag for the runner:
matlab. This tag will be used to call this specific runner in the CI/CD configgitlab-ci.yml. - Press: Create runner
- Under Step 1, the runner authentication token is shown. You will need this token during the next step.
Step 5. Register the MATLAB runner
After deploying the gitlab-runner in step 2, we need to register a new runner for our matlab-gitlab:r2025b image. Run the following command to register your runner and configure it to deploy in a Docker container on your server.
sudo docker run --rm -it -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register In response to this command you will be prompted to answer a series of questions:
- Enter the GitLab instance URL:
https://gitlab.tudelft.nl/ - Enter the registration token:
(from step 4) - Enter a name for the runner:
matlab-runner - Enter an executor:
docker - Enter the default Docker image:
matlab-gitlab:r2025b(name of the image created in step 3)
The runner configurations are stored in /srv/gitlab-runner/config/config.toml.
To finish the setup of the MATLAB runner, we need to modify the configuration file to add some additional Docker settings.
Open the configuration file with
sudo nano /srv/gitlab-runner/config/config.tomlupdate the following line
privileged = trueand add the following line under [runners.docker]:
pull_policy = "if-not-present"For the changes to take effect, restart the gitlab-runner with
sudo docker restart gitlab-runnerAfter registering the runner, the configuration file should contain:
config.toml
concurrent = 1
check_interval = 0
shutdown_timeout = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "matlab-gitlab"
url = "https://gitlab.tudelft.nl"
id = <runner id>
token = "<authentication token>"
executor = "docker"
[runners.custom_build_dir]
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
[runners.cache.azure]
[runners.docker]
tls_verify = false
image = "matlab-gitlab:r2025b"
privileged = true
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/cache"]
shm_size = 0
network_mtu = 0
pull_policy = "if-not-present"Step 6. Obtain a MATLAB license file
Every TU Delft employee has access to an Individual MATLAB license. Normally, you would activate MATLAB only once after installation through an online activation step. However, this does not work for a Docker container as it is relaunched for each CI trigger.
The following steps for activating MATLAB on an offline machine are adapted from the MATLAB Forum:
- Obtain your Host ID
- Obtain your computer login name or username
- Activate the license through the License Center to obtain license file
1. Obtain your Host ID
The MATLAB license can only be activated for a specific computer. In the Docker container, we will set the hostID of the container to 0242ac11ffff.
2. Obtain your computer login name or username
The MATLAB license is created for a specific user. In the Docker container, we will set the username to matlab.
3. Activate the license through the License Center to obtain license file
- Go to the License Center: https://www.mathworks.com/mwaccount
- Under My Software, click the license for Designated Computer
- Click the tab: Install and Activate
- Under related tasks, click Activate to Retrieve License File
- Enter the following information:
- the release you are activating = r2025b (same version as in the Dockerfile)
- the operating system = Linux
- the host ID = 0242ac11ffff
- the Computer Label = matlab-gitlab
- Select Continue
- Download the
license.licfile and save it to your local machine.
Step 7. Configure the CI/CD pipeline on GitLab
Before we can run a CI job, we need to configure the license in our GitLab repository. Having the license available as a GitLab variable allows us to renew it without having to change the MATLAB image.
Alternatively, we could have added the license file directly to the Docker image. With the license file in the same folder as the Dockerfile and adding the following command to the Dockerfile, we can build a Docker image with an activated MATLAB:
COPY license.lic /opt/matlab/licenses/Here, we opted to have it accessible through the GitLab settings together with the accompanying hostid.
Never share any Docker images that contain license files or other confidential information.
Under Settings -> CI/CD -> Variables, scroll down to Project variables and select Add variable.
- Type:
File - Environments:
All (default) - Visibility:
Visible(The license contains sensitive information, but its format is incompatible with themaskedsettings) - Key:
MATLAB_LICENSE - Value: Paste the content of the downloaded
license.licfile here
Step 8. Add a job to test the pipeline
To test the pipeline, add the following content to .gitlab-ci.yml via CI/CD -> Editor in your repository.
variables:
MAC_ADDRESS: 02:42:ac:11:ff:ff
check_matlab:
tags:
- matlab
# Optional : specify the docker image to use for this job if other than default in the runner config.toml
image: matlab-gitlab:r2025b
before_script:
# Change the mac-address to match the MATLAB license
- sudo ifconfig eth0 hw ether "$MAC_ADDRESS"
# Add the MATLAB license to the MATLAB installation in the container
- sudo mkdir /opt/matlab/licenses
- sudo mv ${MATLAB_LICENSE} /opt/matlab/licenses/license.lic
script:
# Run a MATLAB function/script through the -batch argument
- matlab -batch "disp('hello world!')"After committing, the pipeline should run and execute the job check_matlab. You can check the status of the pipeline via CI/CD -> Pipelines.
If all went well, you have successfully setup a GitLab runner to run MATLAB code. Congrats!
Step 9. Optional: Updating the MATLAB version
If you need to update the MATLAB version of the Docker container, you will need to go through the following steps:
- Update the MATLAB version in the Dockerfile
- Build the docker image with
sudo docker build . -t matlab-gitlab:<version> - Download a new
license.licfile for the new version (see step 5 of this guide) - Update the CI Variable
MATLAB_LICENSEwith the new license content - Update the image names (not the tags) in
.gitlab-ci.ymlto use the new image.