1

I am trying to

install.packages("odbc")

inside an R 4.2.2 kernel running in anaconda, in WSL (Ubuntu-22.04 distro). The reason I am running anaconda in WSL rather than in Windows is because I need R=4.2.2, and the Windows R base on anaconda is a bit behind at 4.1.3 (see https://anaconda.org/conda-forge/r-base)

I can install.packages("odbc") on a Windows install of anaconda, just not on an anaconda installation on Ubuntu in WSL.

When running install.packages("odbc")

I get this error:

trying URL 'https://cran.stat.auckland.ac.nz/src/contrib/odbc_1.3.4.tar.gz'
Content type 'application/x-gzip' length 338480 bytes (330 KB)
==================================================
downloaded 330 KB

* installing *source* package ‘odbc’ ...
** package ‘odbc’ successfully unpacked and MD5 sums checked
** using staged installation
PKG_CFLAGS=
PKG_LIBS=-lodbc
<stdin>:1:10: fatal error: sql.h: No such file or directory
compilation terminated.
------------------------- ANTICONF ERROR ---------------------------
Configuration failed because odbc was not found. Try installing:
 * deb: unixodbc-dev (Debian, Ubuntu, etc)
 * rpm: unixODBC-devel (Fedora, CentOS, RHEL)
 * csw: unixodbc_dev (Solaris)
 * pacman: unixodbc (Archlinux, Manjaro, etc)
 * brew: unixodbc (Mac OSX)
To use a custom odbc set INCLUDE_DIR and LIB_DIR and PKG_LIBS manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=... PKG_LIBS=...'
--------------------------------------------------------------------
ERROR: configuration failed for package ‘odbc’
* removing ‘/home/toma/anaconda3/envs/r_422/lib/R/library/odbc’

The downloaded source packages are in
        ‘/tmp/RtmpzR29mc/downloaded_packages’
Updating HTML index of packages in '.Library'
Making 'packages.html' ... done
Warning message:
In install.packages("odbc") :
  installation of package ‘odbc’ had non-zero exit status

I have done the suggested sudo apt-get install unixodbc unixodbc-dev, and have these versions:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
unixodbc-dev is already the newest version (2.3.9-5).
unixodbc is already the newest version (2.3.9-5).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

This didn't work, so I followed these instructions from https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server

if ! [[ "18.04 20.04 22.04" == *"$(lsb_release -rs)"* ]];
then
    echo "Ubuntu $(lsb_release -rs) is not currently supported.";
    exit;
fi

sudo su
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -

curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list > /etc/apt/sources.list.d/mssql-release.list

exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y apt-get install -y mssql-tools18
echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo apt-get install -y unixodbc-dev

I still get the earlier error about missing sql.h, however this file does exist in

~anaconda3/pkgs/unixodbc-2.3.11-h5eee18b_0/include/sql.h
~anaconda3/include/sql.h

Any ideas?

  • If using Conda R, then it is usually better to avoid `install.packages` for anything other than pure R packages. The CRAN `odbc` package goes by `r-odbc` on Conda Forge. See https://stackoverflow.com/a/60767530/570918. Conda will bring all the dependencies with it, and everything is precompiled (only needs copying). – merv Feb 21 '23 at 23:25

1 Answers1

0

as the conf error states: Configuration failed because odbc was not found (!) . So you have the needed dependencies installed (twice); but your R session can't access them, because the PATH vars are not provided. This could be due to the way your conda envs are set up. It seems you want to use r_422 at ‘/home/toma/anaconda3/envs/r_422'

Point R to the unixODBC installation by providing the path during or prior of the install.

As the message suggests, you could call R from bash in following fashion :

R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=... PKG_LIBS=...

...or from inside of R:

install.packages("odbc", configure.vars=c("LIB_DIR='r_422/lib/R/library/' INCLUDE_DIR='anaconda3/pkgs/unixodbc-2.3.11-h5eee18b_0/include/sql.h''"))

... or add the path to .Renviron file, and restart R

echo "INCLUDE_DIR='anaconda3/pkgs/unixodbc-2.3.11-h5eee18b_0/include/sql.h'" > .Renviron

... with the right paths to unixODBC (either the one that you manually installed into conda pkgs or the system wide version you installed through apt) To find the location of the apt version of odbc you could use dpkg list:

dpkg -L unixodbc

This will give you a list of the source files of unixodbc.

user12256545
  • 2,755
  • 4
  • 14
  • 28
  • Thanks. The installation gets further now, but some secondary process now can't access the PATH: /home/toma/anaconda3/envs/r_422/bin/../lib/gcc/x86_64-conda-linux-gnu/12.2.0/../../../../x86_64-conda-linux-gnu/bin/ld: cannot find -lodbc: No such file or directory collect2: error: ld returned 1 exit status make: *** [/home/toma/anaconda3/envs/r_422/lib/R/share/make/shlib.mk:10: odbc.so] Error 1 ERROR: compilation failed for package ‘odbc’ * removing ‘/home/toma/anaconda3/envs/r_422/lib/R/library/odbc’ – EuginePickett Feb 21 '23 at 21:00
  • how experienced are you with conda envs? maybe it is easier if you would just use apt and handle envs with multiple WSL instances instead? – user12256545 Feb 21 '23 at 21:35
  • I'm pretty good with conda - but completely new to R. That is why I'm using conda - so I can switch from my comfort zone of python to this strange R beast. Ideally I can write my python code and R code both in jupyter-lab, and just switch kernels – EuginePickett Feb 21 '23 at 22:23