Install Meson on Ubuntu 24.04

Install Meson on Ubuntu 24.04

Meson is a modern open-source build system designed to be fast, user-friendly, and highly portable. It is primarily used to compile and manage software projects written in C, C++, Fortran, Rust, and other languages. This tutorial explains how to install Meson on Ubuntu 24.04.

Prepare environment

Make sure you have Ninja installed, as it's the default backend used by Meson. Installation instructions can be found in the post.

You will also need essential development tools such as gcc or g++ compiler:

sudo apt install -y gcc
sudo apt install -y g++

Install Meson

First, fetch the latest Meson release version number:

MESON_VERSION=$(curl -s "https://api.github.com/repos/mesonbuild/meson/releases/latest" | grep -Po '"tag_name": "\K[0-9.]+')

Download the corresponding archive:

wget -qO meson.tar.gz https://github.com/mesonbuild/meson/releases/latest/download/meson-${MESON_VERSION}.tar.gz

Create a directory for Meson and extract the files:

sudo mkdir /opt/meson
sudo tar xf meson.tar.gz --strip-components=1 -C /opt/meson

Move the main script to a more typical executable name:

sudo mv /opt/meson/meson.py /opt/meson/meson

Add Meson to your system PATH so it's available system-wide:

echo 'export PATH=$PATH:/opt/meson' | sudo tee -a /etc/profile.d/meson.sh

To apply the changes, you can either log out and log back in, or run the following command to activate them right away:

source /etc/profile

Verify the installation by checking Meson version:

meson --version

Finally, clean up the downloaded archive:

rm -rf meson.tar.gz

Testing Meson

Let's create a simple project to verify that Meson is working correctly. Create a new directory for the project and navigate into it:

mkdir helloworld && cd helloworld

Create the C source file:

nano main.c

Add the following code to main.c:

helloworld/main.c

#include <stdio.h>

int main() {
    printf("Hello world\n");

    return 0;
}

Next, create the Meson build definition file:

nano meson.build

Add the following content to meson.build:

helloworld/meson.build

project('hello', 'c')

executable('hello', 'main.c')

Now, configure the project in the build directory using Meson:

meson setup build

Compile the project:

meson compile -C build

Run the compiled executable:

./build/hello

Uninstall Meson

To fully uninstall Meson, just delete its installation directory:

sudo rm -rf /opt/meson

Remove the meson.sh file that was used to configure the environment variable:

sudo rm -rf /etc/profile.d/meson.sh

Leave a Comment

Cancel reply

Your email address will not be published.