Got it — here’s how to go from zero to running C# on Linux in a clean, beginner-friendly way.
1. Install .NET SDK
The .NET SDK is the tool you need to write and run C# code.
On most Linux distros:
Ubuntu/Debian:
sudo apt update
sudo apt install dotnet-sdk-8.0
(Replace 8.0 with the latest LTS version if needed — currently .NET 8.)
If your distro's repo is outdated, use Microsoft's official package repo:
sudo apt update && sudo apt install -y wget apt-transport-https
wget https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt update
sudo apt install dotnet-sdk-8.0
2. Verify Installation
dotnet --version
You should see something like:
8.0.100
3. Create Your First C# Project
From your terminal:
mkdir MyFirstCSharpApp
cd MyFirstCSharpApp
dotnet new console
This creates:
Program.cs
→ your C# codeMyFirstCSharpApp.csproj
→ project file
4. Run the Program
dotnet run
You'll see:
Hello, World!
5. Edit the Code
Open Program.cs
in a text editor (like nano, vim, or VS Code):
using System;
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Usage: mycli <command> [options]");
return;
}
string command = args[0].ToLower();
switch (command)
{
case "greet":
string name = args.Length > 1 ? args[1] : "World";
Console.WriteLine($"Hello, {name}!");
break;
case "time":
Console.WriteLine($"Current time: {DateTime.Now}");
break;
default:
Console.WriteLine("Unknown command.");
break;
}
}
}
Save the file, then run:
dotnet run
Now Create linux command of this:
cd MyFirstCSharpApp
dotnet publish -c Release -r linux-x64 --self-contained true /p:PublishSingleFile=true -o publish
sudo cp publish/MyFirstCSharpApp /usr/local/bin/mycli
sudo chmod +x /usr/local/bin/mycli
Test Command:
mycli greet Alice
No comments:
Post a Comment