Skip to content

build native pack of Torchsharp and libtorch for arm64 ubuntu #1516

Open

Description

@abednejad

How to build native pack of Torchsharp and libtorch on arm64 ubuntu 24.04?

sudo apt update
sudo apt install -y git cmake build-essential python3 python3-pip
libopenblas-dev libomp-dev python3.12-dev python3-venv

wget https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh
chmod +x dotnet-install.sh
./dotnet-install.sh --channel 8.0
export PATH=$PATH:$HOME/.dotnet

git clone --recursive https://github.com/pytorch/pytorch.git
cd pytorch

sudo apt install python3-venv python3-full
python3 -m venv ~/pytorch-env
source ~/pytorch-env/bin/activate

=== 1. Prepare environment ===

sudo apt update

pip install --upgrade pip setuptools wheel
pip install typing_extensions numpy pyyaml
pip install -r requirements.txt

=== 2. Clone PyTorch and install deps ===

git clone --recursive https://github.com/pytorch/pytorch.git
cd pytorch
git checkout 2.7.1

pip install -r requirements.txt

Clean any old build

rm -rf build

=== 3. Build LibTorch for ARM64 ===

tmux new -s pytorch-build

python3 setup.py build | tee build.log

#if ssh closed use this for keept it

  • If SSH disconnects, reconnect and run
    tmux attach -t pytorch-build.

#for detach from tmux
Ctrl + b, then d

#Check logs if needed
tail -f ~/pytorch/build.log

Generate LibTorch dir include bin,lib,share,...

cd /pytorch/build
cmake --install . --prefix ./install

back to root

cd /

=== 4. Clone TorchSharp and integrate LibTorch ===

git clone https://github.com/dotnet/TorchSharp.git
cd TorchSharp

Copy native libs into TorchSharp NativeDependencies

mkdir -p src/NativeDependencies/arm64-linux/libtorch
cp -r /pytorch/build/install/* /TorchSharp/src/Native/NativeDependencies/arm64-linux/libtorch

cd /TorchSharp/src/Native

cmake -DCMAKE_PREFIX_PATH=/TorchSharp/src/Native/NativeDependencies/arm64-linux/libtorch -DCMAKE_BUILD_TYPE=Release

=== 5. link TorchSharp to natively built libtorch ===

how to do that?
how to do that?
I'm stock here!!!!!!!!!

=== 6. Build and pack TorchSharp NuGet ===

dotnet build -c Release
dotnet pack -c Release

I'm stock at step 5 , when going to do step 6, it download libtorch set in
/TorchSharp/src/Redist/libtorch-cpu

and got this error:
build failed with 2 error(s) (0.9s)
collect2 : error : ld returned 1 exit status
/TorchSharp/src/Native/build.proj(40,5): error MSB3073: The command ""/TorchSharp/src/Native/build.sh" --configuration Release --arch arm64 --libtorchpath /TorchSharp/bin/obj/AnyCPU.Release/libtorch-cpu/libtorch-cxx11-abi-shared-with-deps-2.7.1cpu/libtorch/share/cmake/Torch" exited with code 2.

Build failed with 2 error(s) in 4.5s

please help

Please complete the following information:

  • OS: [arm64 ubuntu 24.04
  • Package Type [e.g. torchsharp-cpu-linux, torchsharp-cuda-linux]
  • Version [e.g. 0.105.0]

Activity

  1. abednejad commented on Dec 13, 2025

    @abednejad
    Author

    anybody can help?

  2. abednejad commented on Dec 14, 2025

    @abednejad
    Author

    finally I found a solution to build native nuget packages for torchsharp on arm64 ubuntu

    here is :

    sudo apt update
    sudo apt install -y git cmake build-essential python3 python3-pip
    libopenblas-dev libomp-dev

    wget https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh
    chmod +x dotnet-install.sh
    ./dotnet-install.sh --channel 8.0
    export PATH=$PATH:$HOME/.dotnet

    git clone https://github.com/dotnet/TorchSharp.git
    cd TorchSharp

    git clone --recursive https://github.com/pytorch/pytorch.git
    cd pytorch
    git checkout v2.7.1
    git submodule sync
    git submodule update --init --recursive

    sudo apt install python3-venv python3-full
    python3 -m venv ~/pytorch-env
    source ~/pytorch-env/bin/activate

    pip install --upgrade pip setuptools wheel
    pip install typing_extensions numpy pyyaml
    pip install -r requirements.txt

    USE_CUDA=0 USE_AVX=0 USE_AVX2=0 USE_FMA=0 python3 tools/build_libtorch.py

    USE_CUDA=0 python3 tools/build_libtorch.py



    #!/usr/bin/env bash
    set -e

    === 1. Prepare environment ===

    sudo apt update

    sudo apt install -y git cmake ninja-build build-essential python3 python3-venv
    libopenblas-dev libomp-dev

    sudo apt-get update
    sudo apt-get install python3.12-dev

    Create a clean venv

    python3 -m venv ~/pytorch-env
    source ~/pytorch-env/bin/activate

    Upgrade pip/setuptools/wheel

    pip install --upgrade pip setuptools wheel

    === 2. Clone PyTorch and install deps ===

    git clone --recursive https://github.com/pytorch/pytorch.git
    cd pytorch
    pip install -r requirements.txt

    Clean any old build

    rm -rf build

    === 3. Build LibTorch CPU-only for ARM64 ===

    tmux new -s pytorch-build
    #USE_CXX11_ABI=1 USE_CUDA=0 USE_AVX=0 USE_AVX2=0 USE_FMA=0 python3 tools/build_libtorch.py

    #USE_CXX11_ABI=1 USE_CUDA=0 USE_AVX=0 USE_AVX2=0 USE_FMA=0
    #python3 tools/build_libtorch.py | tee build.log

    python3 setup.py build | tee build.log

    #if ssh closed use this for keept it

    • If SSH disconnects, reconnect and run
      tmux attach -t pytorch-build.

    #for detach from tmux
    Ctrl + b, then d

    #Check logs if needed
    tail -f ~/pytorch/build.log

    Generate install dir include bin,lib,share,...

    cd /pytorch/build
    cmake --install . --prefix ./install

    After build, LibTorch will be in pytorch/build/install

    === 4. Clone TorchSharp and integrate LibTorch ===

    cd /
    git clone https://github.com/dotnet/TorchSharp.git
    cd TorchSharp

    Copy native libs into TorchSharp NativeDependencies

    mkdir -p /TorchSharp/src/Native/NativeDependencies/arm64-linux/libtorch
    cp -r /pytorch/build/install/* /TorchSharp/src/Native/NativeDependencies/arm64-linux/libtorch

    LIBTORCH_DIR=/TorchSharp/src/Native/NativeDependencies/arm64-linux/libtorch

    cd /TorchSharp/src/Native

    cmake -DCMAKE_PREFIX_PATH=/TorchSharp/src/Native/NativeDependencies/arm64-linux/libtorch -DCMAKE_BUILD_TYPE=Release
    
    make install
    
    -- Installing: /usr/local/./libLibTorchSharp.so
    
    
    cp /user/local/libLibTorchSharp.so /TorchSharp/bin/arm64.Release/Native/libLibTorchSharp.so" 
    

    or

    KE_BUILD_TYPE=Release
    

    or

    # fully recommand this command to build libLibTorchSharp.so and put it into correct dir for project
    # this copy build libLibTorchSharp.so to /TorchSharp/bin/arm64.Release/Native
    
    ./build.sh --arch arm64 --configuration Release --libtorchpath /TorchSharp/src/Native/NativeDependencies/arm64-linux/libtorch
    
    #for prevent to run /TorchSharp/src/Native/build.sh do these steps
    #build.sh using downloaded Library from pytorch and Not your own native build of pytorch
    1- comment this lines in /TorchSharp/src/Native/build.proj
       <!-- Run script that invokes Cmake to create VS files, and then calls msbuild to compile them
    	<Message Text="$(MSBuildProjectDirectory)\build.cmd $(BuildArgs)" Importance="High"/>
    	<Exec Command="&quot;$(MSBuildProjectDirectory)\build.cmd&quot; $(BuildArgs)" />
    	-->
    
    2- remove     <PackProject Include="**\TorchAudio.nupkgproj" /> from /TorchSharp/pkg/pack.proj
    

    === 5. Build and pack TorchSharp NuGet ===

    dotnet build -c Release

    dotnet pack -c Release -p:RuntimeIdentifier=linux-arm64 -p:TargetFramework=net6.0 -p:NoWarn=NU5128 -p:ApiCompatValidateAssemblies=false

    echo "✅ TorchSharp NuGet package built successfully!"
    echo "You can find it under TorchSharp/bin/Release/*.nupkg"

    === 6. remember to copy Native build of Pytorch lib in you build .net project ===

  3. ingted commented on Feb 6, 2026

    @ingted
    Image
  4. jchristn commented on Jun 18, 2026

    @jchristn

    +1 for needing native support for using TorchSharp on GB10-powered ARM systems (Dell GB10, similar to DGX Spark)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions