Setup for Torch

Torch is an open source machine learning library, a scientific computing framework, and a script language based on the Lua programming language. It provides a wide range of algorithms for deep machine learning, and uses the scripting language LuaJIT, and an underlying C implementation.

A summary of core features:

  • a powerful N-dimensional array
  • lots of routines for indexing, slicing, transposing, …
  • amazing interface to C, via LuaJIT
  • linear algebra routines
  • neural network, and energy-based models
  • numeric optimization routines
  • Fast and efficient GPU support
  • Embeddable, with ports to iOS, Android and FPGA backends



Torch Instalation

Step 0: Installing Torch

# in a terminal, run the commands WITHOUT sudo
git clone ~/torch --recursive
cd ~/torch; bash install-deps;

The first script installs the basic package dependencies that LuaJIT and Torch require. The second script installs LuaJIT, LuaRocks, and then uses LuaRocks (the lua package manager) to install core packages like torch, nn and paths, as well as a few other packages.

The script adds torch to your PATH variable. You just have to source it once to refresh your env variables. The installation script will detect what is your current shell and modify the path in the correct configuration file.

# On Linux with bash
source ~/.bashrc

if you ever need to uninstall torch, simply run the command:

rm -rf ~/torch

Step 1: Install torch with Lua 5.2

If you want to install torch with Lua 5.2 instead of LuaJIT, simply run:

git clone ~/torch --recursive
cd ~/torch

# clean old torch installation

# : set env to use lua

Step 3: Install required packages using Luarocks

New packages can be installed using Luarocks from the command-line:

# Install using luarocks
luarocks install torch
luarocks install nn
luarocks install nngraph 
$luarocks install image 
luarocks install optim
luarocks install lua-cjson
luarocks install torch-word-emb
luarocks install rnn
luarocks install nnx
luarocks install dp
luarocks install dpnn
luarocks install itorch
luarocks install sys
luarocks install xlua
luarocks install penlight 

luarocks install display
luarocks install gnuplot
luarocks install imgraph
luarocks install signal

luarocks install senna
luarocks install cephes

Step 4: Install torch-hdf5

# We need to install torch-hdf5 from GitHub
git clone
cd torch-hdf5
luarocks make hdf5-0-0.rockspec

Step 5: Install loadcaffe

loadcaffe depends on Google’s Protocol Buffer library so we’ll need to install that first:

sudo apt-get install libprotobuf-dev protobuf-compiler

Now we can instal loadcaffe:

luarocks install loadcaffe

Step 6: Install CUDA backend for torch(For GPU  Only)

If you’d like to train on an NVIDIA GPU using CUDA (this can be to about 15x faster), you’ll of course need the GPU, and you will have to install the CUDA Toolkit. Then get the cutorch and cunn packages:

luarocks install cutorch
luarocks install cunn
luarocks install cudn

Step 7: Install  OpenCL Backend for torch(Optional)

If you’d like to use OpenCL GPU instead (e.g. ATI cards), you will instead need to install the cltorch and clnn packages, and then use the option -opencl 1 during training (cltorch issues):

luarocks install cltorch
luarocks install clnn

Step 8: Install cuDNN(Optional)

cuDNN is a library from NVIDIA that efficiently implements many of the operations (like convolutions and pooling) that are commonly used in deep learning.

After registering as a developer with NVIDIA, you can download cuDNN here. Make sure to download Version 4.

After dowloading, you can unpack and install cuDNN like this:

tar -xzvf cudnn-7.0-linux-x64-v4.0-prod.tgz
sudo cp cuda/lib64/libcudnn* /usr/local/cuda-7.0/lib64/
sudo cp cuda/include/cudnn.h /usr/local/cuda-7.0/include/

Also check your LD_LIBRARY_PATH:


You should see /usr/local/cuda-7.0/lib64 along with possibly other things.

if ur not getting like this then do

You need to add something like this to your .bashrc or other startup scripts.

export LD_LIBRARY_PATH=/usr/local/cuda-7.0/lib64:$LD_LIBRARY_PATH

then source ~/.bashrc.

Which can solve following problem

CuDNN problems

user@user-XPS-8500:~/neural-style$ th neural_style.lua -gpu 0 -backend cudnn
/home/user/torch/install/bin/luajit: /home/user/torch/install/share/lua/5.1/trepl/init.lua:384: /home/ben/torch/install/share/lua/5.1/trepl/init.lua:384: /home/user/torch/install/share/lua/5.1/cudnn/ffi.lua:1279: 'libcudnn (R4) not found in library path.
Please install CuDNN from
Then make sure files named as or libcudnn.4.dylib are placed in your library load path (for example /usr/local/lib , or manually add a path to LD_LIBRARY_PATH)

stack traceback:
    [C]: in function 'error'
    /home/user/torch/install/share/lua/5.1/trepl/init.lua:384: in function 'require'
    neural_style.lua:64: in function 'main'
    neural_style.lua:500: in main chunk
    [C]: in function 'dofile'
    .../user/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:145: in main chunk
    [C]: at 0x00406670

Next we need to install the torch bindings for cuDNN:

luarocks install cudnn


Install Torch Locally

Self-contained Torch installation

Install dependencies. Usesapt-get on Ubuntu, which might require sudo. Usesbrew on OSX.

curl -s | bash

Install this repo, which installs the torch distribution, with a lot of nice goodies.

git clone ~/torch --recursive
cd ~/torch; ./

By default Torch will install LuaJIT 2.1. If you want other options, you can use the command:


Now, everything should be installed. Either open a new shell, or source your profile via

. ~/.bashrc  # or: . ~/.zshrc
th -e "print 'I just installed Torch! Yesss.'"

Note: If you use a non-standard shell, you’ll want to run this command


Tested on Ubuntu 14.04, CentOS/RHEL 6.3 and OSX

for more information, plz check
not required

luarocks install --deps-mode=all --local nngraph

Torch Installation 3 Steps

git clone ~/torch --recursive
cd ~/torch; ./

By default Torch will install LuaJIT 2.1. If you want other options, you can use the command:

step 2: (optional)

Now, everything should be installed. Either open a new shell, or source your profile via

step 3:
. ~/.bashrc 
th -e "print 'I just installed Torch! Yesss.'"

Torch uninstallation

rm -rf ~/torch 

luarocks package Installation

luarocks install cunn 1.0.0
luarocks remove  cutorch

luarocks package remove

remove – Uninstall a rock.
luarocks remove --force cunn 1.0.0
luarocks remove --force cutorch



2 thoughts on “Setup for Torch

  1. Vinod Kumar says:

    I am facing problem to install ‘matio’ package of Luarocks.. getting the following error.

    $ luarocks install matio
    Warning: Failed searching manifest: Failed fetching manifest for – Failed downloading – /home/vinod/.cache/luarocks/https___raw.githubusercontent.com_torch_rocks_master/manifest
    Warning: Failed searching manifest: Failed fetching manifest for – Failed downloading – /home/vinod/.cache/luarocks/https___raw.githubusercontent.com_rocks-moonscript-org_moonrocks-mirror_master/manifest

    Error: No results matching query were found.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s