UbuntuUbuntu Based

How To Install Mono on Ubuntu 24.04 LTS

Install Mono on Ubuntu 24.04

Mono provides a powerful open-source implementation of Microsoft’s .NET Framework, enabling developers to run .NET applications on Linux systems. This comprehensive guide will walk you through installing Mono on Ubuntu 24.04 LTS using various methods, troubleshooting common issues, and setting up your development environment for .NET application development on Linux.

Understanding Mono and Its Importance

Mono serves as a cross-platform, open-source alternative to Microsoft’s .NET Framework, allowing developers to build and run .NET applications across different operating systems. Created initially by Xamarin (now part of Microsoft), Mono implements the ECMA standards for C# and the Common Language Runtime.

The significance of Mono extends beyond simple compatibility. It empowers Linux users to run Windows .NET applications without requiring a Windows installation. This cross-platform capability proves invaluable for organizations transitioning between operating systems or maintaining applications across multiple platforms.

Mono supports various application types including:

  • Console applications
  • Windows Forms applications (via WinForms implementation)
  • ASP.NET web applications
  • GTK# desktop applications
  • Mobile applications

What distinguishes Mono from alternatives like .NET Core (now .NET 5+) is its historically better support for the full .NET Framework features, making it ideal for running legacy .NET applications on Linux systems. While Microsoft has made significant progress with cross-platform support in newer .NET implementations, Mono remains relevant for specific use cases and applications requiring features not yet available in .NET 5+.

Prerequisites for Installing Mono

Before installing Mono on your Ubuntu 24.04 LTS system, ensure you meet the necessary requirements. Standard installation requires approximately 500MB of disk space, though requirements vary depending on which components you install. A minimum of 1GB RAM is recommended for comfortable development.

First, update your system packages to ensure compatibility:

sudo apt update
sudo apt upgrade -y

Next, install essential packages that support the Mono installation process:

sudo apt install -y apt-transport-https dirmngr gnupg ca-certificates

This command installs:

  • apt-transport-https: Allows package transfers over HTTPS
  • dirmngr: Manages and downloads certificates
  • gnupg: Handles cryptographic key management
  • ca-certificates: Provides common CA certificates for SSL applications

Basic familiarity with terminal commands will help you follow this tutorial effectively. If you’re new to Ubuntu, some essential commands include:

  • cd: Change directory
  • ls: List files in a directory
  • sudo: Execute commands with superuser privileges
  • apt: Ubuntu’s package management tool

With these prerequisites in place, you’re ready to install Mono using one of several methods.

Method 1: Installation via Official Mono Repository

Installing Mono from the official repository ensures you receive the latest stable release with regular updates and security patches. This method provides the most reliable installation experience for Ubuntu 24.04 LTS users.

Step 1: Installing Required Dependencies

First, ensure you have all necessary dependencies:

sudo apt install -y apt-transport-https dirmngr gnupg ca-certificates software-properties-common

The software-properties-common package enables easier management of distribution and independent software vendor repositories.

Step 2: Adding the Mono Repository GPG Key

To verify package authenticity, add the Mono Project GPG signing key:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

This imports the Mono Project’s GPG key, which ensures you’re installing authentic packages from the official repository.

Step 3: Adding the Mono Repository to Your System

Add the Mono repository to your system’s software sources:

sudo apt-add-repository 'deb https://download.mono-project.com/repo/ubuntu stable-focal main'

While this repository URL uses ‘focal’ (the codename for Ubuntu 20.04), it is compatible with Ubuntu 24.04 LTS and provides the necessary packages.

Step 4: Updating the Package Index

Update your package index to include the newly added Mono packages:

sudo apt update

Step 5: Installing the Complete Mono Package

Now install the complete Mono package:

sudo apt install -y mono-complete

The mono-complete package includes:

  • The Mono runtime
  • Development tools (compiler, etc.)
  • Libraries and frameworks
  • Documentation

For a minimal installation, you could use mono-runtime instead, but mono-complete is recommended for most users as it provides all necessary components for both running and developing .NET applications.

Verify your installation by checking the Mono version:

mono --version

Method 2: Alternative Repository Options

While the official Mono repository is recommended, alternative repositories might better suit specific configurations or provide different versions of Mono.

Using the Debian Stable Repository

To use the Debian stable repository:

# Add the signing key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

# Add the Debian repository
sudo apt-add-repository 'deb https://download.mono-project.com/repo/debian stable-buster main'

# Update package index
sudo apt update

# Install Mono
sudo apt install -y mono-complete

Compatibility Considerations

When using alternative repositories, consider:

  1. Package naming: Most packages maintain consistent naming between repositories, but verify package names before installation.
  2. Dependencies: Alternative repositories might have different dependency structures that Ubuntu’s package manager typically handles automatically.
  3. Version differences: The Debian repository might offer different Mono versions than the Ubuntu repository.
  4. Update frequency: Security updates might arrive on different schedules.

For most users, the official Mono repository remains the recommended approach, but alternative methods provide flexibility for specific requirements.

Method 3: Building Mono from Source

Building Mono from source offers maximum flexibility and control over your installation. This approach is particularly valuable for developers who need the latest features, require customization, or are contributing to Mono itself.

When to Build from Source

Consider building from source when:

  • You need the absolute latest version with cutting-edge features
  • You want to customize compilation options
  • You’re developing for or contributing to the Mono project
  • You need to apply specific patches or modifications
  • You’re experiencing compatibility issues with pre-built packages

Prerequisites for Source Compilation

Install essential build dependencies:

sudo apt install -y git autoconf automake libtool build-essential gettext cmake python3 libnunit-dev libgdiplus libx11-dev

These packages provide necessary tools for downloading, configuring, and compiling Mono source code.

Downloading the Source Code

Clone the Mono repository from GitHub:

git clone https://github.com/mono/mono.git
cd mono

By default, this clones the main development branch. For a stable release, check out a specific version tag:

git checkout tags/mono-6.12.0.182

Replace mono-6.12.0.182 with your desired version.

Compilation and Installation Steps

First, initialize the build system:

./autogen.sh --prefix=/usr/local

The --prefix parameter specifies the installation directory. /usr/local is recommended for custom-built software.

Next, compile the source code:

make -j$(nproc)

The -j$(nproc) flag parallelizes compilation across all available CPU cores.

Finally, install Mono:

sudo make install

Verifying Your Mono Installation

After installing Mono, verify that the installation was successful and is functioning correctly.

Checking Mono Version

Check the installed Mono version:

mono --version

This displays detailed information about your Mono installation, including the version number, build date, runtime environment details, and JIT compiler information.

Creating and Running a Simple Test Program

Create a simple C# program to verify compiler and runtime functionality:

1. Create a file named HelloWorld.cs:

echo 'using System;
 
public class HelloWorld
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Hello Mono World!");
        Console.WriteLine("Running on Mono version: {0}", Environment.Version);
    }
}' > HelloWorld.cs

2. Compile the program:

mcs HelloWorld.cs

3. Run the compiled program:

mono HelloWorld.exe

If everything works correctly, you’ll see output displaying “Hello Mono World!” and the Mono version.

Common Verification Commands

Additional commands to verify different aspects of your Mono installation:

Check installed Mono packages:

apt list --installed | grep mono

Verify Mono’s certificate store:

cert-sync --user /etc/ssl/certs/ca-certificates.crt

Check if Mono ASP.NET is working (if installed):

xsp4 --version

Verify GAC (Global Assembly Cache):

gacutil -l

Installing MonoDevelop IDE

MonoDevelop is a powerful, cross-platform Integrated Development Environment (IDE) designed for Mono and .NET development. Installing it alongside Mono provides a comprehensive development environment with features like code completion, integrated debugging, and visual designers.

What is MonoDevelop

MonoDevelop offers numerous features for .NET developers on Linux:

  • Advanced code editor with syntax highlighting and code completion
  • Integrated debugger for Mono applications
  • Project and solution management compatible with Visual Studio
  • Version control integration (Git, Subversion)
  • Visual designers for GTK# and other UI frameworks
  • NuGet package management
  • Unit testing framework integration

Installation Steps

Install MonoDevelop through the official repository:

# Ensure you have the Mono repository added (if not already done)
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
sudo apt-add-repository 'deb https://download.mono-project.com/repo/ubuntu stable-focal main'
sudo apt update

# Install MonoDevelop
sudo apt install -y monodevelop

Alternatively, install through flatpak for a more recent version:

# Install flatpak if not already installed
sudo apt install -y flatpak
sudo flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo

# Install MonoDevelop
flatpak install flathub com.xamarin.MonoDevelop

Basic Configuration Settings

After installation, consider these basic configuration steps:

1. Set default project location:

  • Open MonoDevelop
  • Go to Edit > Preferences > Projects > Locations
  • Set your preferred project directory

2. Configure external tools:

  • Go to Tools > External Tools
  • Add Git, terminal, or other frequently used tools

3. Install additional add-ins:

  • Go to Tools > Add-in Manager
  • Browse available add-ins for additional functionality

4. Customize code formatting:

  • Go to Edit > Preferences > Source Code > Code Formatting > C#
  • Adjust to your preferred coding style

Troubleshooting Common Installation Issues

Even with careful installation, you might encounter issues when setting up Mono. This section addresses common problems and their solutions.

Repository Connection Problems

Issue: Unable to connect to the Mono repository with errors like:

Failed to fetch https://download.mono-project.com/repo/ubuntu/dists/stable-focal/main/binary-amd64/Packages

Solutions:

1. Verify internet connectivity:

ping download.mono-project.com

2. Check if the repository service is down:
Visit the Mono Project status page or try accessing the repository URL in a browser.

3. Use an alternative DNS server:

sudo systemd-resolve --set-dns=1.1.1.1 --interface=your_interface

Replace your_interface with your network interface (e.g., eth0, wlan0).

4. Configure proxy settings if you’re behind a proxy:

export http_proxy=http://proxy_address:port
export https_proxy=http://proxy_address:port

Missing Dependency Issues

Issue: Installation fails with dependency errors like:

The following packages have unmet dependencies: mono-complete : Depends: mono-runtime but it is not going to be installed

Solutions:

1. Update package information:

sudo apt update

2. Install specific dependencies first:

sudo apt install -y mono-runtime

3. Use apt’s fix-broken option:

sudo apt --fix-broken install

4. Force architecture if necessary:

sudo apt install -y mono-complete:amd64

Common Error Messages and Their Solutions

Error: “Certificate validation failed”
Solution: Update the certificate store:

sudo cert-sync /etc/ssl/certs/ca-certificates.crt

Error: “Assembly not found” when running Mono applications
Solution: Install the necessary libraries:

sudo apt install -y libmono-system-runtime4.0-cil

Error: “The operation was canceled” during package installation
Solution: Clear APT cache and retry:

sudo apt clean
sudo apt update
sudo apt install -y mono-complete

Issues Specific to Ubuntu 24.04

Ubuntu 24.04 LTS might have specific compatibility issues due to newer libraries and dependencies:

1. LibICU version incompatibility:

sudo apt install -y libicu70

2. Mono application crashes with SIGABRT:

export MONO_ENV_OPTIONS=--arch=64

Add this to your ~/.bashrc file for persistence.

3. Missing Mono GAC directories:

sudo mkdir -p /usr/lib/mono/gac
sudo chown -R root:root /usr/lib/mono/gac

Creating Your First Mono Project

Let’s put your Mono installation to practical use by creating a simple C# application.

Creating a Simple C# “Hello World” Application

1. Create a new directory for your project:

mkdir ~/MonoFirstProject
cd ~/MonoFirstProject

2. Create a file named Program.cs:

nano Program.cs

3. Add the following C# code:

using System;

namespace MonoFirstProject
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello from Mono on Ubuntu 24.04 LTS!");
            
            // Get and display system information
            Console.WriteLine($"Running on: {Environment.OSVersion}");
            Console.WriteLine($".NET Version: {Environment.Version}");
            Console.WriteLine($"Machine Name: {Environment.MachineName}");
            
            // Simple user interaction
            Console.Write("Enter your name: ");
            string name = Console.ReadLine();
            Console.WriteLine($"Hello, {name}! Welcome to Mono development.");
        }
    }
}

Compiling the Program with Mono

Compile your C# code using the Mono C# compiler:

mcs Program.cs

If compilation succeeds, a file named Program.exe will be created in the current directory.

Running the Application

Run your compiled application:

mono Program.exe

You should see output displaying system information and a prompt to enter your name.

Basic Debugging Techniques

If you encounter issues with your application, use these debugging options:

1. Enable trace debugging:

MONO_LOG_LEVEL=debug mono Program.exe

2. Use the Mono debugger:

mdb Program.exe

3. Debug with MonoDevelop if you installed it, which provides a more user-friendly debugging experience.

Advanced Configuration Options

As you become more comfortable with Mono, you may want to fine-tune your development environment.

Environment Variables for Mono

Customize Mono’s behavior through various environment variables:

1. MONO_PATH: Sets additional directories to search for assemblies:

export MONO_PATH=/path/to/assemblies:/another/path

2. MONO_CONFIG: Specifies a custom configuration file:

export MONO_CONFIG=/path/to/mono.config

3. MONO_DEBUG: Enables specific debugging features:

export MONO_DEBUG=explicit-null-checks

Add these to your ~/.bashrc file for persistence:

echo 'export MONO_PATH=/path/to/assemblies' >> ~/.bashrc
source ~/.bashrc

Performance Tuning

Optimize Mono’s performance with these configurations:

1. Garbage collector settings:

export MONO_GC_PARAMS=nursery-size=64m

2. JIT optimization level:

export MONO_OPTS=--optimize=all

3. Assembly precompilation:

mono --aot Program.exe

4. Thread pool configuration:

export MONO_THREADS_PER_CPU=5

Multi-Version Installations

Sometimes you need multiple Mono versions on the same system:

1. Install different versions in separate prefixes when compiling from source:

./autogen.sh --prefix=/opt/mono-6.12

2. Switch between versions using symbolic links:

sudo ln -sf /opt/mono-6.12/bin/mono /usr/local/bin/mono

3. Create version-specific startup scripts:

#!/bin/bash
export PATH=/opt/mono-6.12/bin:$PATH
export LD_LIBRARY_PATH=/opt/mono-6.12/lib:$LD_LIBRARY_PATH
exec "$@"

Congratulations! You have successfully installed Mono. Thanks for using this tutorial for installing Mono on Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the official Mono website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button