Adding SCP/SFTP to curl & RCurl on Mac OS X

Update: As of 2019-01-17, Homebrew curl no longer supports the –with-libssh2 option. Instead, curl needs to be downloaded and compiled from source with the flag. The instructions below are incomplete.


RCurl provides wrapper R functions for most protocols supported by libcurl. I like to use the RCurl::scp() function to read files from remote servers, but every time I try to use it on a new Mac, I’m reminded that the OS X system curl is not built with SCP/SFTP support, and therefore, the corresponding R functions won’t work.

To get RCurl working with SCP/SFTP, we first need to install a new version of curl built with libssh2.


1. Install Homebrew

If you’re not already using Homebrew on your Mac, I recommend checking it out. It makes installing/removing software super simple.

You should probably go to the Homebrew site for installation instructions, but if you want to take my word for it, you can install Homebrew like this…

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2. Install libssh2 and curl

brew install libssh2
brew install curl --with-libssh2

3. Install RCurl with the new curl

Outside of RCurl, I don’t really need a new version of curl with scp/sftp support, and overwriting the system curl is probably a bad idea anyways. Instead, when we install RCurl, you can temporarily add the new curl to your $PATH.

Sys.setenv(PATH=paste('/usr/local/opt/curl/bin', Sys.getenv('PATH'), sep=":"))
install.packages("RCurl", type="source")

Finally, you can make sure it worked by checking the available protocols…

> library(RCurl)
> curlVersion()$protocols
 [1] "dict"   "file"   "ftp"    "ftps"   "gopher" "http"   "https"  "imap"
 [9] "imaps"  "ldap"   "ldaps"  "pop3"   "pop3s"  "rtsp"   "scp"    "sftp"
[17] "smb"    "smbs"   "smtp"   "smtps"  "telnet" "tftp"