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.

13 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"

    1. K. Adam White says:

      I’m afraid I don’t know, my best suggestion would be to try to manually edit the script to download the right file. (Sorry for the delayed response)

  2. Richardo says:

    I got other problem when running bash install-deps,

    :0:7: internal compiler error: Segmentation fault
    ztpsv_L.c:45:5: note: in expansion of macro ‘CNAME’
     int CNAME(BLASLONG m, FLOAT *a, FLOAT *b, BLASLONG incb, void *buffer){
         ^~~~~
    Please submit a full bug report,
    with preprocessed source if appropriate.
    See  for instructions.
    Makefile:2899: recipe for target 'ztpsv_NLU.o' failed
    make[1]: *** [ztpsv_NLU.o] Error 1
    make[1]: *** Waiting for unfinished jobs....
    make[1]: Leaving directory '/tmp/tmp.tVbxuMU1Hy/OpenBLAS/driver/level2'
    Makefile:145: recipe for target 'libs' failed
    make: *** [libs] Error 1
    Error. OpenBLAS could not be compiled

    can you help me with resolve this?

  3. Richardo says:

    The install-deps part doesnt work for me even after commenting it. I still got another error which is
    Makefile:145: recipe for target ‘libs’ failed
    make: *** [libs] Error 1
    Error. OpenBLAS could not be compiled
    I would appreciate if you can help

    1. Sean says:

      This is another issue that’s fairly easy to fix. Each generation of GPU has a code, I’m pretty sure you can look it up on NVIDIAs site. Change all the 20s to the number corresponding with you GPU and you’re good.

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.