Install lxml for Python on DigitalOcean

Currently I am using a DigitalOcean droplet with 512 MB to run this website. I ran into an issue when I was trying to install lxml. First make sure the correct libraries are installed before lxml is installed.

$ sudo apt-get install python-dev libxml2-dev libxslt1-dev zlib1g-dev

Next, be aware that the 512 MB is not enough memory to compile the lxml package with Cython when you use pip to install, which means some additional steps are needed. To virtually increase your work memory, you could use a swapfile. Create a swapfile with these commands:

$ sudo dd if=/dev/zero of=/swapfile1 bs=1024 count=524288
$ sudo mkswap /swapfile1
$ sudo chown root:root /swapfile1
$ sudo chmod 0600 /swapfile1

Now you can use pip to install the lxml Python module

$ sudo pip install lxml

And of course you need to clean up after installation is done.

$ sudo swapoff -v /swapfile1
$ sudo rm /swapfile1