.NET環境構築
.NET Core/5/6/7/8の開発環境を構築します。複数のバージョン管理方法も含めて説明します。
基本インストール
Section titled “基本インストール”Homebrewを使用したインストール
Section titled “Homebrewを使用したインストール”brew install --cask dotnet-sdkインストール確認
Section titled “インストール確認”# バージョン確認dotnet --version
# インストール済みSDK一覧dotnet --list-sdks基本的な使い方
Section titled “基本的な使い方”コンソールアプリケーションの作成
Section titled “コンソールアプリケーションの作成”# .NET 8で作成dotnet new console -o MyApp
# 特定のバージョンを指定dotnet new console -o MyApp -f net7.0Web APIプロジェクトの作成
Section titled “Web APIプロジェクトの作成”dotnet new webapi -o SampleServiceプロジェクトの実行
Section titled “プロジェクトの実行”# 依存関係の復元dotnet restore
# アプリケーションの実行dotnet run
# ビルドのみdotnet build複数バージョンの管理(Homebrew方式)
Section titled “複数バージョンの管理(Homebrew方式)”dotnet-sdk-versions tapの追加
Section titled “dotnet-sdk-versions tapの追加”brew tap isen-ng/dotnet-sdk-versions特定バージョンのインストール
Section titled “特定バージョンのインストール”# .NET 6.0brew install --cask dotnet-sdk6-0-400
# .NET 7.0brew install --cask dotnet-sdk7-0-201
# .NET 8.0brew install --cask dotnet-sdk8-0-100brew install --cask dotnet-sdk8-0-200バージョンの切り替え
Section titled “バージョンの切り替え”一度アンインストールしてから、必要なバージョンをインストール:
# アンインストールbrew uninstall --zap --cask dotnet-sdk8-0-100
# 別バージョンをインストールbrew install --cask dotnet-sdk6-0-400miseを使用したバージョン管理(推奨)
Section titled “miseを使用したバージョン管理(推奨)”miseを使用すると、より簡単にバージョンを切り替えられます:
# .NET 8をグローバルに設定mise use --global dotnet@8
# プロジェクトで.NET 6を使用mise use dotnet@6グローバルツール
Section titled “グローバルツール”Entity Framework Core
Section titled “Entity Framework Core”# 特定バージョンをインストールdotnet tool install --global dotnet-ef --version 8.0.1
# アンインストールdotnet tool uninstall --global dotnet-ef
# バージョン確認dotnet ef --versionカバレッジレポート生成ツール
Section titled “カバレッジレポート生成ツール”# ReportGeneratorのインストールdotnet tool install -g dotnet-reportgenerator-globaltoolプロジェクトテンプレート
Section titled “プロジェクトテンプレート”利用可能なテンプレート一覧
Section titled “利用可能なテンプレート一覧”dotnet new listよく使うテンプレート
Section titled “よく使うテンプレート”# MVC Webアプリケーションdotnet new mvc -o MyMvcApp
# Blazor Serverアプリdotnet new blazorserver -o MyBlazorApp
# クラスライブラリdotnet new classlib -o MyLibrary
# xUnitテストプロジェクトdotnet new xunit -o MyTestsglobal.jsonによるバージョン固定
Section titled “global.jsonによるバージョン固定”プロジェクトで使用する.NET SDKバージョンを固定:
# global.jsonの作成dotnet new globaljson --sdk-version 8.0.100global.jsonの内容:
{ "sdk": { "version": "8.0.100" }}トラブルシューティング
Section titled “トラブルシューティング”SDKが見つからない場合
Section titled “SDKが見つからない場合”dotnet --list-sdksでインストール済みSDKを確認global.jsonが存在する場合、指定バージョンを確認- 必要なバージョンをインストール
# .NETのインストール先を確認which dotnet
# 環境変数を確認echo $PATH