How to Install Dotnet on a Mac (.NET, CSharp)
In this article, I will show you how to install and use dotnet (.NET) on a Mac. This will allow you to run and even write C# (CSharp) projects.
In this article, I will show you how to install and use dotnet (.NET) on a Mac. This will allow you to run and even write C# (CSharp) projects.
Why dotnet and not .NET?
Why am I writing "dotnet" and not ".NET?" As you will see below, the name of the binary / command-line tool is "dotnet."
Step 1. Install dotnet
Before installing dotnet, see if you have it installed already by opening up a terminal window and running this command:
To install dotnet on a Mac, do the following:
dotnet --version
If dotnet is already installed, skip to the next step, otherwise:
- Browse to: https://dotnet.microsoft.com/en-us/download/dotnet
- Select the most recent version that is "active" (latest)
- Select the install for your Mac - for an Intel-based Mac, select x64, for Apple Silicon (M1, etc) choose Arm64
This will download the installer (*.pkg) file.
- Once the download is finished, run the installer
- When the installer is done, close the terminal window and open a new one
- Try running the dotnet command again to verify that it was installed:
dotnet --version
Step 2. Create a project
To create a new dotnet project, open up a terminal window and do the following:
- Create the project folder (if any subfolder doesn't exist it will be created):
mkdir -p ~/projects/charp/csharp-demo
cd ~/projects/charp/csharp-demo
- Create a project using dotnet by running this command in the demo folder:
dotnet new console
- Verify the project was created:
ls -ls
- Verify you see a listing for these files and folders:
Program.cs
csharp-demo.csproj
obj
- View the contents of the *.cs file by using cat:
cat Program.cs
You should see something like this:
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
Step 3. Run the project
To run the project, do the following from the project folder:
dotnet run
- Verify that you see output like this:
Hello, World!
Step 4. Update Visual Studio Code
If you don't have Visual Studio Code installed already, see the installation link:
Once Visual Studio code is installed, from the root of your project run this command (note the dot (.) at the end):
code .
- Click on the source (*.cs) file
You should get prompted to install the C# extension:
- Click Install
You may also get another prompt saying something like "Required assets to build and debug are missing ..."
- Click Yes to install the required assets
Step 5. Install the .NET Extension Pack (optional)
You may want to also install the .NET Extension Pack (which already includes the C# extension and will skip or update it):
Step 6. Add a .gitignore file (optional)
To add a .gitignore file to your project, simply run this command:
dotnet new gitignore
To view it:
cat .gitignore
Example Project
An example of the project created in this article can be found here:
Conclusion
In this article, you learned how to:
- Install dotnet on a Mac
- Use dotnet to create a simple project
- Use dotnet to run the project
- Update Visual Studio Code to work with .NET and CSharp projects