Search

How to Install C# on Ubuntu 22.04 and Getting Started

C Sharp is a strong-typed, multi-paradigm programming language developed by Microsoft as part of the .NET platform.


How to Install C# on Ubuntu 22.04 and Getting Started


C# is a strong-typed, multi-paradigm programming language developed by Microsoft as part of the .NET platform.

Its syntax is object-oriented and was based on C++ but includes many influences from other programming languages, such as Object Pascal and, mainly, Java.

In this article we will see how to install on Ubuntu 22.04, however, you can adapt the instructions for any other version or even another distribution.


Installation

Before installing, register the keys:

wget https://packages.microsoft.com/config/ubuntu/21.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb

Now install the key:

Tip: execute one command at a time, if your Ubuntu version is different, find 22.04 for the version corresponding to yours. See here all available versions.

sudo apt-get install -y gpg
wget -O - https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor -o microsoft.asc.gpg
sudo mv microsoft.asc.gpg /etc/apt/trusted.gpg.d/
wget https://packages.microsoft.com/config/ubuntu/22.04/prod.list
sudo mv prod.list /etc/apt/sources.list.d/microsoft-prod.list
sudo chown root:root /etc/apt/trusted.gpg.d/microsoft.asc.gpg
sudo chown root:root /etc/apt/sources.list.d/microsoft-prod.list

Now install the SDK, runtime will also be installed automatically

sudo apt-get update; \
  sudo apt-get install -y apt-transport-https && \
  sudo apt-get update && \
  sudo apt-get install -y dotnet-sdk-6.0

You can now clean the file: packages-microsoft-prod.deb

rm packages-microsoft-prod.deb


Testing a Hello, World!

1. Create a project

dotnet new console -o MyApp

If you want to specify the version, use: dotnet new console -o MyApp -f net6.0

2. Enter the project:

cd MyApp

3. Edit the Program.cs file

Change the output as you wish, example:

Console.WriteLine("Hello, C Sharp via Ubuntu 22.04!");

Now run it at runtime:

dotnet run

Note that it will take a while to run, as it was compiled and then executed. If you want to separate tasks:


First build:

dotnet build

The likely and similar output will be:

Microsoft(R) Build Engine version 17.1.1+a02f73656 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining the projects to restore...
  All projects are up to date for restoration.
  MyApp -> /home/marcos/MyApp/bin/Debug/net6.0/MyApp.dll

Compile successfully.
    0 Warning(s)
    0 Error(s)

Elapsed Time 00:00:02.30

Now just run the binary:

./bin/Debug/net6.0/MyApp

See too:

How to Install PowerShell on Ubuntu and First Steps


Useful links


csharp ubuntu windows


Share



Comments