You are currently viewing Running C sharp on Ubuntu: A Step-by-Step Guide

Running C sharp on Ubuntu: A Step-by-Step Guide

C# is a versatile programming language developed by Microsoft, widely used for developing various types of applications, including web, desktop, mobile, and cloud-based applications. In this article, we will guide you through the process of setting up and running C sharp applications on Ubuntu, a popular Linux distribution.

Prerequisites

Before you begin, ensure that you have:

  • A computer running Ubuntu.
  • Access to a terminal or command-line interface.

Steps to Run C# on Ubuntu

Step 1: Install .NET SDK

The .NET SDK (Software Development Kit) provides the necessary tools and libraries to develop C# applications on Ubuntu. Follow these steps to install the .NET SDK:

  1. Create a script that has all the commands required for the installation, or run the commands one by one. If you chose the script way, ensure to make it executable: Copy and paste the following in a file, and give it a preferred name with .sh extension. eg dotnetscript.sh
#!/bin/bash

# Update package list
sudo apt-get update

# Install the dependencies
sudo apt-get install -y apt-transport-https

# Download and install the Microsoft package signing key
wget https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb

# Install the .NET SDK
sudo apt-get update
sudo apt-get install -y dotnet-sdk-6.0
  1. Make the script executable and run it to install the required applications.
chmod +x dotnetscript.sh #where dotnetscript.sh is the name given to the file.
./dotnetscript.sh   #run the script to do the installation

2. Verify Installation:

After installing the .NET SDK, verify that the installation was successful by running the following command:

dotnet --version

This command should display the installed .NET SDK version.

Step 2: Create and Run a C# Program

After installing the .NET SDK, you can create and run a simple C# program using the following steps:

  1. Create a new directory for your C# project and navigate into it:
mkdir HelloWorldApp
cd HelloWorldApp
  1. Create a new C# console application using the .NET CLI:
dotnet new console
  1. Open the Program.cs file in a text editor and update it with the following C# code:
using System;

namespace HelloWorldApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}
  1. Save the file and run the C# program using the following command:
dotnet run

You should see the output Hello, World! displayed in the terminal, indicating that your C# program executed successfully. You can now add some advanced code depending on what application you are building.

In this article, we explored the steps to set up and run C# applications on Ubuntu. By following the steps outlined above, you can create, build, and execute C# programs on your Ubuntu system using the .NET SDK.

Make a donation to support us


Web Hosting and email hosting Packages


For web development services, SEO services, Digital marketing strategies, website set up services, web hosting and domain registration; contact Dynamic Technologies.


Related articles: