How to Install Mono on Ubuntu 18.04

By 

Published on

3 min read

Install Mono on Ubuntu

Mono is a platform for developing and running cross-platform applications based on the ECMA/ISO Standards. It is a free and open-source implementation of Microsoft’s .NET framework.

This tutorial explains how to install Mono on Ubuntu 18.04.

Prerequisites

The instructions assume that you are logged in as root or user with sudo privileges .

Installing Mono on Ubuntu

The easiest and the recommended way to install Mono on Ubuntu 18.04 is to install it from Mono’s repositories. It is a relatively straightforward process and will only take a few minutes.

  1. Start by installing the necessary packages:

    Terminal
    sudo apt update
    sudo apt install dirmngr gnupg apt-transport-https ca-certificates
  2. Import the repository’s GPG key using the following command:

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

    The output should look something like this:

    output
    gpg: key A6A19B38D3D831EF: public key "Xamarin Public Jenkins (auto-signing) <releng@xamarin.com>" imported
    gpg: Total number processed: 1
    gpg:               imported: 1
  3. Add the Mono repository to your system sources’ list by running the command below:

    Terminal
    sudo sh -c 'echo "deb https://download.mono-project.com/repo/ubuntu/ stable-bionic main" > /etc/apt/sources.list.d/mono-official-stable.list'
  4. Once the apt repository is enabled , update the packages list and install Mono with:

    Terminal
    sudo apt update
    sudo apt install mono-complete

    The mono-complete is a meta-package that installs the Mono runtime, development tools, and all libraries.

  5. Verify the installation by typing the following command which will print the Mono version:

    Terminal
    mono --version

    At the time of writing this article, the latest stable version of Mono is 6.6.0 Stable (6.6.0.161).

    output
    Mono JIT compiler version 6.6.0.161 (tarball Tue Dec 10 10:36:32 UTC 2019)
    Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug 
        Interpreter:   yes
        LLVM:          yes(610)
        Suspend:       hybrid
        GC:            sgen (concurrent by default)

That’s it, you have successfully installed Mono on your Ubuntu, and you can start using it.

Getting Started with Mono

To verify that everything is set up correctly, we’ll build a Hello World program that will print the classic “hello world” message.

Open your text editor and create a file named hello.cs with the following content:

hello.cscs
using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        Console.WriteLine ("Hello World!");
    }
}

Use the csc compiler to build the program:

Terminal
csc hello.cs

The command above will build an executable named hello.exe.

Run the executable using the command below:

Terminal
mono hello.exe

The output should look something like this:

output
Hello, World

If you want to execute the program only by typing its name, you’ll need to set an executable flag :

Terminal
chmod +x hello.exe

You can now run the hello.exe file by typing:

Terminal
./hello.exe

Conclusion

The latest stable Mono release packages are available for installation from the official Mono package repository.

If you hit a problem or have feedback, leave a comment below.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page