446

I'm having difficulty installing lxml with easy_install on Ubuntu 11.

When I type $ easy_install lxml I get:

Searching for lxml
Reading http://pypi.python.org/simple/lxml/
Reading http://codespeak.net/lxml
Best match: lxml 2.3
Downloading http://lxml.de/files/lxml-2.3.tgz
Processing lxml-2.3.tgz
Running lxml-2.3/setup.py -q bdist_egg --dist-dir /tmp/easy_install-7UdQOZ/lxml-2.3/egg-dist-tmp-GacQGy
Building lxml version 2.3.
Building without Cython.
ERROR: /bin/sh: xslt-config: not found

** make sure the development packages of libxml2 and libxslt are installed **

Using build configuration of libxslt 
In file included from src/lxml/lxml.etree.c:227:0:
src/lxml/etree_defs.h:9:31: fatal error: libxml/xmlversion.h: No such file or directory
compilation terminated.

It seems that libxslt or libxml2 is not installed. I've tried following the instructions at http://www.techsww.com/tutorials/libraries/libxslt/installation/installing_libxslt_on_ubuntu_linux.php and http://www.techsww.com/tutorials/libraries/libxml/installation/installing_libxml_on_ubuntu_linux.php with no success.

If I try wget ftp://xmlsoft.org/libxml2/libxml2-sources-2.6.27.tar.gz I get

