Victor is a full stack software engineer who loves travelling and building things. Most recently created Ewolo, a cross-platform workout logger.

    Ubuntu tips and tricks

    Updated on October 24, 2015 with a how-to on installing the latest node.js version.

    Managing disk space in Ubuntu

    I've been running Ubuntu on my personal computer for over 10 years now and I've been fortunate enough to always have had a nice fat hard drive that could accomodate most of my needs (which aren't much to be honest, most of my disk space is used up by different virtual images which I need for development). Anyways, I recently got a new laptop with an SSD and it has made a world of a difference performance-wise and really boosted my productivity. However, given that SSDs aren't cheap, I was forced to go for a 100Gb hard drive, which as you can imagine, filled up rather quickly.

    So without further ado, here's a quick tutorial/helplist of useful commands for managing disk space which might be handy for anyone trying to squeeze out that extra little space out of their machines (or save money in case of hosted servers).

    Commands for disk usage analysis

    df -h
    Display the amount of disk space available on the file system on all currently mounted file systems (the -h option is to output in human readable format)
    du -skh
    Calculate the space being used by the current directory outputted in human readable format. You can also use du --max-depth=1 ./ | sort -n -r to list all the files within the current directory by size (change ./ to a different directory to process that instead).
    Commandline-base disk usage analyser tool, sudo apt-get install ncdu to install it
    Disk Usage Analyser
    If you have access to/prefer a GUI, this program can be run via the dash

    Now that we have a few tools to be able to analyse disk usage we can see what can be done to free up space from an Ubuntu perspective. Before we dig into this, a quick point needs to be made regarding the usual suspects such as clearing out your browser cache, etc.

    The following commands show you how much space is being used by cached packages and how to clear it:

    $ du -sh /var/cache/apt/archives
    $ sudo apt-get clean

    The following command lists out all the packages installed in decreasing order of size:

    $ dpkg-query -W --showformat='${Installed-Size} ${Package}\n' | sort -nr | less
    Note that the usual suspects here are old kernels which you usually do not need. Before wiping out old kernels, make sure that you don't delete the latest or rather the one that you are currently running. The following command can be used to display the current kernel version: uname -a

    There's actually quite a bit more that can be done within the package manager such as removing residual config, removing orphaned packages, removing unused/unnecessary packages, etc. Another useful trick is to purge locale data but I'll let you google this stuff as in my experience, this hasn't really provided me with all that much gain for the effort required. Of course if you have an 8Gb AWS instance then every byte counts ;)

    Free up unused memory in Ubuntu

    If you're like me, then you probably have quite a few chrome/chromium browser tabs and windows open. This is all very well but I usually find that even after closing a bunch of unused tabs, ubuntu refuses to release unused memory.

    ubuntu hogging memory

    Given that this happens to me frequently enough on various systems that I end up using, I figured that this entry would be a simple way of keeping myself sane.

    We can use the following command to keep track of memory usage: watch -n 1 free -m

    free -m

    A little known key combination, Shift + Esc in Chrome brings up a little task manager window which show how much resources are being used by each tab (gmail is always a hog in my experience).

    The following beast of a command will free up both used and cached memory (page cache, inodes, and dentries):

    sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches

    I like to put this into an executable script called and run it every once in a while when chromuim doesn't co-operate :)

    Installing the latest Node.js in Ubuntu - deprecated

    Note that this article has been deprecated in favour of this one.

    Installing the latest Node.js on Ubuntu is pretty straight-forward:

    curl -sL | sudo bash -
    sudo apt-get install -y nodejs

    The upgrade of Node.js also came with an updated npm (2.11) which I then manually updated to 3.3.8:

    sudo npm update -g npm

    However the new npm was installing and looking for global node_modules under /usr/lib/node_moduleswhereas my earlier modules were setup under /usr/local/lib/node_modules. This could be confirmed via ls -la $(which mocha) since I had mocha installed globally. The fix involved telling npm what the install prefix path is:

    sudo npm config set prefix /usr/local

    Some other useful npm commands:

    # List all installed packages
    sudo npm ls -g
    # update a given package
    sudo npm update -g mocha
    # find out all outdated packages
    sudo npm outdated -g

    HackerNews submission / discussion

    Back to the article list.

    SmallData newsletter

    Subscribe to get articles as they are published direct to your inbox!