64 bit R on Mac

R comes in both 32 and 64 bit versions. The 32-bit R is the standard version and works just fine in most cases. However, if you are working with very large (e.g., 1 GB or more) datasets, you are going to be much better off using 64-bit R. Your operating system determines whether you have access to 32 or 64 bit computing. Luckily, Mac OS’s can do either, so it just depends on the particular program.

Why use 64-bit R? For 32-bit programs, you can only access up to about 3.5Gb of RAM before the program runs out of memory (in R, this comes across as an error reading something like “cannot allocate vector of size xxx Mb”). Of course, if your system has less than 3.5Gb of RAM to begin with, then there is no need to switch to 64 bit. However, if your system has over 3.5Gb RAM and your R sessions are approaching this limit, 64-bit R is for you.

To install 64-bit R on a Mac, go to this website: http://r.research.att.com/. If you run into problems with the official instructions, search through the r-sig-mac listserve posts about it or post a question there yourself.

There are several options for installing 64-bit packages in R. Usually, simply calling install.packages() in R will successfully install 64-bit packages:

install.packages("foreach" , repos="http://lib.stat.cmu.edu/R/CRAN",dependencies=TRUE)
library(foreach) #will check if it is installed correctly

Sometimes, however, you’ll need to build packages from source. If, e.g., you get the following error:

Error: package 'multicore' is not installed for 'arch=x86_64'

Then you need to install from source. Go to the CRAN website, navigate to the “Packages” link and find the package you want. Then download the “Package source”. To build this source, use TERMINAL to navigate to where you downloaded the source package and type the following into terminal (this example assumes the multicore package is being built in 64-bit):

R --arch x86_64 CMD INSTALL multicore_0.1-3.tar.gz
R --arch=i386 CMD INSTALL --libs-only multicore_0.1-3.tgz

The first command installs the package multicore for 64-bit architecture. It will erase any existing ‘multicore’ library that exists at the default location where libraries are installed (Library/Frameworks/R.framework/Resources/library). The second command installs the package for 32-bit use in case you want that. Make sure on the second command to include the --libs-only option or else you’ll erase your 64-bit package and replace it with a 32-bit one.



PLEASE NOTE: THE INSTRUCTIONS BELOW ARE NOW (NOV 2008) OUT OF DATE. THEY WORKED FINE FOR R 2.7, BUT THE WAY TO INSTALL 64-BIT R HAS CHANGED AS OF R 2.8.0. I RECOMMEND THAT YOU DO NOT USE MY INSTRUCTIONS UNLESS YOU KNOW WHAT YOU’RE DOING. THE OFFICIAL INSTRUCTIONS - WHICH I RECOMMEND YOU TO USE - ARE AT http://r.research.att.com/. If you run into problems with the official instructions, search through the r-sig-mac listserve posts about it or post a question there yourself.

<Begin outdated instructions...>

I found the official instructions for installing 64-bit R a bit confusing (that may just be me), and so I will write what worked for me here, mainly for myself, but also in case it is helpful for someone else. *Please note* I am definitely NOT a computer jock. This advice may not be the ‘right’ way to do it - it is just what worked for me. There may be a much easier way! If you do happen by and notice a better way to do it or any errors, send me an email.

Date: July 25, 2008
R version: 2.7
Aquamacs version: 1.4
Operating System: Mac OSX 10.5.4 (Leopard)
Computer System: 2008 Mac Pro, 2x3GHz Quad-Core Intel Xeon, 18GB 800MHz DDR2 RAM


1) Download and install regular R from http://cran.r-project.org/. This will give you the 32 bit version and the GUI

2) Make sure that you’ve installed the newest x-code developer tools from your Leopard disk (which includes necessary compilers) as well as the newest versions of gfortran (e.g., mine is gfortran-4.2.1) and gcc. Using the directions below, you won’t have to have these to install 64-bit R, but you will need them to install any additional 64-bit packages. These compilers (and additional instructions) are listed at http://r.research.att.com/tools/. If you’re unsure whether you have the compilers and just can’t be bothered right now, you can skip down to #4 below and proceed until you run into a problem (probably in installing a package), and then take the time to figure out the compiler stuff at that point.

