Install Torch 7 and CUDA 9.1 on Ubuntu 18.04 LTS

This post documents the process I followed to be able to run torch on my development machine after upgrading to Ubuntu 18.04 LTS. I had high hopes that the .deb file provided by NVIDIA would “just work,” and it installed fine—but Torch and TensorFlow don’t yet support CUDA 10, so I had to sudo apt remove --purge --auto-remove cuda and start over.

Installing CUDA

CUDA 9.1 is available from NVIDIA as an archived release, and I downloaded the runfile for Ubuntu 17.04.

This installed without complaint using the following command-line flags:

sudo ./cuda_9.1.85_387.26_linux.run  --verbose --silent --toolkit --override

Next I ran the installer runfile again with --silent --samples to install the sample content, then attempted to make one of the example projects. The make failed because Ubuntu 18.04 ships with gcc 7.3 and CUDA 9.1 requires gcc 6. Following this forum post about the gcc version issue, I installed gcc-6 and g++-6 then symlinked CUDA’s bin references to uses these older versions:

sudo apt install gcc-6 g++-6
sudo ln -s /usr/bin/gcc-6 /usr/local/cuda/bin/gcc
sudo ln -s /usr/bin/g++-6 /usr/local/cuda/bin/g++

After a restart, the sample project built properly. Great! Time to install Torch.

Installing Torch

Using the Torch installation guide I pulled Torch down from Git, but immediately the install-deps script gave me trouble.

Package python-software-properties is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
However the following packages replace it:
software-properties-common
E: Package 'python-software-properties' has no installation candidate

In Ubuntu 18.04 python-software-properties is replaced by software-properties-common, which in fact is also installed by the install-deps script. To sidestep this error I commented out the line installing the defunct package, which should look like this once edited:

#sudo apt-get install -y python-software-properties

After this change, bash install-deps now executed successfully.

Next, I needed to run ./install.sh within the Torch directory. This script detects CUDA and attempts to install CUDA-specific lua extensions, but those installations fail because of the same GCC version issue discussed above.

You can specify a particular CC version using environment variables when running the luarocks install command directly, e.g. CC=gcc-6 CXX=g++-6 luarocks install cutorch. Unfortunately this trick does not work at the level of the install.sh script. To work around this, edit install.sh and comment out anything inside of conditionals which check if [ -x "$path_to_nvcc" ]: during initial installation we won’t even try to install anything CUDA-related. Once modified in this way, install.sh should run to completion without complaint.

After installing all non-CUDA-related modules, we need to go back and add the cutorch and cudnn modules we commented out previously. We’ll be using the locally-installed copy of luarocks within the torch directory, and will set environment variables to ensure the proper version of GCC gets used. Run these commands from within the root torch directory:

CC=gcc-6 CXX=g++-6 install/bin/luarocks install cutorch
CC=gcc-6 CXX=g++-6 install/bin/luarocks install cunn
CC=gcc-6 CXX=g++-6 install/bin/luarocks install cudnn

Once cutorch, cunn, and cudnn are installed, your CUDA 9.1 & Torch environment should be good to go. You will now be able to run neural-style, torch-rnn, or any other relevant torch modules.

2 thoughts on “Install Torch 7 and CUDA 9.1 on Ubuntu 18.04 LTS

  1. K. Adam White says:

    A further note on neural-style: After installing cudnn v7.1.3 for CUDA 9.1, I had to download and run an alternative version of cudnn.torch to avoid a version conflict where the bundled packages expected cudnn v5. See soumith/cudnn.torch#383, and note the commands

    git clone https://github.com/soumith/cudnn.torch.git -b R7
    cd cudnn.torch
    luarocks make cudnn-scm-1.rockspec
    


    This approach may require setting the environment variable CUDNN_PATH:

    export CUDNN_PATH="/usr/local/cuda/lib64/libcudnn.so.7"

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.