<successful connection info>
==> SYST ... done.    ==> PWD ... done.
==> TYPE I ... done.  ==> CWD (1) /libxml2 ... done.
==> SIZE libxml2-sources-2.6.27.tar.gz ... done.
==> PASV ... done.    ==> RETR libxml2-sources-2.6.27.tar.gz ... 
No such file `libxml2-sources-2.6.27.tar.gz'.

If I try the other first, I'll get to ./configure --prefix=/usr/local/libxslt --with-libxml-prefix=/usr/local/libxml2 and that will fail eventually with:

checking for libxml libraries >= 2.6.27... configure: error: Could not find libxml2 anywhere, check ftp://xmlsoft.org/.

I've tried both versions 2.6.27 and 2.6.29 of libxml2 with no difference.

Leaving no stone unturned, I have successfully done sudo apt-get install libxml2-dev, but this changes nothing.

bluszcz
  • 3,776
  • 4
  • 28
  • 49
Eric Wilson
  • 51,818
  • 71
  • 192
  • 262

11 Answers11

957

Since you're on Ubuntu, don't bother with those source packages. Just install those development packages using apt-get.

apt-get install libxml2-dev libxslt1-dev python-dev

If you're happy with a possibly older version of lxml altogether though, you could try

apt-get install python-lxml

and be done with it. :)

Stephen Fuhry
  • 11,090
  • 6
  • 49
  • 52
AKX
  • 93,995
  • 11
  • 81
  • 98
  • 52
    this answer should be added to lxml install instructions :) thanks +1 – Boaz Mar 16 '12 at 09:17
  • 1
    This answer does not answer to question in the title 'with easy_install' – bluszcz Nov 14 '12 at 19:03
  • 43
    After running `apt-get install libxml2-dev libxslt-dev` you can just `easy_install lxml` – Bob Jansen Dec 06 '12 at 18:37
  • 11
    in the latest ubuntu-like distros, you have to install libxslt1-dev instead of libxslt-dev – Jose A. Martín Jul 18 '13 at 16:03
  • Obviously not the (gr)easy_install way about it... however, it may help a fellow in a pinch to attempt `apt-get build-dep python-lxml` and then `STATIC_DEPS=true sudo pip install lxml`. – 4Z4T4R Feb 12 '14 at 10:55
  • 1
    I have to install one additional package for my ubuntu 12.4 LTS: apt-get install libffi-dev – chfw May 29 '14 at 21:18
  • 21
    At least on Ubuntu 14.04 you also have to install the package `zlib1g-dev`. – Christian Berendt Jun 20 '14 at 17:41
  • 1
    @BobJansen python-dev is required, as the answer suggests - `easy_install lxml` won't work without it. You possibly already had python-dev installed. – nevelis Oct 23 '14 at 05:33
  • but what if I'm not root? – Jim Raynor Jan 07 '15 at 15:18
  • Similarly for python3.x. But when manually installing a new python you don't need the python3.x-dev. – Bobble Jun 22 '15 at 09:26
  • what packages for Amazon EC2? – Vaulstein Feb 05 '16 at 07:57
  • I had to install lib32z1-dev also in Ubuntu 14.04 to get it working actually. – Paris Feb 17 '16 at 11:47
  • 3
    On Ubuntu 16.04 too `zlib1g-dev` is necessary. Without this I got the error `/usr/bin/ld: cannot find -lz`. – Jabba Aug 04 '16 at 16:02
  • You also need pkg-config otherwise it'll fail to find xmlversion.h even if libxml2-dev is installed. – steveayre Aug 06 '16 at 00:59
  • But why doesn't pip handle this - I thought pip handled installing requirements. What is really going on? Or, what else might pip not work with? – nealmcb Nov 18 '16 at 00:06
  • There one sees how complex the world is. And why does pip not handle it? - Because it's not 0install. And why does it not work without being root? - Because too few care for 0install. We are not omniscient and not openminded enough - generic solution to package management seems to remove the reason for existence of distributions when they think their package manager is the one and only. (There is the init system too. Nevertheless distros insist on theirs. My Tip Use both.) So go ahead and use 0install and package programs/other projects only once. We also package electric and CAD projects. – Radagast Nov 27 '16 at 08:53
  • the @Jabba comment should also be in the resolved answer. – Robert Reynolds Jul 09 '19 at 13:13
114

I also had to install lib32z1-dev before lxml would compile (Ubuntu 13.04 x64).

sudo apt-get install lib32z1-dev

Or all the required packages together:

sudo apt-get install libxml2-dev libxslt-dev python-dev lib32z1-dev
Druska
  • 3,982
  • 2
  • 25
  • 30
43

As @Pepijn commented on @Druska 's answer, on ubuntu 13.04 x64, there is no need to use lib32z1-dev, zlib1g-dev is enough:

sudo apt-get install libxml2-dev libxslt-dev python-dev zlib1g-dev
Chris Seymour
  • 75,961
  • 24
  • 144
  • 187
JimmyYe
  • 814
  • 6
  • 6
16

I installed lxml with pip in Vagrant, using Ubuntu 14.04 and had the same problem. Even though all requirements where installed, i got the same error again and again. Turned out, my VM had to little memory by default. With 1024 MB everything works fine.

Add this to your VagrantFile and lxml should properly compile / install:

config.vm.provider "virtualbox" do |vb|
  vb.memory = 1024
end

Thanks to sixhobbit for the hint (see: can't installing lxml on Ubuntu 12.04).

Community
  • 1
  • 1
Chris
  • 601
  • 6
  • 12
10

For Ubuntu 14.04

sudo apt-get install python-lxml

worked for me.

Wade
  • 125
  • 1
  • 7
  • 7
    Well that's bad news. I've gained over 1000 rep from this question, if they have fixed that it will stop getting views ... – Eric Wilson Jul 01 '15 at 09:43
10

Step 1

Install latest python updates using this command.

sudo apt-get install python-dev

Step 2

Add first dependency libxml2 version 2.7.0 or later

sudo apt-get install libxml2-dev

Step 3

Add second dependency libxslt version 1.1.23 or later

sudo apt-get install libxslt1-dev

Step 4

Install pip package management tool first. and run this command.

pip install lxml

If you have any doubt Click Here

Community
  • 1
  • 1
Simplans
  • 1,993
  • 1
  • 20
  • 22
8

After installing the packages mentioned by AKX I still had the same problem. Solved it with

apt-get install python-dev
Chris Seymour
  • 75,961
  • 24
  • 144
  • 187
bcoughlan
  • 23,518
  • 17
  • 81
  • 132
6

For Ubuntu 12.04.3 LTS (Precise Pangolin) I had to do:

apt-get install libxml2-dev libxslt1-dev

(Note the "1" in libxslt1-dev)

Then I just installed lxml with pip/easy_install.

gonz
  • 4,746
  • 3
  • 36
  • 54
2

From Ubuntu 18.4 (Bionic Beaver) it is advisable to use apt instead of apt-get since it has much better structural form.

sudo apt install libxml2-dev libxslt1-dev python-dev

If you're happy with a possibly older version of lxml altogether though, you could try

sudo apt install python-lxml
Rarblack
  • 4,160
  • 4
  • 17
  • 31
1

Many answers here are rather old,
thanks to the pointer from @Simplans (https://stackoverflow.com/a/37759871/417747) and the home page...

What worked for me (Ubuntu bionic):

sudo apt-get install python3-lxml  

(+ sudo apt-get install libxml2-dev libxslt1-dev I installed before it, but not sure if that's the requirement still)

NSGaga-mostly-inactive
  • 13,264
  • 3
  • 38
  • 50
  • 1
    Yeah, old answers on an old question. I mean, I was on Ubuntu 11 when I asked! – Eric Wilson Mar 27 '19 at 12:41
  • yes, I doubt you'd even be able to ask it here any more (would be moved over to ubuntu etc.) but it's still atrackting lot of web search. And doubt anyone is still using Ubuntu 11 :). And then that old linux versions and packages in-compatibility, hope that improves one day. – NSGaga-mostly-inactive Mar 27 '19 at 20:13
0

First install Ubuntu's python-lxml package and its dependencies:

sudo apt-get install python-lxml

Then use pip to upgrade to the latest version of lxml for Python:

pip install lxml
Alf Eaton
  • 4,344
  • 3
  • 35
  • 43
Joseph
  • 118
  • 5
  • code only answers are frowned upon. Add some explanation – Manos Nikolaidis Oct 14 '15 at 11:34
  • While this answer may be correct, please add some explanation. Imparting the underlying logic is more important than just giving the code, because it helps the OP and other readers fix this and similar issues themselves. – CodeMouse92 Oct 14 '15 at 17:00
  • Downvoting because nothing new is added to a question that has several good answers already. – Eric Wilson Oct 14 '15 at 19:49