Installing .NET MAUI on macOS
Tags: dotnet
Published: 2021-12-30 02:15:59

Visual Studio 2022 takes on a lot of the effort for setting up your cross-platform development environment. So what do you do when you don't have VS22? In this post I'll cover how you can still get your environment setup for [.NET MAUI](https://github.com/dotnet/maui) development.

### Pre-requisites

* Xcode 13 [latest version](https://xcodereleases.com/) - available in the Mac App Store and from [https://developer.apple.com](https://developer.apple.com)
* [Android Studio](https://developer.android.com/studio) 
  * Latest Android SDK - once installed, make sure you have the latest SDK from the SDK Manager
  * Android Emulator (optional) - once installed, create and start an Android emulator. Optionally, you can use an Android device that is configured for development
* [OpenJDK 11 from Microsoft](https://docs.microsoft.com/en-us/java/openjdk/download#openjdk-11)
* [VS Code](https://code.visualstudio.com/) (optional) - you'll want something to edit your code
  * [Omnisharp extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp) for C# intellisense

## Install .NET 6 with .NET MAUI

1. Download and run the .NET 6 installer from [dot.net](https://dot.net). 
2. Open Terminal or your favorite command line app and check that you're ready to install .NET MAUI.

```
> dotnet --version
```

**Option A)** Install .NET MAUI using workload install command:

```
> sudo dotnet workload install maui
```

This command will get you the latest released version of .NET MAUI plus the platform SDKs for Android, iOS, macOS, and Windows. 

**Option B)** Pass in additional parameters to the same command in order to get a specific branch build. 

```
> sudo dotnet workload install maui --from-rollback-file https://aka.ms/dotnet/maui/preview.11.json --source https://aka.ms/dotnet6/nuget/index.json --source https://api.nuget.org/v3/index.json
```

Your app will use the newest version available on your system. If you wish to pin your project to a specific version of .NET MAUI such as preview 11, add this to your ".csproj":

```
<MauiVersion>6.0.101-preview.11.2349</MauiVersion>
```

> These commands can be found in the contributor's [development guide on dotnet/maui](https://github.com/dotnet/maui/blob/main/.github/DEVELOPMENT.md)

That's it! You now have the building blocks to create, build, and run a .NET MAUI app. Let's do that.

## New App

To create a new app run:

```
> dotnet new maui -n "MyMauiApp"
```

CD into the "MyMauiApp" directory and run the app:

```
> dotnet build -t:Run -f net6.0-maccatalyst
```


![Xcode Devices and Simulators](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7mi23lod2z06m0j85di1.png)

This will restore the project dependencies, compile the app, and launch it. The ``-f`` parameter is the "target framework moniker". Options include:

* net6.0-android
* net6.0-ios
* net6.0-maccatalyst

> To run Windows on Windows you need Visual Studio 2022. 

### Targeting iOS

In order to target an iOS emulator, you need to provide the device id (UUID). Open Xcode and then go to Windows > Devices and Simulators. Right-click the simulator you want to use and copy the "Device Identifier".

![app running on macOS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vezefpi5aev3oziqk2ef.png)

Now append the value to the parameter ``-p:_DeviceName=:v2:uuid=``:

```
> dotnet build -t:Run -f net6.0-ios -p:_DeviceName=:v2:udid=02C556DA-64B8-440B-8F06-F8C56BB7CC22
```

### Targeting Android

Before building the Android app, start an emulator or connect to a device using ``adb connect``. Then you can run:

```
> dotnet build -t:Run -f net6.0-android
```

![app running on Android](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k98ye6rqfejcvxz2s7r1.png)

> **Troubleshooting tip:** if you get an error indicating you need to set the JavaSdkDirectory in Visual Studio, try adding the following to a file "Directory.Build.props".

```
<Project>
    <PropertyGroup>
        <JavaSdkDirectory Condition="'$(JavaSdkDirectory)'=='' and '$(JAVA_HOME_8_X64)'!=''">$(JAVA_HOME_8_X64)</JavaSdkDirectory>
    </PropertyGroup>
</Project>
```

## Conclusion

Now you are up and running with .NET MAUI for building Android, iOS, and macOS apps on macOS. This doesn't provide you with modern productivity features like C# and XAML hot reload. Those come standard with Visual Studio 2022 (and the upcoming Mac version), or you can navigate your way through configuring any number of other hot reload options available such as [LiveSharp](https://www.livesharp.net/) or [Reloadify3000](https://github.com/Clancey/Reloadify3000). Personally, I'll be using [Comet](https://github.com/dotnet/Comet/) which rides atop .NET MAUI and is built around hot reload.