Using .NET 5 with VS Code on Linux.
Tags: dotnet, linux, tutorial, vscode
Published: 2020-06-25 15:21:57

A simple and quick approach to use .NET 5 Preview with VS Code on Linux.

> Before install, it's important to check .NET 5 - [Supported OS  versions](https://github.com/dotnet/core/blob/master/release-notes/5.0/5.0-supported-os.md#net-5---supported-os-versions)

#Installing

1 - Create a directory to use for the download location and change it into that directory, for example:

```bash
mkdir $HOME/dotnet_install && cd $HOME/dotnet_install
```
2 - Download the shell script with instructions:

```bash
curl -H 'Cache-Control: no-cache' -L https://aka.ms/install-dotnet-preview 
-o install-dotnet-preview.sh
```

3 - Run the script to begin the install:

```bash
sudo bash install-dotnet-preview.sh
```

The last step will download and unzip all packages and dependencies into `dotnet_packages/` and then start the installation.

But if the environment is not "clean", it is possible that packages conflicts happen. In this case, the script will stop the execution, then we can do the installations manually, solving or replacing dependencies with conflicts.

```bash
cd dotnet_install

ls -l
  dotnet-5.0.0-preview.5-rpm.tar.gz  
  dotnet_packages/  
  install-dotnet-preview.sh
```

Use a package manager of your preference, and install all the SDKs, Runtimes, and dependencies packages. In this example, we decided to replace files:

```bash
cd dotnet_packages

ls -l
  aspnetcore-runtime-5.0.0-preview.5.20279.2-x64.rpm
  aspnetcore-targeting-pack-5.0.0-preview.5.20279.2.rpm
  dotnet-apphost-pack-5.0.0-preview.5.20278.1-x64.rpm
  dotnet-host-5.0.0-preview.5.20278.1-x64.rpm
  dotnet-hostfxr-5.0.0-preview.5.20278.1-x64.rpm
  dotnet-runtime-5.0.0-preview.5.20278.1-x64.rpm
  dotnet-runtime-deps.rpm
  dotnet-sdk-5.0.100-preview.5.20279.10-x64.rpm
  dotnet-targeting-pack-5.0.0-preview.5.20278.1-x64.rpm
  netstandard-targeting-pack-2.1.0-x64.rpm

sudo rpm -i --replacefiles netstandard-targeting-pack-2.1.0-x64.rpm
```
It's important to maintain the default **CLI** alias like `dotnet` because **VS Code** will use this way to create and run applications.

###OR

If you prefer a more **simple setup**, you can install it using **[Snap](https://github.com/dotnet/core/blob/master/release-notes/5.0/preview/5.0.0-preview.5-install-instructions.md#install-using-snap)**. But, after install, is necessary to change alias from CLI:

`sudo snap alias dotnet-sdk.dotnet dotnet`

[IMPORTANT] If the **[dotnet CLI](https://docs.microsoft.com/pt-br/dotnet/core/tools/)** already installed, it's not recommended to use **Snap** installation, because it's occurrence in an *isolated environment* and it's not possible to have double alias like `dotnet`.

> *For more instructions or ways to install SDK/Runtime, use [this documentation](https://github.com/dotnet/core/blob/master/release-notes/5.0/preview/5.0.0-preview.5-install-instructions.md).*

#VS Code settings

To enjoy new features from **C# 9** is necessary to install or update **VS Code** for [version 1.46](https://code.visualstudio.com/updates/v1_46).

Some recommended extensions: [C#](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp) and [C# Extensions](https://marketplace.visualstudio.com/items?itemName=jchannon.csharpextensions).

#Projects settings

Now, we can build applications using preview version from .NET 5 just indicating on **C Sharp Project** (*.CSPROJ) file the respective versions from framework and language:

```XML
<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>net5.0</TargetFramework>
        <LangVersion>preview</LangVersion>
    </PropertyGroup>

</Project>
```

#Enjoy!

{% github AntonioFalcao/AntonioFalcao %}