I have successfully installed RMySQL 0.9-3 from source for Mac:
- OS X Mavericks 10.9.4
- R 3.1.0 and RStudio 0.98.945 installed from CRAN and rstudio.com
- Xcode 5.1.1 and the corresponding command-line tools
There are alternatives to achieve the same goal, but I will try to provide the friendliest way. Here are the major steps:
1. Make sure you have "gcc" available. (looks like you do)
Go to Applications > Utilities, open Terminal, and type the following command to verify the command line tools exist: ($ is the command prompt in Terminal)
$ gcc
clang: error: no input files
If you receive -bash: gcc: command not found
, you will need to install the command-line tools (gcc) in the standalone or Xcode way.
2. Install MySQL client via Homebrew.
Homebrew is a package management system that simplifies the installation of software on the Mac OS X operating system.
First, Install Homebrew from Terminal:
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
After that, type "brew" command to check if Homebrew is installed. Then install MySQL via Homebrew:
$ brew install mysql
You should see the Downloading, Pouring, Caveats, and finally the Summary showing that MySQL is installed.
You can verify the installation by connecting to the local MySQL server as root with (default) empty password:
$ mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.6.19 Homebrew
... (skipped) ...
mysql>
Use "exit" to exit the MySQL shell. You may have problems starting or connecting to your local MySQL server, but that's beyond the scope.
3. Install RMySQL from source in RStudio.
So far, all the steps are done in Terminal. Although this step can also be done in Terminal, I will show how to do it in RStudio. From your error message:
Configuration error:
could not find the MySQL installation include and/or library
directories. Manually specify the location of the MySQL
libraries and the header files and re-run R CMD INSTALL.
INSTRUCTIONS:
1. Define and export the 2 shell variables PKG_CPPFLAGS and
PKG_LIBS to include the directory for header files (*.h)
and libraries, for example (using Bourne shell syntax):
export PKG_CPPFLAGS="-I<MySQL-include-dir>"
export PKG_LIBS="-L<MySQL-lib-dir> -lmysqlclient"
Re-run the R INSTALL command:
R CMD INSTALL RMySQL_<version>.tar.gz
This means that R can't find the header files (include things) to include and the libraries (lib things) to link to.
The instruction tells you to set 2 environment variables PKG_CPPFLAGS and PKG_LIBS to indicate where the include and lib are.
Assume you have installed MySQL with the default paths in Homebrew. Within RStudio, you can set them by: (> is the command prompt in RStudio)
### These are the KEY COMMANDS in this turotial ###
> Sys.setenv(PKG_CPPFLAGS = "-I/usr/local/include/mysql")
> Sys.setenv(PKG_LIBS = "-L/usr/local/lib -lmysqlclient")
Finally, you should be able to install RMySQL from source correctly! Either from CRAN repo or the local file.
> install.packages("RMySQL", type = "source")
or
> install.packages("/path/to/package/RMySQL_0.9-3.tar.gz", repos = NULL, type = "source")
Both will give you the success message:
** libs
clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include/mysql/ -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -fPIC -Wall -mtune=core2 -g -O2 -c RS-DBI.c -o RS-DBI.o
clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include/mysql/ -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -fPIC -Wall -mtune=core2 -g -O2 -c RS-MySQL.c -o RS-MySQL.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/lib -o RMySQL.so RS-DBI.o RS-MySQL.o -L/usr/local/lib/ -lmysqlclient -lz -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
installing to /Library/Frameworks/R.framework/Versions/3.1/Resources/library/RMySQL/libs
** R
** inst
** preparing package for lazy loading
Creating a generic function for ‘format’ from package ‘base’ in package ‘RMySQL’
Creating a generic function for ‘print’ from package ‘base’ in package ‘RMySQL’
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (RMySQL)
As usual, load the RMySQL package by:
> library(RMySQL)
Loading required package: DBI
>
Note: The install.packages()
in R actually runs R CMD INSTALL xxx
in the Terminal (Unix shell) environment. So if you prefer the Terminal way, you can also set PKG_CPPFLAGS and PKG_LIBS by the export command in Terminal, and run R CMD INSTALL RMySQL_xxx.tar.gz
to install from the source package you have manually downloaded.
So the following way will also work in Terminal for Step 3:
$ export PKG_CPPFLAGS="-I/usr/local/include/mysql"
$ export PKG_LIBS="-L/usr/local/lib -lmysqlclient"
$ R CMD INSTALL RMySQL_0.9-3.tar.gz