135

I'm new to Linux and R.

I installed R 2.12 in Ubuntu 11.04. Today I tried to install a new package, so I ran the following command:

install.packages('XML')

But the installation failed and gave the following information:

* installing *source* package ‘XML’ ...
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... 
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
No ability to remove finalizers on externalptr objects in this verison of R
checking for sed... /bin/sed
checking for pkg-config... /usr/bin/pkg-config
checking for xml2-config... no
Cannot find xml2-config
ERROR: configuration failed for package ‘XML’
* removing ‘/home/spirit/R/i686-pc-linux-gnu-library/2.12/XML’

The downloaded packages are in
    ‘/tmp/RtmpoZYxnv/downloaded_packages’
Warning message:
In install.packages("XML") :
  installation of package 'XML' had non-zero exit status

I also tried:

sudo install.packages('XML')

But it gives the same error information.

Anyone can give me any advice?

Mukyuu
  • 6,436
  • 8
  • 40
  • 59
Derrick Zhang
  • 21,201
  • 18
  • 53
  • 73
  • Install `xml2-config`? – Wayne Werner Oct 18 '13 at 21:54
  • 10
    This question just helped me (again) in Ubuntu 14.04 – geneorama Mar 22 '15 at 22:24
  • 2
    I don't know about the question being off-topic or not, but the answer below also solved my problem with Ubuntu Trusty Tahr. – msoftrain Dec 04 '15 at 15:17
  • Should this be moved to the AskUbuntu stack? It is a question relating to using R or RStudio (though it probably occurs with other software?), caused by lack of required software packages in Ubuntu. It's a common enough question it seems according to the "Related" list I see at the right. And it could rightly be asked why are these packages missing from Ubuntu? – Rick Henderson Mar 10 '16 at 02:18
  • 1
    @rickHenderson It's also useful for other Linux distros. –  Feb 26 '17 at 05:40

2 Answers2

224

The install.packages method

You need to install the ubuntu package libxml2-dev So in a shell prompt type:

sudo apt-get update
sudo apt-get install libxml2-dev

You will need special sudo powers for this.

Ubuntu package method

As Richie and Dirk mentioned, you can also use:

sudo apt-get install r-cran-xml

If you go down this path, I would recommend that you check out the R ubuntu sources page which will ensure you have a current version of R and the associated R packages. This could be important if you are using the LTS version of ubuntu.

csgillespie
  • 59,189
  • 14
  • 150
  • 185
  • 1
    Can you try synaptic? look for the package you need there – pacomet Oct 14 '11 at 11:19
  • 1
    r-cran-xml is not enough, had to do libxml2-dev – Ferroao Aug 28 '17 at 01:35
  • Sharing my observation for a similar issue which might help MAC users. Even though i had all the dependent libraries i still got errors. After a lot a lot of trials this is what saved me- While installing via rstudio using install.packages(), you get a prompt asking -Do you want to install from sources the package which needs compilation? y/n: Give your response here as 'n'. This worked for me. – minion Apr 06 '18 at 05:42
  • In Debian stretch only `sudo apt-get install libxml2-dev` is enough to work – Adriano Rivolli Aug 28 '18 at 13:18
  • I am getting this error: sudo apt-get install r-cran-xml – Suat Atan PhD Oct 07 '19 at 17:38
  • Fedora users...: `dnf install libxml2-devel` – algae Jan 14 '20 at 04:26
29

The answer by Colin is correct in a narrow sense, but at the same time wrong as you can just say

 sudo apt-get install r-cran-xml

as XML is one of the CRAN packages available in Ubuntu. Do apt-cache search r-cran-* do see the others.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725