3) Once compilers are installed, you'll need to 'let your OS know' to
look in the directory where these compilers live (thus, make sure you
note where they're being installed). If they are installed in
/usr/local/bin, then in terminal, type:
export PATH=$PATH:/usr/local/bin

For whatever reason (I can't remember why) my fortran compilers are in
/usr/local/bin and my Gnu Compiler Collection (gcc) is in /usr/bin.
Thus I also did this:
export PATH=$PATH:/usr/local/bin

To see what directories are already in your path, type this into terminal:

4) Go to http://r.research.att.com. Download the “Universal nightly build for Mac OS X (10.4 Tiger and 10.5 Leopard)” that has “x86_64: OK (log)”. Mine was called R-2.7-branch-leopard-universal.tar.gz. Unzip this file. It should unzip to a folder called “Library”.

5) Go to your /Library/Frameworks folder and replace the “R.frameworks” folder in there with the one in the Library/Frameworks folder you just downloaded. Congrats. You now have 64 bit R on your system. However, there is no GUI for 64-bit R. So to use it, either use R from the command window by typing:
R --arch=x86_64
OR by using an editor, such as aquamacs, which I find to be an excellent way to use R in general.

The command to double check if you are running 32- or 64-bit R is:

if the result is 4, you are running 32-bit R and if 8 you are running 64-bit R.

6) Now, to use packages, you’ll need to download them from source and compile them yourself (which is why having the needed compilers is critical). To install from source, set options(pkgType="source") in R. To install packages, do:

install.packages(pkgs = “my.package”,type = "source", lib = "/Library/Frameworks/R.framework/Versions/2.7/Resources/RLib64").

OR - from a downloaded .gz file:

install.packages(pkgs = '/path/to/file.gz',type = "source",lib = "/Library/Frameworks/R.framework/Versions/2.7/Resources/RLib64",repos=NULL)

*For this to work* you must create a folder called “RLib64” in the path above. This allows for all your 32-bit packages to go into /Library/Frameworks/R.framework/Versions/2.6/Resources/library and all your 64-bit packages to go into /Library/Frameworks/R.framework/Versions/2.7/Resources/RLib64. No more confusion about some packages being 64 and others 32 - thanks to Max Kuhn for this advice.

7) Your 64-bit packages will be installed in the folder you just created, RLib64. But R isn’t going to know to look here. By default it looks for packages in /Library/Frameworks/R.framework/Versions/2.7/Resources/library. How to tell R to look there to find the 64-bit packages? Either:
a) set the lib.loc argument when calling the library() function. See ?library. OR
b) set this function at the start: .libPaths("/Library/Frameworks/R.framework/Versions/2.7/Resources/RLib64"). In fact, here’s what I do. In my Rprofile.site file (which runs automatically each time you start R), I have the following code:

if (.Machine$sizeof.pointer==8) {
   cat("Congratulations! You are running a 64 bit version of R")
   cat("Libraries will be placed in /Library/Frameworks/R.framework/Versions/2.7/Resources/RLib64")
   cat("Packages are set to be installed from source (options(pkgType='source'))")

The Rprofile.site file resides in  /Library/Frameworks/R.framework/Versions/2.7/Resources/etc - it is either already there, in which case just type in the command above, or you’ll need to create it (just a plain text file created in, e.g., aquamacs).

8) Another helpful hint: if you run into problems, check the R-sig-mac (https://stat.ethz.ch/mailman/listinfo/r-sig-mac) and r-help (https://stat.ethz.ch/mailman/listinfo/r-help) mailing lists for new developments and work-arounds.



[Matthew C Keller's Home Page] [Biosketch] [Vita] [Publications] [Keller Lab] [Program Code] [R] [64 bit R on Mac] [Aquamacs] [Bioconductor] [Memory] [Parallelization] [UNIX] [Courses] [Links]