How to install cuDNN

cuDNN provides primitives for deep learning networks that have been accelerated for GPUs by NVIDIA.

  • To download cuDNN head over to the cuDNN page here. cuDNN is not directly available for download. NVIDIA requires you to create a login. After that it presents cuDNN downloads in different formats (.tgz or .deb).

  • I prefer to install from .tgz since it gives more control. Unzip the file and it will create a cuda directory which has the required include and lib directories.

  • I like to rename this directory and keep it at /usr/local:

$ mv cuda cudnn
$ mv cudnn /usr/local
  • Remember to add the path to the cuDNN libraries to your LD_LIBRARY_PATH. For my case, that would be /usr/local/cudnn/lib64

  • For CMake in Caffe to automatically find cuDNN while building, export an environment variable named CUDNN_DIR pointing to the directory. For me, that directory would be /usr/local/cudnn

That is it! Caffe should be able to find and link with cuDNN now.

Tried with: cuDNN 6.0 and Ubuntu 16.04


How to install CUDA for NVIDIA GTX 1050 (Notebook)

Installing NVIDIA graphics drivers on Linux has never been easy for me! I bought a notebook with NVIDIA GTX 1050 GPU recently and installed Kubuntu 16.04. I had to wait for more than a month for NVIDIA to release drivers that supported the notebook 1050 variant.

  • Once the driver was released, I downloaded the .run file directly from NVIDIA’s website here. I ran the installation:
$ sudo sh

When I rebooted, I got a black screen! Not surprising with NVIDIA and Linux! I had to uninstall it to get back to work:

$ sudo sh --uninstall
  • After another month, I found that the latest NVIDIA driver supporting the notebook 1050 was available from Ubuntu. So, I tried installing that:
$ sudo apt install nvidia-381

Reboot and I got a new error message in a GUI dialog box:

The system is running in low-graphics mode
Your screen, graphics card, and input device settings could not be detected correctly.
You will need to configure these yourself.

I had to uninstall it to get back to work:

$ sudo apt purge nvidia-381
  • It finally dawned on me that what I really wanted was to be able to run CUDA programs on the GPU. I did not really care about X or games being able to use the GPU. So, I went back to the .run driver and installed it without OpenGL:
$ sudo sh --no-opengl-files

After rebooting, I found that I still had a desktop. That was a big relief! I proceeded to download and install CUDA:

$ sudo sh

I took care to not install the graphics driver that comes along with the CUDA installer. That is it! I was able to compile and run the CUDA samples. Running ./deviceQuery from the samples showed the GTX 1050 and that is all I wanted! 🙂

The HWE stack for Ubuntu LTS

Many Ubuntu LTS users have a problem. They might want to stick with their LTS installation for the full 5 years of support due to stability of applications, libraries and software. But they might also wish for newer versions of Linux kernel and X, so that they can take advantage of newer hardware or fixes for their existing hardware.

Ubuntu provides Hardware Enablement Stack (HWE) that pulls in more recent versions of Linux kernel and X and provides them to Ubuntu LTS release users. The Linux kernel and X that came with the original LTS release is called the General Availability (GA) stack. The latest available HWE stack is also installed by default if you install an updated point release of a LTS release. For example, if you install Ubuntu 16.04, that installs the GA stack. If you install a more recent Ubuntu 16.04.3, that will install a HWE stack.

I faced a problem recently, where my Intel Wireless-AC 3165 hardware was not detected by the Linux kernel in Ubuntu 16.04, as described here. To install the HWE stack on my Ubuntu 16.04, I used this command:

$ sudo apt-get install --install-recommends linux-generic-hwe-16.04 xserver-xorg-hwe-16.04

After switching to the Linux 4.8.x kernel this installed, my wireless hardware worked fine. I was saved by the HWE stack!

For more details on HWE, see LTS Enablement Stack page.

How to build OGRE in Ubuntu

Ubuntu ships with a particular version of OGRE that can be installed easily. For example, on Ubuntu 16.04:

$ sudo apt install libogre-1.9-dev

However, if you need to use an older or newer version of OGRE then you might need to build it yourself. This is not so hard on Linux, as it is on Windows.

  • Install the essential packages needed for building OGRE:
$ sudo apt install build-essential automake libtool libfreetype6-dev libfreeimage-dev libzzip-dev libxrandr-dev libxaw7-dev freeglut3-dev libgl1-mesa-dev libglu1-mesa-dev libpoco-dev libtbb-dev doxygen libcppunit-dev
  • Download the OGRE version you want as a ZIP file from its Mercurial repository on Bitbucket here.

  • Unzip the file and build OGRE:

$ mkdir build
$ cd build
$ cmake ..
$ make
  • To install OGRE to /usr/local/lib/OGRE:
$ sudo make install

Tried with: OGRE 1.8 and Ubuntu 16.04

The strange case of the notebook that woke up at night


In the last week, I started noticing that my notebook, that I had suspended before going to sleep would have woken up during the night! This notebook was running Ubuntu 16.04. It took a bit of investigation to realize that the notebook was being woken up by my new Targus AMW071 wireless mouse. Some slight vibration to the desk during the night would move the mouse and it would wake up Linux from its sleep!


The list of devices that can wake up Linux from sleep is listed in the file /proc/acpi/wakeup. Each line of that file has a four-letter device code, obtained from BIOS, and whether that device is enabled (can wakeup from sleep) or disabled.

I checked which devices could wakeup my Linux:

$ cat /proc/acpi/wakeup | grep enabled
XHC   S3    *enabled   pci:0000:00:14.0
PWRB      S3    *enabled   platform:PNP0C0C:00
SLPB      S3    *enabled   platform:PNP0C0E:00
LID0      S3    *enabled   platform:PNP0C0D:00

The culprit here was XHC, the USB 3.0 hub to which the mouse was connected. PWRB and SLPB are Power buttons and LID0 is the notebook lid. So, I had to disable the XHC device in this file. Now, if the device name is written to this file, that operation toggles the enabled or disabled state for that device.

So, to disable my mouse from waking up my notebook, I created this shell script:

echo "XHC" > /proc/acpi/wakeup

And I executed that file with superuser privileges:

$ sudo ./

I could see that this had disabled the mouse from waking up my notebook:

$ cat /proc/acpi/wakeup | grep enabled
PWRB      S3    *enabled   platform:PNP0C0C:00
SLPB      S3    *enabled   platform:PNP0C0E:00
LID0      S3    *enabled   platform:PNP0C0D:00

I suspended the notebook and also verified that indeed the mouse could not wake it up.

The only problem left now was that this toggle would apply only for this Linux session. A system reboot would put back the XHC device in enabled state. To run this toggle script on every system reboot:

$ sudo crontab -e
@reboot /home/joe/scripts/

Now I had a permanent solution that makes sure that the mouse can never wake up my Ubuntu from sleep! 🙂


Could not load OGRE dynamic library


I had an binary compiled on Ubuntu 14.04. I tried to run it on Ubuntu 16.04 and got this error:

OGRE EXCEPTION(7:InternalErrorException): Could not load dynamic library /usr/lib/x86_64-linux-gnu/OGRE-1.8.0/RenderSystem_GL


From the error, I could see that it was looking for an OGRE library file. I installed the OGRE library available on Ubuntu 16.04:

$ sudo apt install libogre-1.9-dev

The error still persisted, because Ubuntu 16.04 only has OGRE 1.9, while this binary was looking for OGRE 1.8 library files.

I tried to create a symbolic link of an OGRE 1.8 directory to the existing OGRE 1.9 directory:

$ cd /usr/lib/x86_64-linux-gnu/
$ ln -s OGRE-1.9.0 OGRE-1.8.0

This worked! The executable ran without problems. This saved me from having to build OGRE 1.8 from source on this computer.

How to deal with YAML in Python

YAML (Yet Another Markup Language) is a language similar to JSON for reading and writing configuration information to files that are human readable. YAML is a superset of JSON. It uses indentation instead of the braces used by JSON.

  • To be able to deal with YAML in Python, install the PyYAML package:
$ sudo pip install PyYAML
$ sudo pip3 install PyYAML
  • Similar to JSON, YAML file can be directly loaded into a Python list or dict, depending on whether the root structure of the file is a list or a dict:
import yaml
y = yaml.load(open("foobar.yaml"))
  • Writing a Python structure back to a YAML is similarly straightforward:
