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:

isinstance and issubclass in Python


Use this built-in function to find out if a given object is an instance of a certain class or any of its subclasses. You can even pass a tuple of classes to be checked for the object.

The only gotcha you will discover is this: in Python a bool object is an instance of int! Yes, not kidding!


This built-in function is similar to isinstance, but to check if a type is an instance of a class or any of its subclasses.

Again, the only gotcha is that bool type is subclass of int type.

How to convert Python dict to class object with fields

A Python dict is extremely versatile. However, there might be situations where you want to access the dict as if it were a class object and the keys were its fields. This can be easily done by using a namedtuple. Just give it a name and the use the keys to populate its named fields. Set the values for those fields by passing the values from the dict. It all boils down to a single line.

This example code demonstrates the above:

pgrep and pkill

pgrep and pkill are two useful commands that go well together. You can list processes using ps and kill them using kill. However, I find it easier to use pgrep and pkill when I want to find and kill a process.


  • To list all the PIDs of a user:
$ pgrep --uid joe
$ pgrep -u joe
  • To list the PID and the corresponding process name for a user:
$ pgrep --uid joe --list-name
$ pgrep -u joe -l
  • To list the PID and the corresponding full command line for a user:
$ pgrep --uid joe --list-full
$ pgrep -u joe -a

This is extremely useful because to find Python scripts or commandline arguments to a program that is running as a process.

  • To list the PID whose process name matches input pattern:
$ pgrep foobar
  • To list the PID and process names that match input pattern:
$ pgrep -l foobar
  • To list the PID and command line of processes that match input pattern:
$ pgrep -a foobar


pkill is used to send a signal to or kill processes by using a pattern that matches a process name or its command line. pkill takes many arguments that are similar to pgrep.

  • To kill all processes of current user that matches input pattern:
$ pkill foobar
  • To kill all processes of current user that matches input pattern in its command line:
$ pkill -f foobar

Tried with: Ubuntu 16.04