Phaengris.Art hand-drawn arts, city photos, linux / dev stories

Install Torch.rb on Linux

The process is simple but not without it’s pitfalls. Here’s the instruction how it worked for me. My system is Fedora Linux 38, but I believe it should work for other Linux distros as well.

mkdir -p ~/.local/opt

Go to https://pytorch.org/get-started/locally/

Choose:

  • PyTorch Build: Stable
  • Your OS: Linux
  • Package: LibTorch
  • Language: will be automatically switched to C++ / Java after you choose LibTorch as package.
  • Compute Platform: depends on your system’s abilities. If unsure, choose CPU.

In the “Run This Command field” you’ll see something like this:

Download here (Pre-cxx11 ABI): https://download.pytorch.org/libtorch/cu118/libtorch-shared-with-deps-2.1.1%2Bcu118.zip

Download here (cxx11 ABI): https://download.pytorch.org/libtorch/cu118/libtorch-cxx11-abi-shared-with-deps-2.1.1%2Bcu118.zip

You need the cxx11 ABI version.

mkdir -p ~/Downloads

wget -P ~/Downloads https://download.pytorch.org/libtorch/cu118/libtorch-cxx11-abi-shared-with-deps-2.1.1%2Bcu118.zip

unzip -d ~/.local/opt ~/Downloads/libtorch-cxx11-abi-shared-with-deps-2.1.1+cu118.zip

Go to your Ruby project

bundle config build.torch-rb --with-torch-dir=/home/"$USER"/.local/opt/libtorch

bundle add torch-rb

Compiling the gem will take a while.

You may want to verify if Torch.rb is installed correctly:

bundle exec irb

irb(main):002:0> require 'torch'
=> true

irb(main):003:0> Torch::Tensor.new([1, 2, 3])
=> tensor([1., 2., 3.])

Remark:

  • You don’t need to install Python stuff at all. In fact the Python package seems to be built with Pre-cxx11 ABI, which is not suitable for Torch.rb.

References: