Created
August 31, 2025 06:15
-
-
Save sycomix/3e5cb07a05b084c2717e5afcc0a456ce to your computer and use it in GitHub Desktop.
This script guides you through the process of building PyTorch from source on Windows with libuv support. It avoids common permissions errors by building libuv to a local project directory instead of a system folder.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This script guides you through the process of building PyTorch from source on Windows | |
| # with libuv support. It avoids common permissions errors by building libuv to a local | |
| # project directory instead of a system folder. | |
| # | |
| # Prerequisite: | |
| # - Visual Studio Build Tools (2019 or later) with "Desktop development with C++" and "Python development" workloads. | |
| # - Git | |
| # - A clean Python environment (e.g., from Miniconda) | |
| # - (Optional) CUDA Toolkit for GPU support. | |
| # Stop on errors | |
| $ErrorActionPreference = "Stop" | |
| # --- Script Configuration --- | |
| $project_dir = (Get-Location).Path | |
| $pytorch_dir = "$project_dir\pytorch_build" | |
| $libuv_dir = "$project_dir\libuv_build" | |
| $libuv_install_dir = "$project_dir\libuv_install" | |
| $cuda_version = "12.6" # Set to "none" for a CPU-only build | |
| $python_version = "3.11" # Your Python version | |
| Write-Host "--- Starting PyTorch with Libuv Build Process ---" -ForegroundColor Green | |
| Write-Host "This script will build libuv to a local directory: '$libuv_install_dir'." -ForegroundColor Yellow | |
| Write-Host "This avoids permissions errors with 'Program Files' and is the recommended approach." -ForegroundColor Yellow | |
| Write-Host "----------------------------------------------------" | |
| # --- Step 1: Install Python Dependencies for PyTorch --- | |
| Write-Host "Step 1: Installing required Python packages..." -ForegroundColor Cyan | |
| try { | |
| pip install -r https://raw.githubusercontent.com/pytorch/pytorch/main/requirements.txt | |
| pip install numpy ninja cmake cffi | |
| } | |
| catch { | |
| Write-Host "Error installing Python packages. Please ensure pip is in your PATH." -ForegroundColor Red | |
| exit | |
| } | |
| Write-Host "Python packages installed successfully." -ForegroundColor Green | |
| # --- Step 2: Clone PyTorch and Libuv Repositories --- | |
| Write-Host "Step 2: Cloning PyTorch and Libuv repositories..." -ForegroundColor Cyan | |
| try { | |
| if (-not (Test-Path -Path $pytorch_dir)) { | |
| git clone https://github.com/pytorch/pytorch.git $pytorch_dir | |
| } else { | |
| Write-Host "PyTorch directory already exists. Skipping clone." -ForegroundColor Yellow | |
| } | |
| if (-not (Test-Path -Path $libuv_dir)) { | |
| git clone https://github.com/libuv/libuv.git $libuv_dir | |
| } else { | |
| Write-Host "Libuv directory already exists. Skipping clone." -ForegroundColor Yellow | |
| } | |
| } | |
| catch { | |
| Write-Host "Error cloning repositories. Please ensure Git is in your PATH." -ForegroundColor Red | |
| exit | |
| } | |
| Write-Host "Repositories cloned successfully." -ForegroundColor Green | |
| # --- Step 3: Build and Install Libuv to a Local Directory --- | |
| Write-Host "Step 3: Building and installing Libuv from source..." -ForegroundColor Cyan | |
| try { | |
| cd $libuv_dir | |
| if (-not (Test-Path -Path "build")) { | |
| mkdir build | |
| } | |
| cd build | |
| # Configure cmake to install to our local, non-protected directory. | |
| if (-not (Test-Path -Path $libuv_install_dir)) { | |
| mkdir $libuv_install_dir | |
| } | |
| Write-Host "Setting libuv install prefix to: '$libuv_install_dir'" | |
| cmake .. -DCMAKE_INSTALL_PREFIX="$libuv_install_dir" | |
| # Now build and install | |
| Write-Host "Building libuv..." | |
| cmake --build . --config Release | |
| Write-Host "Installing libuv..." | |
| cmake --install . --config Release | |
| cd .. | |
| cd .. | |
| } | |
| catch { | |
| Write-Host "Error building libuv. This is a critical step. Please check the error output and verify your Visual Studio installation." -ForegroundColor Red | |
| exit | |
| } | |
| Write-Host "Libuv built and installed successfully to '$libuv_install_dir'." -ForegroundColor Green | |
| # --- Step 4: Configure and Build PyTorch with Libuv --- | |
| Write-Host "Step 4: Configuring and building PyTorch with Libuv support..." -ForegroundColor Cyan | |
| try { | |
| cd $pytorch_dir | |
| # Set environment variables for the build | |
| $env:USE_LIBUV = "1" | |
| $env:CMAKE_GENERATOR = "Ninja" | |
| $env:USE_NINJA = "ON" | |
| # Set to "0" for a CPU-only build, "1" for CUDA | |
| $env:USE_CUDA = "1" | |
| $env:USE_MKLDNN = "ON" | |
| # Point to the new local libuv installation path. | |
| Write-Host "Set LIBUV_ROOT to: '$libuv_install_dir'" | |
| $env:LIBUV_ROOT = $libuv_install_dir | |
| # Add the libuv DLLs to the PATH so PyTorch can find them. | |
| $env:PATH += ";$libuv_install_dir\bin" | |
| Write-Host "Updated PATH with libuv bin directory." | |
| # Run the setup.py to build and install PyTorch. | |
| Write-Host "Running python setup.py develop. This will take a long time." | |
| C:\Users\sycom\miniconda3\python.exe setup.py develop | |
| } | |
| catch { | |
| Write-Host "Error during PyTorch build. This is a complex process with many potential failure points." -ForegroundColor Red | |
| Write-Host "Common issues: missing Visual Studio components, incorrect environment variables, or dependency conflicts." -ForegroundColor Red | |
| Write-Host "Please review the output and consult the official PyTorch build documentation for Windows." -ForegroundColor Red | |
| exit | |
| } | |
| Write-Host "--- PyTorch Build Complete ---" -ForegroundColor Green | |
| Write-Host "PyTorch has been built from source with libuv support." -ForegroundColor Green | |
| Write-Host "You should now be able to run your distributed scripts with 'use_libuv=True'." -ForegroundColor Green | |
| Write-Host "----------------------------------------------------" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment