# Installation

After the usual R package installation, `torch` requires installing other 2 libraries: LibTorch and LibLantern. They are automatically installed by detecting information about you OS if you are using `torch` in interactive mode. If you are running `torch` in non-interactive environments you need to set the `TORCH_INSTALL` env var to 1, so it’s automatically installed or manually call `torch::install_torch()`.

We have provide pre-compiled binaries for all major platforms and you can find specific installation instructions below.

## Windows

### CPU

If you don’t have a GPU or want to install the CPU version of `torch`, you can install with:

    install.packages("torch")

Some Windows distributions don’t have the Visual Studio runtime pre-installed and you will observe an error like:

    Error in cpp_lantern_init(normalizePath(install_path())): C:\Users\User\Documents\R\R-4.0.2\library\torch\deps\lantern.dll - The specified module could not be found.

See here for instructions on how to install it.

### GPU

Since version 0.1.1 torch supports GPU installation on Windows. In order to use GPU’s with torch you need to:

- Have a CUDA compatible NVIDIA GPU. You can find if you have a CUDA compatible GPU here.

- Have properly installed the NVIDIA CUDA toolkit version 10.2 or 11.1. For CUDA v11.1, follow the installation instructions here. Note: We currently do not support the latest CUDA version 11.

- Have installed cuDNN version 7.6 (if using CUDA v10.2) or cuDNN 8.0 (if using CUDA 11.1). Follow the installation instructions available here.

Once you have installed all pre-requisites you can install `torch` with:

    install.packages("torch")

If you have followed default installation locations we will detect that you have CUDA software installed and automatically download the GPU enabled Lantern binaries. You can also specify the `CUDA` env var with something like `Sys.setenv(CUDA="11.1")` if you want to force an specific version of the CUDA toolkit.

## MacOS

### CPU

We only support CPU builds of torch on MacOS. On MacOS you can install torch with:

    install.packages("torch")

## Linux

### CPU

To install the cpu version of `torch` you can run:

    install.packages("torch")

### GPU

To install the GPU version of `torch` on linux you must verify that:

- You have a NVIDIA CUDA compatible GPU. You can find if you have a CUDA compatible GPU here.

- You have correctly installed the NVIDIA CUDA Toolkit versions 10.2 or 11.1, follow the instructions here.

- You have installed cuDNN version 7.6 - (for CUDA 10.2) and 8.0 for CUDA 11.1. Follow the installation instructions available here.

Once you have installed all pre-requisites you can install `torch` with:

    install.packages("torch")

If you have followed default installation locations we will detect that you have CUDA software installed and automatically download the GPU enabled Lantern binaries. You can also specify the `CUDA` env var with something like `Sys.setenv(CUDA="10.2")` if you want to force an specific version of the CUDA toolkit.

## Troubleshooting

### Large file download timeout

If you encounter timeout during library download, or if after a while, downloads end-up with a warning such as:

    Warning messages:
    1: In utils::download.file(library_url, temp_file) :
      downloaded length 44901568 != reported length 141774525
    2: In utils::download.file(library_url, temp_file) :
      URL '...': Timeout of 60 seconds was reached
    3: Failed to install Torch, manually run install_torch(). download from 'https://download.pytorch.org/libtorch/cpu/libtorch-macos-1.7.1.zip' failed

This means you encounter a download timeout. then, you should increase the timeout value in `install_torch()` like

    install_torch(timeout = 600)

### File based download

In cases where you cannot reach download servers from the machine you intend to install torch on, last resort is to install Torch and Lantern library from files. This is done in 3 steps :

1- get the download URLs of the files.

    get_install_libs_url(type = "10.2")

2- save those files into the machine filesystem. We will use `/tmp/` here as an example .

3- install torch from files

    install_torch_from_file(libtorch = "file:///tmp/libtorch-cxx11-abi-shared-with-deps-1.7.1%2Bcu101.zip",
                            liblantern = "file:///tmp/Linux-gpu-101.zip")