Building Shiny Server from Source

Joe Cheng edited this page Oct 19, 2016 · 23 revisions

RStudio provides some pre-compiled installers for Shiny Server. If you're using Ubuntu 12.04 (64 bit) or CentOS/RHEL >= 5.4 (64 bit), or SUSE Linux 11, we recommend that you use one of these pre-built installers. If you're on a different distribution or prefer to build from source, these instructions may help.

Prerequisites

The following software must be available on the system before continuing:

  • python 2.6 or 2.7 (Really. 3.x will not work)
  • cmake (>= 2.8.10) (see the cmake question below if an appropriate version isn't available on your system)
  • gcc (as of Shiny Server 1.5, gcc 4.8 or newer is required)
  • g++ (as of Shiny Server 1.5, g++ 4.8 or newer is required)
  • git
  • R-base-devel - Many distributions provide two packages when distributing R: one for base R and one "devel" package which is helpful in building extra packages, among other things. In addition to base R, Shiny Server requires many of the components typically included in the "devel" packages, such as libpng and libjpg. If you're on a platform that doesn't have such a "devel" package, be sure to include these components when installing R.

Finally, you must install the Shiny package in the system-wide library. One way to do that is the following command:

sudo su - -c "R -e \"install.packages('shiny', repos='https://cran.rstudio.com/')\""

Note that you may need to download the package and install it from the command line if you're using an older version of R that doesn't support installation from an HTTPS CRAN repo, or use un-encrypted HTTP (insecure).

Installation

Once all of the prerequisites have been satisfied, you can use the following commands to download and install Shiny Server.

# Clone the repository from GitHub
git clone https://github.com/rstudio/shiny-server.git

# Get into a temporary directory in which we'll build the project
cd shiny-server
mkdir tmp
cd tmp

# Add the bin directory to the path so we can reference node
DIR=`pwd`
PATH=$DIR/../bin:$PATH

# See the "Python" section below if your default python version is not 2.6 or 2.7. 
PYTHON=`which python`

# Check the version of Python. If it's not 2.6.x or 2.7.x, see the Python section below.
$PYTHON --version

# Use cmake to prepare the make step. Modify the "--DCMAKE_INSTALL_PREFIX"
# if you wish the install the software at a different location.
cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DPYTHON="$PYTHON" ../
# Get an error here? Check the "How do I set the cmake Python version?" question below

# Recompile the npm modules included in the project
make
mkdir ../build
(cd .. && ./bin/npm --python="$PYTHON" install)
(cd .. && ./bin/node ./ext/node/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js --python="$PYTHON" rebuild)

# Install the software at the predefined location
sudo make install

At this point, you've successfully installed Shiny Server! You'll still need to configure a few directories and other assets in order for Shiny Server to work properly, however. We'll do this in the next section.

Post-Install

Shiny Server will look for resources in specific locations. Some of these (log directories, application directories, etc.) can be modified using a configuration file stored at /etc/shiny-server/shiny-server.conf. If no such file is provided, the default configuration will be used. The following commands prepare a system for such a configuration.

# Place a shortcut to the shiny-server executable in /usr/bin
sudo ln -s /usr/local/shiny-server/bin/shiny-server /usr/bin/shiny-server

#Create shiny user. On some systems, you may need to specify the full path to 'useradd'
sudo useradd -r -m shiny

# Create log, config, and application directories
sudo mkdir -p /var/log/shiny-server
sudo mkdir -p /srv/shiny-server
sudo mkdir -p /var/lib/shiny-server
sudo chown shiny /var/log/shiny-server
sudo mkdir -p /etc/shiny-server

At this point, you should be able to follow the Configuration section of the README to begin serving some Shiny applications. You should be able to start the server from the command line by executing shiny-server. If you'd like Shiny Server to start automatically when your machine is booted, see the associated question in the F.A.Q. below.

Frequently Asked Questions

What if Python 2.6 or 2.7 isn't installed or isn't the default?

The cmake ... line above requires that the PYTHON environment variable be set to an installation of Python 2.6.x or 2.7.x (no older, no newer). Many Linux distributions maintain multiple versions of Python in their repositories. Some older distributions will default to an older version of Python but also provide a python26 package for Python 2.6. Some newer distributions will default to Python 3, but provide a python2 package for Python 2.7. It's very likely that some package repository hosts either Python 2.6 or 2.7 for your distribution.

Once Python 2.6 or 2.7 is installed on your system, it still may be necessary to distinguish which version of Python should be used, as the default may be an older or newer version. To do this, you can alter the line above which sets Python to either

PYTHON=`which python2`
#   ...OR...
PYTHON=`which python26`

Once Python has been properly set, the command $PYTHON --version should echo Python 2.6.x or Python 2.7.x.

How do I set the cmake Python Version?

The answer above should solve this issue for most people. If you properly set PYTHON before running cmake, it's less likely you'll have any problems. However, some distributions are known to be a bit more stubborn about letting you override the default version of Python. In these cases, you may need to do some extra work to make your system default to using Python 2.6 or 2.7, if just while installing Shiny Server.

The easiest way to go about this is to add a reference to the desired version of Python earlier in your $PATH than Python is currently. Often times, /usr/local/bin/ is earlier in the list of $PATH directories than the default locations for python. If so, you can create a link to the desired version of python in /usr/local/bin using the following command, after first checking to ensure that you won't overwrite anything stored at /usr/local/bin/python:

sudo ln -s $PYTHON /usr/local/bin/python

You can then try the cmake step above again -- hopefully with more success this time. You'll likely want to remove this link when the installation is complete using sudo rm /usr/local/bin/python.

What if a sufficiently recent version of cmake isn't available?

Shiny Server requires version 2.8.10 or newer of cmake. Some distributions will not have this recent of a version of cmake available in their repositories. In this case, cmake must be built from source. Building cmake from source is often as simple as:

wget http://www.cmake.org/files/v2.8/cmake-2.8.11.2.tar.gz
tar xzf cmake-2.8.11.2.tar.gz
cd cmake-2.8.11.2
./configure
make
sudo make install

How can I make Shiny Server start automatically?

Some newer systems use Upstart to manage system daemons. If your distribution has Upstart available, we recommend using the Upstart script we provide to automatically run Shiny Server on boot. You can install this script using the following command:

sudo wget\
  https://raw.github.com/rstudio/shiny-server/master/config/upstart/shiny-server.conf\
  -O /etc/init/shiny-server.conf

You can then run sudo start shiny-server to start Shiny Server.

If your distribution does not use upstart, you'll likely need to create an init.d script for Shiny Server. We provide some init.d scripts here that may serve as a useful guide for you, but they may need some modification to work as expected on your distribution. Unfortunately, because the init.d files vary from distribution to distribution, we aren't able to provide exhaustive documentation on how to make this work on your particular setup. Once properly configured, you can run sudo /etc/init.d/shiny-server start to manually start Shiny Server.