In some scenarios, you may need to configure the .NET CLI to use a specific version of the .NET SDK, especially when working on multiple projects that require different SDK versions. The .NET CLI allows you to specify the SDK version to use for a particular project by utilizing a global.json file. This guide will walk you through the steps to configure the CLI to use a specific version of the .NET SDK.

What is global.json?

The global.json file is a configuration file that allows you to define the SDK version that the .NET CLI should use when running commands in a specific directory or project. By creating this file in your project directory, you can ensure that the correct SDK version is used, regardless of the versions installed on your machine.

How to Create and Configure global.json

Follow these steps to create and configure a global.json file for your project:

Step 1: Open Your Command Line Interface

Open your terminal (macOS/Linux) or command prompt (Windows).

Step 2: Navigate to Your Project Directory

Use the cd command to navigate to the directory of your project. For example:

cd /path/to/your/project

Step 3: Create the global.json File

You can create the global.json file using a text editor or directly from the command line. To create the file from the command line, you can use the following command:

echo { "sdk": { "version": "5.0.400" } } > global.json

In this example, replace 5.0.400 with the specific version of the .NET SDK you want to use for your project.

Step 4: Verify the Configuration

After creating the global.json file, you can verify that the CLI is using the specified SDK version by running the following command:

dotnet --version

This command should output the version specified in your global.json file. If the specified version is not installed, the CLI will display an error message indicating that the version is not available.

Example of a global.json File

Here is an example of what a global.json file might look like:

{
"sdk": {
"version": "5.0.400",
"rollForward": "latestMinor"
}
}

In this example, the rollForward option is set to latestMinor, which allows the CLI to use the latest minor version of the specified SDK if the exact version is not available.

Conclusion

Configuring the .NET CLI to use a specific version of the .NET SDK is a straightforward process that can help you manage multiple projects with different SDK requirements. By creating a global.json file in your project directory, you can ensure that the correct SDK version is used, providing consistency and reducing potential issues. Understanding how to use the global.json file is essential for any developer working with the .NET ecosystem.