Mastering .NET Installations with dotnet-install.sh - A Comprehensive Guide

What is dotnet-install.sh?

The dotnet-install.sh script is a powerful, feature-rich tool provided by Microsoft for installing .NET Core SDKs and runtimes across Linux and macOS environments. It’s designed for flexibility and automation, making it ideal for CI/CD pipelines and environments where traditional package managers might not be the best fit.

Key Features of dotnet-install.sh

Platform Detection

get_current_os_name() {
    local uname=$(uname)
    if [ "$uname" = "Darwin" ]; then
        echo "osx"
        return 0
    elif [ "$uname" = "FreeBSD" ]; then
        echo "freebsd"
        return 0
    elif [ "$uname" = "Linux" ]; then
        # Detect specific Linux distributions
        local linux_platform_name=""
        linux_platform_name="$(get_linux_platform_name)" || true
        
        # Handle different scenarios like RHEL 6, musl-based distros
        # ...
    fi
}

The script automatically detects your operating system and architecture, ensuring you get the right binaries for your environment.

Flexible Installation Options

  • Version Control: Install specific versions, latest releases, or channel-based releases (LTS/STS)
  • Runtime Selection: Choose between SDK, .NET Runtime, or ASP.NET Core Runtime
  • Quality Tiers: Select from daily, signed, validated, or preview builds
  • Architecture Support: x64, arm, arm64, s390x, ppc64le, and loongarch64

Headless Operation

# No user interaction needed
./dotnet-install.sh --version 8.0.101 --install-dir ~/dotnet

The script is designed for non-interactive environments, making it perfect for automation.

No Administrator Rights Required

Unlike package managers that often require sudo privileges, dotnet-install.sh installs to user directories by default.

Multiple Version Support

The script allows multiple versions to coexist, enabling testing across different .NET versions.

How It Works

Version Resolution

The script has a sophisticated version resolution system that:

  1. Checks for explicit versions specified by command line
  2. Resolves “latest” using channel information
  3. Reads from global.json if specified
  4. Falls back to LTS versions when no version is specified

Download Process

The script uses a multi-tiered approach to downloads:

generate_download_links() {
    # Try aka.ms links first
    generate_akams_links || return

    # Fall back to other feeds if needed
    if [[ "${#download_links[@]}" -lt 1 ]]; then
        for feed in ${feeds[@]}
        do
            generate_regular_links $feed || return
        done
    fi
}
  1. First attempts to use Microsoft’s aka.ms URL shortening service
  2. Falls back to direct Azure feeds if necessary
  3. Supports both curl and wget for downloads
  4. Validates downloaded packages to ensure integrity

Installation

After downloading, the script:

  1. Extracts the package to the specified installation directory
  2. Preserves existing non-versioned files if requested
  3. Verifies that the installed version matches the expected version
  4. Updates PATH if requested

Common Usage Examples

Install Latest LTS SDK

./dotnet-install.sh

Install Specific SDK Version

./dotnet-install.sh --version 8.0.101

Install ASP.NET Core Runtime Only

./dotnet-install.sh --runtime aspnetcore

Install to Custom Location

./dotnet-install.sh --install-dir /opt/dotnet

Install Preview Version

./dotnet-install.sh --channel 9.0 --quality preview

Dry Run (Display Download URLs)

./dotnet-install.sh --version 8.0.100 --dry-run

Integration with DevOps Workflows

Docker Containerization

FROM ubuntu:22.04
WORKDIR /app

# Download and run the installer
ADD https://dot.net/v1/dotnet-install.sh ./dotnet-install.sh
RUN chmod +x ./dotnet-install.sh && \
    ./dotnet-install.sh --version 8.0.101 && \
    ln -sf /root/.dotnet/dotnet /usr/bin/dotnet

# Build and run application
COPY . .
RUN dotnet publish -c Release -o out
ENTRYPOINT ["dotnet", "out/MyApp.dll"]

CI/CD Pipelines

# GitHub Actions workflow example
steps:
  - name: Setup .NET
    run: |
      curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin --version 8.0.101
      echo "$HOME/.dotnet" >> $GITHUB_PATH

Advanced Scenarios

Air-gapped Environments

For environments without internet access:

  1. Download the script and desired .NET packages on a connected machine
  2. Transfer to the air-gapped environment
  3. Use --zip-path to specify the pre-downloaded archive

Version Management

Use the script alongside the .NET CLI’s global.json support:

# Create a global.json with specific SDK version
dotnet new globaljson --sdk-version 8.0.101

# Install the version specified in global.json
./dotnet-install.sh --jsonfile global.json

Conclusion

The dotnet-install.sh script is a testament to Microsoft’s commitment to flexibility and cross-platform support for .NET developers. By understanding how to use this powerful tool, you can create more reliable build processes, consistent development environments, and streamlined deployment pipelines across diverse environments.

Whether you’re managing CI/CD pipelines, containerizing applications, or need specific versions for development, dotnet-install.sh provides the control and flexibility needed for modern .NET development workflows.