Quick start

Prior to using the tensorflow R package you need to install a version of Python and TensorFlow on your system. Below we describe how to install to do this as well the various options available for customizing your installation.

Note that this article principally covers the use of the R install_tensorflow() function, which provides an easy to use wrapper for the various steps required to install TensorFlow. You can also choose to install TensorFlow manually (as described at https://www.tensorflow.org/install/). In that case the Custom Installation section covers how to arrange for the tensorflow R package to use the version you installed.

TensorFlow is tested and supported on the following 64-bit systems:

Installation

First, install the tensorflow R package from GitHub as follows:

install.packages("tensorflow")

Next, configure R with a Python installation it can use, like this:

library(reticulate)
path_to_python <- install_python()
virtualenv_create("r-reticulate", python = path_to_python)

Note that if you already have Python installed, you don’t need to call install_python() and instead can just supply an absolute path to the Python executable.

Then, use the install_tensorflow() function to install TensorFlow.

library(tensorflow)
install_tensorflow(envname = "r-reticulate")

You can also use keras::install_keras(), which installes Tensorflow, in addition to some commonly used packages like “scipy” and “tensorflow-datasets”.

install.packages("keras")
library(keras)
install_keras(envname = "r-reticulate")

You can confirm that the installation succeeded with:

library(tensorflow)
tf$constant("Hello Tensorflow!")
Loaded Tensorflow version 2.10.0
tf.Tensor(b'Hello Tensorflow!', shape=(), dtype=string)

This will provide you with a default installation of TensorFlow suitable for use with the tensorflow R package. Read on if you want to learn about additional installation options, including installing a version of TensorFlow that takes advantage of Nvidia GPUs if you have the correct CUDA libraries installed.

Installation methods

TensorFlow is distributed as a Python package and so needs to be installed within a Python environment on your system. By default, the install_tensorflow() function attempts to install TensorFlow within an isolated Python environment (“r-reticulate”).

These are the available methods and their behavior:

Method Description
auto Automatically choose an appropriate default for the current platform.
virtualenv Install into a Python virtual environment at ~/.virtualenvs/r-reticulate
conda Install into an Anaconda Python environment named r-reticulate

Note that install_tensorflow() will intentionally not install into a system Python installation (e.g., /usr/bin/python).

install_tensorflow() is a wrapper around reticulate::py_install. Please refer to ‘Installing Python Packages’ for more information.

Alternate Versions

By default, install_tensorflow() install the latest release version of TensorFlow. You can override this behavior by specifying the version parameter. For example:

install_tensorflow(version = "2.7")

Note that you can provide a full major.minor.patch version specification, or just a major.minor specification, in which case the latest patch is automatically selected.

The default tensorflow package is GPU capable. However, if you will not be using a GPU, you can install the smaller cpu-only package like this:

install_tensorflow(version = "cpu")

You can install the nightly build of TensorFlow (CPU or GPU version) with:

install_tensorflow(version = "nightly")      # cpu+gpu version
install_tensorflow(version = "nightly-cpu")  # cpu version

You can install any other build of TensorFlow by specifying a URL to a TensorFlow binary. For example:

install_tensorflow(version = "https://files.pythonhosted.org/packages/c2/c1/a035e377cf5a5b90eff27f096448fa5c5a90cbcf13b7eb0673df888f2c2d/tf_nightly-1.12.0.dev20180918-cp36-cp36m-manylinux1_x86_64.whl")