yaml.dump(y, open("foobar.yaml", "w"))
  • Note that the YAML file is written in flow style by default. This makes it look a bit like JSON. For human readability, it might be better to dump in block style, like this:
yaml.dump(y, open("foobar.yaml", "w"), default_flow_style=False)

Tried with: PyYAML 3.11, Python 3.5.2 and Ubuntu 16.04

Size of argument and environment lists exceeds limit


If you try to cp or mv a large number of files or directories, you will encounter this error:

$ cd big_dir
$ cp * ../../some_other_dir
Failed to execute process '/bin/cp'. Reason:
The total size of the argument and environment lists 3.2MB exceeds the operating system limit of 2MB.
Try running the command again with fewer arguments.

Note that you can copy or move one directory that contains a million files or any number of files for that matter. This error happens only if the shell has to pass a large number of input arguments to the cp or mv programs. So, if you run the above command from inside a directory containing 100K files, you will surely get this error.

This is because there is a limit to the size of the arguments and environment strings that can be passed to a program. That limit can be queried:

$ getconf ARG_MAX

The result will vary on different computers. But they always have a limit and it is encoded in the Linux kernel. There does not seem to be an userspace method to increase this size.


Instead of looking to increase the ARG_MAX size, examine the real problem. Why make the shell expand all the filenames and pass them as one gigantic list of strings to the programs? Instead these alternate solutions can be tried:

  • See if you can instead cp or mv a parent directory, instead of a million files.

  • Move the files one by one by writing a loop in shell script or Python.

  • Use other programs like rsync to copy a directory to the destination.

Reference: Search for ARG_MAX in the execve(2) manpage

Tried with: Ubuntu 16.04

How to use Bluetooth headphones with Linux

Configuring Bluetooth headphones

One of the irritating problems with Linux in general is Bluetooth. Below are the steps I had to follow to pair and use a Creative WP-300 Bluetooth headphones with Kubuntu 16.04. The procedure should be similar for any Bluetooth headphones or speaker and with any other variant of Ubuntu.

  • Make sure that your computer has a Bluetooth adapter and it is working. Do not just assume there is an adapter and its working in Linux! You should be able to see a Bluetooth icon in the system panel or system tray. In the Bluetooth settings you should be able to see the adapter.

  • Power on your Bluetooth headphones or speaker and put it into pairing mode. Refer to its documentation if you do not know how to do this.

  • Go to the Bluetooth settings in Linux and try to find device and once the device is listed, pair with it and connect to it.

  • Go to the audio settings and you should be able to see the Bluetooth device listed there. If it is not there, you might need to restart Linux. I know it is crazy, I had to do this, since no other solution offered online worked for me!

  • If your device is listed in audio settings then try to play some music or video and see if it plays in the device. Most probably, it will still play on the default speaker of your computer! Go to System Settings → Multimedia → Audio and Video → Device Preference → Audio Playback. Your Bluetooth device must be listed here. Grab it and move it above the built-in audio device. This tells Linux to use this as the default audio output.

Now you should be able to playback audio to your Bluetooth device 👍

Aligned memory allocation

In some scenarios, you want to get memory that is aligned at an address that is a certain power of 2. Certain CPU architectures and certain operations require (or are faster) if their operands are located at an address that is a multiple of a certain power-of-2 number. For these reasons, you might see that many multi-platform libraries use an aligned memory allocator instead of malloc in their code. For example, OpenCV uses methods named fastMalloc and fastFree inside its code that do this type of allocation and freeing.

Most of these methods work like this:

  • They internally get memory from malloc. However, if you requested for N bytes, the wrapper will request for N+P+A bytes from malloc. Here, P is the size of a pointer on that CPU architecture and A is the alignment required, expressed in power-of-2 number of bytes. For example, if I request for 100 bytes on a 64-bit CPU and require the memory to be aligned to a multiple of 32, then the wrapper will request for 140 bytes.

  • After getting the memory from malloc, it aligns the pointer forward so that (1) the pointer is at an address that is aligned as per requirement and (2) there is space behind the pointer to store a memory address.

  • Then we sneak and store the address actually returned by malloc behind the pointer address and return the pointer to the user.

  • The user has to use our free wrapper to free this pointer. When she does that we sneak back to reveal the actual address returned by malloc and free using that.

Here is some example code that illustrates aligned memory allocation: