Unable to install some R packages in Ubuntu 20.04 because of internal compiler error: Segmentation fault - r

I've recently installed R version 4.0.5 (2021-03-31) onto Ubuntu 20.04.2 LTS. R is working as expected.
However, while some packages have installed without issue (e.g., R.matlab), there are several packages that have not been able to install. As as example, running install.packages("data.table") throws the following error:
* installing *source* package ‘data.table’ ...
** package ‘data.table’ successfully unpacked and MD5 sums checked
** using staged installation
gcc -std=gnu99 9.3.0
zlib 1.2.11 is available ok
R CMD SHLIB supports OpenMP without any extra hint
** libs
gcc -std=gnu99 -I"/usr/share/R/include" -DNDEBUG -fopenmp -fpic -g -O2 -fdebug-prefix-map=/build/r-base-tRgc13/r-base-4.0.5=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c assign.c -o assign.o
during GIMPLE pass: ccp
assign.c: In function ‘memrecycle’:
assign.c:1205:1: internal compiler error: Segmentation fault
1205 | }
| ^
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-9/README.Bugs> for instructions.
make: *** [/usr/lib/R/etc/Makeconf:172: assign.o] Error 1
ERROR: compilation failed for package ‘data.table’
I get similar errors (i.e., "internal compiler error") with other package installation attempts, but with slightly different output. For example, install.packages("xfun") throws the following error, truncated for brevity:
gcc -std=gnu99 -I"/usr/share/R/include" -DNDEBUG -fpic -g -O2 -fdebug-prefix-map=/build/r-base-tRgc13/r-base-4.0.5=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c base64.c -o base64.o
during GIMPLE pass: ccp
base64.c: In function ‘base64_decode_impl’:
base64.c:237:1: internal compiler error: Segmentation fault
237 | }
| ^
I have tried reinstalling gcc-9, which has not helped. I had found that the PATH in Sys.getenv("PATH") was pointing to a miniconda3 install on my system, so updated that to /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/rstudio/bin/postback, which also has not helped.
My goal is to install packages among those above without throwing these compiler errors. Any help would be greatly appreciated.

In case others also run into this issue in the future, I am posting the solution that was suggested to me by https://answers.launchpad.net/ubuntu. Here is the link to the question I posted: https://answers.launchpad.net/ubuntu/+question/696623.
The issue turned out to be that R was using gcc-9 rather than gcc-10 to compile packages. The older version of gcc was throwing an error. Here are the steps I took to solve the problem:
Install gcc-10, which was not available on my system: sudo apt install gcc-10.
Edit the CC= pointer in the /usr/lib/R/etc/Makeconf file to gcc-10: open Terminal, type sudo nano /usr/lib/R/etc/Makeconf and replace the current CC= to CC=gcc-10. Save the file.
Restart R and run the install.packages() command for those packages that were not compiling correctly.
EDIT: Please see the comments below for a discussion. The above steps resolve the issue, but are not recommended. The issue was related to R not using the system package manager to install packages when the call install.packages() was used in R.
Installing the package bspm solved the issue for me. This package and its utility is discussed here for those who are curious.
To use install.packages() within R, bspm can be used in two ways:
bspm::enable() within R and then install.packages()
As written in its documentation: To enable bspm system-wide by default, include the following: suppressMessages(bspm::enable()) into the Rprofile.site file.
Thanks very much to Dirk for his guidance.

Related

R cannot find g77

I am trying to install from Cran the R package "fGarch", using install.packages('fGarch') I am on Ubuntu, I have r-base-dev and r-base-core installed.
But the install fails because:
77 -fno-optimize-sibling-calls -fpic -g -O2 -fdebug-prefix-map=/build/r-base-ttHamR/r-base-4.0.2=. -fstack-protector-strong -c dist.f -o dist.o
/bin/bash: 77: command not found
/usr/lib/R/etc/Makeconf:190: recipe for target 'dist.o' failed
make: *** [dist.o] Error 127
I have gfortran, gcc, g++. I also have something called "f77".
which f77
/usr/bin/f77
But nothing simply called "77". I do not understand what R is trying to call with "77".
I also installed the package from apt-get, but this then returns an error in R saying the version is too old and I should reinstall it.
Thank you for any help.

Prepare CRAN R package with external dependencies (nlopt)

I am trying to submit a package to CRAN that fails during the pretest
process on Debian.
I use some C++ code that interfaces to the nlopt optimization
library with Rcpp/RcppArmadillo (using <nlopt.hpp>). Thus, my package requires a system version of nlopt that can be installed via the deb package libnlopt-dev (I added this to the SystemRequirements field).
I use a configure script and pkg-config to retrieve the (hopefully)
appropriate compiler flags which I then send to src/Makevars
NLOPT_LIBS=`pkg-config --libs ${PKG_CONFIG_NAME}`
NLOPT_FLAG=`pkg-config --cflags ${PKG_CONFIG_NAME}`
This approach works on my system (Ubuntu 18.04, R 3.5.2) and on Travis
(linux and mac OS), but fails on the Debian CRAN server :
https://win-builder.r-project.org/incoming_pretest/PLNmodels_0.7_20190119_161032/Debian/00install.out
I added some verbosity to check that the flags were correctly exported
on CRAN servers, and they seems alright to me: at least, a version of
nlopt is found on the system (see below, NLOPT_LIBS=-lnlopt -lm).
However, the <nlopt.hpp> is not found latter on...
Any help would be greatly appreciated. Thank for taking the time to read this.
* installing *source* package ‘PLNmodels’ ...
Using NLOPT_LIBS=-lnlopt -lm
Using NLOPT_FLAG=
** libs
g++-8 -std=gnu++11 -I"/home/hornik/tmp/R/include" -DNDEBUG -fopenmp
-I"/home/hornik/lib/R/Library/3.6/x86_64-linux-gnu/Rcpp/include"
-I"/home/hornik/lib/R/Library/3.6/x86_64-linux-gnu/RcppArmadillo/include"
-I/usr/local/include -fopenmp -fpic -g -O2 -Wall -pedantic
-mtune=native -c RcppExports.cpp -o RcppExports.o
g++-8 -std=gnu++11 -I"/home/hornik/tmp/R/include" -DNDEBUG -fopenmp
-I"/home/hornik/lib/R/Library/3.6/x86_64-linux-gnu/Rcpp/include"
-I"/home/hornik/lib/R/Library/3.6/x86_64-linux-gnu/RcppArmadillo/include"
-I/usr/local/include -fopenmp -fpic -g -O2 -Wall -pedantic
-mtune=native -c call_nlopt_PLN_VE.cpp -o call_nlopt_PLN_VE.o
In file included from call_nlopt_PLN_VE.cpp:6:
utils.h:5:10: fatal error: nlopt.hpp: No such file or directory
#include <nlopt.hpp>
^~~~~~~~~~~
compilation terminated.
make: *** [/home/hornik/tmp/R/etc/Makeconf:173: call_nlopt_PLN_VE.o] Error 1
ERROR: compilation failed for package ‘PLNmodels’
* removing ‘/srv/hornik/tmp/CRAN/PLNmodels.Rcheck/PLNmodels’
The C++ interface for nlopt has migrated to a separate package in Debian, c.f. https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=855600. So it looks like that particular CRAN machine uses either Debian Testing or Stable + Backports and has only libnlopt-dev but not libnlopt-cxx-dev installed. You will have to contact the CRAN maintainers to install this dependency.
BTW, ideally such errors should be cought in the configure script, i.e. you should try to compile a simple program that requires nlopt.hpp.
You asked the same question here on r-package-devel and I had no good answer for you.
In essence, you have fine variation within one required upstream package. That is very delicate. I cannot think of anything but what #Ralf suggested: test for it.
Or maybe you just fail in communicating the requirements of your package to CRAN. But that would be an even simpler question, and when the wrong version is encountered, you are bound to fail installation.
So overall, my recommendation is the same as previously: work with Jelmer, and wait for him to release an updated nloptr release. Until then, all solutions are bound to be fudges as you cannot control what version of nloptr your package may encounter.

Preparing for CRAN: '-fopenmp' clang error?

I am preparing a package (which makes use of Rcpp and RcppArmadillo) for submission to CRAN. I am using Travis CI and I have included the following to test my package more thoroughly:
os:
- linux
- osx
compiler:
- clang
- gcc
This, however, yields the (familiar, it seems) error when using clang on OS X
* installing *source* package ‘my_pkg’ ...
** libs
clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I"/Users/travis/R/Library/Rcpp/include" -I"/Users/travis/R/Library/RcppArmadillo/include" -I/usr/local/include -fopenmp -fPIC -Wall -g -O2 -c RcppExports.cpp -o RcppExports.o
clang: error: unsupported option '-fopenmp'
make: *** [RcppExports.o] Error 1
ERROR: compilation failed for package ‘pkg’
* removing ‘/Users/travis/build/my_user/pkg/pkg.Rcheck/pkg’
By appropriately adjusting my travis.yml file to adhere to one of the workarounds suggested here, it seems like I can avoid the issue.
My question is: what should I do if I want to put this on CRAN? Jim's suggestions seems to me only local, do I have to do something in my package to automatically avoid the compile issue for the user in order to be accepted by CRAN?
Edit: To be a little bit more specific, could I just put CXX=g++ in Makevars, or is that frowned upon (and would that be sufficient)?
So ... one step at a time, please. Which RcppArmadillo version are you pulling in here? Which clang version are you pulling in? Where is that clang version from? Does it have OpenMP support?
Starting with the previous release of RcppArmadillo, we ensured we simply turn OpenMP off on macOS as the build support is too fragile, see this PR on GitHub. So you may have to remove unconditional use of -fopenmp in your files, or simple not use that "cell" of your Matrix (ie clang && macOS).
Otherwise, the answer by Jim Hester is spot-on. Things are just more work on macOS.

Mac OS X R error "ld: warning: directory not found for option"

I am trying to install an R package from source, but getting an error:
* installing *source* package ‘mclust’ ...
** package ‘mclust’ successfully unpacked and MD5 sums checked
** libs
gfortran-4.8 -fPIC -g -O2 -c mclust.f -o mclust.o
gfortran-4.8 -fPIC -g -O2 -c mclustaddson.f -o mclustaddson.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o mclust.so mclust.o mclustaddson.o -L/Library/Frameworks/R.framework/Resources/lib -lRlapack -L/Library/Frameworks/R.framework/Resources/lib -lRblas -L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2 -lgfortran -lquadmath -lm -L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2 -lgfortran -lquadmath -lm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: warning: directory not found for option '-L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2'
ld: warning: directory not found for option '-L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2'
ld: library not found for -lquadmath
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mclust.so] Error 1
ERROR: compilation failed for package ‘mclust’
* removing ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library/mclust’
Warning in install.packages :
installation of package ‘mclust’ had non-zero exit status
I don't have /usr/local/lib/gcc/x86_64-apple-darwin13.0.0, so it makes sense that it can't be found. I have /usr/local/lib/gcc/i686-apple-darwin11 and /usr/local/lib/gcc/4.8 (symlink to Homebrew installation). Where is it getting x86_64-apple-darwin13.0.0 from?
There are a lot of references to a similar error online. However, all of them are related to compiling in Xcode and resolved by updating project settings, which is not applicable here.
You need to modify the ~/.R/Makevars file. For a greater overview of this see: https://cran.r-project.org/doc/manuals/r-release/R-admin.html#OS-X-packages
Alternatively, this has been answered before in a bit more depth by #kevin-ushey in Rcpp warning: "directory not found for option '-L/usr/local/Cellar/gfortran/4.8.2/gfortran'".
What is happening is your code is not being run under gcc instead it is being forwarded to clang
You will need to change your compile statements in ~/.R/Makevars/ to gcc using:
VER=-5.3.0
CC=gcc$(VER)
CXX=g++$(VER)
CFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
CXXFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
FLIBS=-L/usr/local/Cellar/gcc/5.3.0/lib/gcc/5
This assumes you have already installed gcc via homebrew under:
brew install gcc
(gfortran ships with gcc in brew now)
Incorporating previous solutions with additional help from the comments, the following solution has worked for me on Mac OS X High Sierra.
Create/edit ~/.R/Makevars with the following contents:
VER=-8
CC=gcc$(VER)
CXX=g++$(VER)
CXX11=g++$(VER)
CXX14=g++$(VER)
CXX17=g++$(VER)
CFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
CXXFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
FLIBS=-L/usr/local/Cellar/gcc/8.2.0/lib/gcc/8
Note, I am using homebrew and have gcc version 8.2.0 installed.
From http://thecoatlessprofessor.com/programming/rcpp-rcpparmadillo-and-os-x-mavericks-lgfortran-and-lquadmath-error/ you can fix this by downloading the optional gfortran libraries from http://r.research.att.com/libs/ and extracting them. To do this on the command line
curl -O http://r.research.att.com/libs/gfortran-4.8.2-darwin13.tar.bz2
sudo tar fvxz gfortran-4.8.2-darwin13.tar.bz2 -C /
I had this issue for a long time, working on a Mac. Following the other answers on this page and other questions, I did the following and it finally worked!
Step 1 - brew install gcc (it installed gcc-11 for me. If this is different for you in the future, replace gcc-11 below with the corresponding version. You can find the version by checking in the folder /usr/local/bin and seeing the latest gcc version file there)
Step 2 - xcode-select --install (this installs Xcode CLI)
Step 3 - ln -sf /usr/local/bin/gcc-11 /usr/local/bin/gcc (this makes a new gcc symlink under /usr/local/bin/. Source)
Step 4 - I did not have a ~/.R/Makevars file. Heck, I didn't even have the ~./R folder. Made the folder and the file. Here is what I wrote in the Makevars file (remember to change the GCC version in the first and last line according to the version you have. Also remember to not have the entire version number that's in the last line, in the first line. Just the overall version number - 11 in my case) -
VER=-11
CC=gcc$(VER)
CXX=g++$(VER)
CXX11=g++$(VER)
CXX14=g++$(VER)
CXX17=g++$(VER)
CFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
CXXFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
FLIBS=-L/usr/local/Cellar/gcc/11.2.0_3/lib/gcc/11
Now restart, and voila! It works.
I'm working on MacOS Mojave (10.14.5) and on R 4.0.0. The problem here is that "CRAN R 4.0.0 builds and higher no longer use any custom compilers" (see here), so the Makevars solution does not appear to work anymore.
The solution for me was to download and install the GNU Fortran compiler from the official R-Project website. Note that you will also need Xcode and Xcode command-line tools.
After running the installer with default settings, compilation of gfortran code worked without problems.
I received the same error on MAC. All I needed to do was to install gfortran-6.1.pkg from https://cran.r-project.org/bin/macosx/tools/. Make sure that the package is installed under /usr/local/gfortran so it can be found by R.
Alternatively you can install it via homebrew typing brew cask install gfortran in the terminal (it may ask for your password).
In my case I combined this answer with this one, to produce the following code in the ~./R/.Makevars-file.
touch ~./R/.Makevars
(because it didn't exist there)
open -a BBEdit ~./R/.Makevars
(I use BBEdit as text editor)
Added the following lines to the Makevars-file:
VER=-11
CC=gcc$(VER)
CXX=g++$(VER)
CFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
CXXFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
# FLIBS=-L/usr/local/Cellar/gcc/11.1.0_1/lib/gcc/11
# FLIBS = -L`gfortran -print-file-name=libgfortran.dylib | xargs dirname`
FLIBS=`gfortran -print-search-dirs | grep ^libraries: | sed 's|libraries: =||' | sed 's|:| -L|g' | sed 's|^|-L|'`
These two lines were suggested by #KevinUshy.
# FLIBS=-L/usr/local/Cellar/gcc/11.1.0_1/lib/gcc/11
# FLIBS = -L`gfortran -print-file-name=libgfortran.dylib | xargs dirname`
I commented these out, as I figure that the final line would probably work best.
Two notes.
I have brew installed and I used brew to install gcc using the command brew install gcc.
I figure out the version-naming using brew info gcc, which gave me 11.1.0_1, but it is linked as gcc-11, so VER=-11 in the .Makevars-file.
Hope this helps others.
I am having Mac OS Catalina and in my case installation of Homebrew, the newest gcc and Gfortran 8.2.0 solved the issue.
The solution was to re-install problematic packages with Homebrew.
$ brew uninstall --ignore-dependencies --force openssl
$ brew install openssl
$ brew uninstall --ignore-dependencies --force readline
$ brew install readline
Digging this topic, if you tried these and much others approaches related to this error and got no success, I strongly recommend you to check if your OSx - just in case - deleted the .zshrc file.
If that's the case, you should create again this file in the root dir. type in your terminal:
touch .zshrc
followed by
openssl_prefix=$(brew --prefix openssl) printf "CFLAGS=-I$openssl_prefix/include \nLDFLAGS=-L$openssl_prefix/lib"
example of output:
CFLAGS=-I/usr/local/opt/openssl#3/include LDFLAGS=-L/usr/local/opt/openssl#3/lib
add them to the zshrcfile content, run
source .zshrc
to refresh the file and that`s all!
Had this problem using R on macos 12.4. It was caused by old and stale entries in my ~/.R/Makevars file. Once I commented everything out everything compiled just fine. I have XCode and brew install gcc active.
For later versions of R (R 4.0 and higher) and later operating systems, the recommended fix for this problem seems to be to install gfortran via rtools (see here, already mentioned in the answer of #lks_swrx).
When following these instructions, I ran into an additional problem, since my computer uses Apple Silicon chips (not Intel anymore), for which I wanted to add a solution here:
Download the appropriate gfortran tarball from rtools
Run tar fxz gfortran-12.0.1-20220312-is-darwin20-arm64.tar.xz -C /
Add it to your path by adding this line to your .zshrc file:
nano .zshrc (or whatever you use as text editor)
export PATH=$PATH:/opt/R/arm64/gfortran/bin
So far, so good; but the installation of DESeq2, which brought me here, did still not work, because R could not find the library. This is the fix:
Create a symlink to the gfortran library in /usr/local, so that R can find it:
ln -s /opt/R/arm64/gfortran /usr/local/gfortran (might require sudo)

How can I install topicmodels package in R?

I am trying to install the package called topicmodels in R and I have not had success. Here's what I have tried...
Action:
Install the package using install.packages("topicmodels")
Result:
package ‘topicmodels’ is available as a source package but not as a binary
Warning in install.packages :
package ‘topicmodels’ is not available (for R version 3.1.0)
So there I said okay let's install from source
Action:
install.packages("/Users/my_name/Downloads/topicmodels_0.2-1.tar.gz",repos=NULL,type="source")
Result:
* installing *source* package ‘topicmodels’ ...
** package ‘topicmodels’ successfully unpacked and MD5 sums checked
** libs
clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -fPIC -Wall -mtune=core2 -g -O2 -c cokus.c -o cokus.o
clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -fPIC -Wall -mtune=core2 -g -O2 -c common.c -o common.o
clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -fPIC -Wall -mtune=core2 -g -O2 -c ctm.c -o ctm.o
ctm.c:29:10: fatal error: 'gsl/gsl_rng.h' file not found
include gsl/gsl_rng.h
1 error generated.
make: *** [ctm.o] Error 1
ERROR: compilation failed for package ‘topicmodels’
* removing ‘/Library/Frameworks/R.framework/Versions/3.1/Resources/library/topicmodels’
Warning in install.packages :
installation of package ‘/Users/me/Downloads/topicmodels_0.2-1.tar.gz’ had non-zero exit status
So then I researched this gsl thing and came upon this link. And for the moment I thought all of my problems were gone and when I finally follow these directions. I get the following error (in the terminal)...
Warning in untar2(tarfile, files, list, exdir, restore_times) :
using pax extended headers
ERROR: cannot extract package from ‘topicmodels.tar.gz’
First of all you have to install gsl. Once you have that installed you can try to install the R package. You can download gsl from here (there you can pick the latest version gsl-latest.tar.gz). Once it's been downloaded, install it by doing the following:
Decompress the downloaded file (in my case the "latest" file contained this version: gsl-1.16)
Open the "Terminal"
Then (using the Terminal), move inside the folder that you created on step 1. In my case I had the file on the folder called "Downloads", so I moved to the newly created folder by doing:
cd Downloads/gsl-1.16
Once you are within that folder run the following commands (in order):
./configure
make
sudo make install
After doing it you won't get the previous error saying fatal error: 'gsl/gsl_rng.h' file not found, so you can try again the installation.
Now go back to your R environment (e.g., RStudio) to try again to install the package by doing:
install.packages("PATH_TO_TOPIC_MODELS.tar.gz", repos=NULL, type="source").
I had the same problem, and after doing this I got the R package correctly installed, I hope it also works in your case.
If you already using homebrew. It is better and faster to fix with variables than installing gsl manually from source.
Install gsl with homebrew brew install gsl
Edit ~/.R/Makevars and add.
PKG_LIBS=-L/usr/local/opt/gettext/lib
CFLAGS=-I/usr/local/opt/gsl/include
LDFLAGS=-L/usr/local/opt/gsl/lib -lgsl -lgslcblas
If you are using Linux based on Debian, that's my solution:
Using terminal run sudo apt install gsl-bin libgsl-dbg libgsl-dev libgsl25 libgslcblas0;
Then, using R console, run install.packages("topicmodels",dependencies =T);
Enjoy!!

Resources