Home Media Server using Raspberry Pi & Plex

A comprehensive step by step guide, covering everything you need to know to install, run and maintain Plex Media Server running on a Raspberry Pi shared across your network.

Plex is a powerful media server which make your videos, music and photos accessible on all your favourite devices; Web, Android, iOS, Windows, Xbox, Chromecast, Smart TVs and more.

Plex running on multiple devices

Plex indexes all you Movies, TV shows and Music, gathers beautiful posters and artwork, cast and crew info, Rotten Tomatoes ratings, and provides a nice clean interface where all your media can live and come alive.

Plex needs a running server instance on a machine locally on your network. Raspberry Pi can be a perfect in-expensive solution for an always on, low energy media server. Raspberry Pi is a tiny and inexpensive (~50$) computer.

An external Hard Drive with external power is needed as well to hold all your files. Raspberry Pi is not powerful enough to power the external hard drive, thus external power source. In the following steps I’ll go through setting up a Plex Media Server on a Raspberry Pi (RPi 3 is the latest and fastest model with a 1.2Ghz cpu and 1Gb ram) with a 1TB 3.5 HDD in an enclosure and USB interface on a micro SD card.


The Plan

a. Requirments
b. Preparing your Raspberry Pi
c. Static IP for the Raspberry Pi
d. SSH-ing the Pi
e. Installing Plex Media Server
f. Preparing the disk storage
g. Setting up Plex
h. Keeping the server up & running autonomously 
i. Monitoring your Pi
j. Updating Plex Media Server
k. Raspberry Pi as a client


Requirements

  1. Raspberry Pi 3
  2. Raspberry Pi power adapter
  3. 8GB (or greater) micro SD Card class 10 (or faster) (and a card reader, duh)
  4. Router with internet & ethernet port
  5. Network cable
  6. Monitor with an HDMI connection, USB Keyboard and USB mouse (temporarily for the initial setup)
  7. External USB Hard Drive (the bigger the better) with an external power source
  8. Can-do attitude!

Before we dive in, this article was written in mid-2016. Since then some packages/software (even plex itself) might have been updated with breaking changes. If you don’t manage to install Plex Server following this tutorial at first and manage to make it work in some way following help (or coming up with a solution yourself), please take note of the changes, comment below and I will update the article accordingly! Thanks


Preparing your Raspberry Pi

To start we need to prepare the OS on the SD Card, setup the hardware and connect everything for initial setup.

IMPORTANT NOTE: from now on any terminal commands in block with “<…>” as value, means that you must remove the “<>” and the value inside is variable depending on your setup or information gathered in a previous step.
  1. First download Raspbian, an OS based on Debian optimized for RPi hardware. Download the image from: https://www.raspberrypi.org/downloads/noobs/ (use full not lite)

2. Format the micro SD Card (use FAT32 file system)

3. Install Raspbian image on the SD Card using NOOBS installer. Follow the official guide: https://www.raspberrypi.org/help/noobs-setup/

4. Insert the micro SD card into the RPi and attach the keyboard, mouse, monitor and network cables. Finally attach the power cable and turn on the power to start your RPi.

5. Once the RPi go to Menu > Preferences > Raspberry Pi Configuration

opening the raspberry pi configuration

6. In the following setting screen change the following:
i. Turn ‘Auto Login’ on
ii. Boot is set to CLI/Terminal 
iii. Change pi user password
iv. Change your hostname, like ‘plex-media-server’
v. Performance - GPU Memory is set to 16 (lowest possible)
vi. Set Locale as en(english) | US(USA) | UTF-8
vii. Set your Timezone

Finally click OK and you will be prompted to restart your RPi. Choose Yes.


Static IP for the Raspberry Pi

Home routers are commonly DHCP meaning that they will assign an IP randomly from an available set to each connected device. This can make things difficult if we need to login remotely (ssh) on our RPi. Thus, we need to set a static/fixed ip. 
There are two ways to go about this, either setup the router to reserve an IP by the device MAC address or edit the network interface in the RPi to have a fixed ip. We’ll do the latter.

Our RPi should be in command line mode (if not restart device to command line). Lets take note of the current IP:

sudo ifconfig

From the list given take note of the eth0 settings: (yours will be different, note the second line)

sudo ifconfig

will translate to :

Current IP Address = 192.168.0.104
Broadcast Range = 192.168.0.255
Subnet Mask = 255.255.255.0

Now we need to get information about the router:

sudo route -n

We need to take note of the Destination IP and the Gateway IP

sudo route -n

Gateway = 192.168.0.10
Destination = 192.168.0.0

Edit the network interfaces file

sudo nano /etc/network/interfaces

Edit the file which we just opened, first we need to update an existing line. Look for the following line:

iface eth0 inet manual 

or

iface etho inet dhcp

We need to change this to:

iface eth0 inet static

Than, under that line we need to add the following with the values you noted down before

address <your IP address>
netmask <your subnet mask>
network <your destination>
broadcast <your broadcast range>
gateway <your gateway>
the updated section with static and address added

Finally save the file (CTRL + X , press Y and Enter)

Now we need to restart the RPi for to get the fixed ip.

sudo reboot

SSH

edit: 10/3/17 (thanks to Johanna Petersson)
“As of the November 2016 release, Raspbian has the SSH server disabled by default. You will have to enable it manually. This is done using raspi-config
Enter sudo raspi-config in the terminal, first select Interfacing options, then navigate to ssh, press Enter and select Enable or disable ssh server”.

We don’t need to have the RPi always attached to the keyboard, mouse and monitor. We can login remotely from a PC/Mac and continue setting up everything hassle-free.

the power of cable ties

Store away the RPi in a cool dry place where it can run ad aeternum till the end of the world. I placed mine next to the router and attached to the eternal hard drive.

RPi only needs power cable, network cable and of course the external hard drive where we will be storing all of our media. Turn your RPi on again.

(Note: if you are on Windows, download PuTTY so we can SSH to our RPi)

In Terminal (or PuTTY) ssh to the RPi

ssh pi@<192.168.0.104>

(Where the IP address is the one we noted before.) If you are prompted with an ‘authenticity cannot be established’ message continue anyway by typing ‘yes’ (first time only). Login by entering your pi password and you should be in!

(note, if you didn’t update the pi password, by default it should be ‘raspberry’)


Installing the Plex Media Server

Let’s start by making sure we have the OS and all other packages up to date

sudo apt-get update && sudo apt-get upgrade -y

and

sudo apt-get update && sudo apt-get dist-upgrade

Now we enable HTTPS transport for packages by installing the following

sudo apt-get install apt-transport-https -y --force-yes

We also download a key to authenticate and make sure our downloads are safe

wget -O — https://dev2day.de/pms/dev2day-pms.gpg.key | sudo apt-key add -
NB: if the above command doesn’t work after you copy & paste in terminal, make sure that after ‘wget -O’, there is a double dash, not a single one or a long one (medium formatting keeps changing this and some people reported this error.)

With the key downloaded now we add a new repository provided by dev2day.de who has worked on the packages that will power Plex.

echo "deb https://dev2day.de/pms/ jessie main" | sudo tee /etc/apt/sources.list.d/pms.list

Run update again

sudo apt-get update

Install the Plex Media Server

sudo apt-get install -t jessie plexmediaserver -y

Fix the IP for boot by

sudo nano /boot/cmdline.txt

At the end of the line in this text file, add (with a space before)

 ip=<192.168.0.104>

Finally save the file (CTRL + X , press Y and Enter).

Now reboot your RPi

sudo reboot

(SSH connection will be closed and you will need to login again)

After reboot is complete, from the PC/Mac you are working on, open the browser and run the following url:

http://<192.168.0.104>:32400/web

This means Plex Server is now installed and running on your network. Hurray!!


Preparing the storage disk

Auto mount the disk

Make sure the external storage is connected to the RPi and turned on. To properly mount the external storage follow this guide:


Disk spin down

Next we will configure the OS to spin down and better manage the life of the disk and save energy. (This only applies to HDD external disk and is OPTIONAL)

hd-idle is a linux packgae used to detect disk activity. Let’s check if we can use it:

cat /proc/diskstats

You should see some output including the lines below (starting with 8):

If you get no such file or directory you cannot use hd-idle. In this case, use other methods found here http://www.htpcguides.com/spin-down-and-manage-hard-drive-power-on-raspberry-pi/

You have to build hd-idle so make sure you have compilation tools

sudo apt-get install build-essential fakeroot debhelper -y

Grab the hd-idle source

wget http://sourceforge.net/projects/hd-idle/files/hd-idle-1.05.tgz

Unpack hd-idle and enter the folder

tar -xvf hd-idle-1.05.tgz && cd hd-idle

Build and install the hd-idle package

dpkg-buildpackage -rfakeroot
sudo dpkg -i ../hd-idle_*.deb

Check again that hd-idle works with your disk

sudo hd-idle -i 0 -a sda -i 300 -d

You should see output like this

Use Ctrl+C to stop the above

Open the hd-idle configuration file to enable the service to automatically start and spin down drives

sudo nano /etc/default/hd-idle

Change this line to enable hd-idle

START_HD_IDLE=true

Adjust this line to enable sleeping on the drive every 5minutes (60 seconds * 5 = 300)

HD_IDLE_OPTS="-i 0 -a sda -i 300"

Finally save the file (CTRL + X , press Y and Enter) and restart the service

sudo service hd-idle restart

Share disk over network

Finally we will make the external drive accessible on the network. This will facilitate file management from a remote machine. This will come handy when you need to add new media. We will use Samba to share the disk

sudo apt-get install samba samba-common-bin -y

Make your user owner of the path to be shared

sudo chown -R pi:pi /mnt/usbstorage

Backup the samba share file

sudo cp /etc/samba/smb.conf /etc/samba/smb.bak

Edit the samba configuration file

sudo nano /etc/samba/smb.conf

Uncomment WINS support, change this line, it should enable the NETBIOS name RASPBERRYPI on your network. Update the line by removing the # and set to yes:

Copy and paste the below at the end of the file. This contains the path we are going to share (defined before in previous steps) and security settings on which user to use.

[PLEXMEDIA] #Name of the share
comment = Plex media share
path = /mnt/usbstorage/
create mask = 0775
directory mask = 0775
read only = no
browseable = yes
public = yes
force user = pi
#force user = root
only guest = no

Save and exit (CTRL + X , press Y and Enter), and set a samba password for user pi

sudo smbpasswd -a pi

Restart the samba service

sudo service samba restart
sudo service smbd restart
sudo service nmbd restart

(Note: the first line may not work depending on the dist of your OS, thus restart the service separately and finally restart you RPi)

sudo reboot

Once the RPi has turned on again, check that the disk is now accessible over the network from your PC/Mac. On Mac you will find the Raspberry Pi and it’s disk under network. On Windows you might need to navigate to (in file explorer address bar)

\\<192.168.0.104>\<plexmedia> 

Where \\<raspberry-pi-IP-address>\<samba-share-name> - Samba share name is the first value in the square brackets from the snippet above from the samba config file.


Setting up Plex

Register for a free account https://www.plex.tv/sign-up/ with Plex before we start setting up. After, from the browser visit

http://<192.168.0.104>:32400/web

and login with your account.

From here, follow the wizard to add the media which you have on the external drive.

choose the type of media you’re going to add
select the media location from /mnt/usbstorage path

After adding some media, wait for the library to index the files. This will take some time, especially if you have a lot of files. I suggest adding no more than 30 files at one time to a library and let Plex finish indexing those first before adding another set of 30.

Once the media has been added and indexed, you can play it from a selection of devices by downloading the corresponding app from https://www.plex.tv/downloads/

If you feel the quality is low or the RPi is struggling to transcode the videos over, you can change settings of streaming quality amongst others which can result in better performance.

server settings page with streaming settings

Keeping the server up & running autonomously

Auto Restart

One thing which I noticed after few days was that after some heavy tasks Plex can take 100% of your memory. Thus I ended up restarting the service and freeing memory every now and than. To tackle this problem I added cronjob to restart the RPi daily at 7am. Since Plex starts at boot and the external disk auto mounts, a reboot will ensure that Plex is fresh everyday with all available memory at hand.

Edit crontab:

sudo crontab -e

(If you get prompted to select from a list of files, choose the second one marked as easiest)

Insert the following line at the bottom

0 7  *   *   *    /sbin/shutdown -r +5

The above translates to:

m      h    dom        mon   dow       command
minute hour dayOfMonth Month dayOfWeek commandToRun

so the line:

will reboot your system every day at 7:05am. (The +5 will give a warning on any connected ssh, enough time to safely close in case you’re doing something.) Save and exit the file (CTRL + X , press Y and Enter).

Monitor service and restart if fails

Another thing which we’ll do is monitor the Plex service and restart it in case it stops working. This will prove very handy, an auto restart in case it stops working and you don’t end up ‘sshing’ to restart your RPi or the Plex service.

First install monit package. monit lets you monitor services via their pid.

sudo apt-get install monit -y

Now lets create a symlink for the Plex pid for monit to monitor

sudo ln -s /var/lib/plexmediaserver/Library/Application\ Support/Plex\ Media\ Server/plexmediaserver.pid /run/plexmediaserver.pid

Create Plex monit config file

sudo nano /etc/monit/conf.d/plex

Paste the following at the end of the file opened

check process plexmediaserver with pidfile /run/plexmediaserver.pid
start program = "/etc/init.d/plexmediaserver start" with timeout 60 seconds
stop program = "/etc/init.d/plexmediaserver stop"
if failed host 127.0.0.1 port 32400 then restart
if failed url http://127.0.0.1:32400/web/index.html then restart

This will monitor the Plex service every 60 seconds and restart it in-case it’s down. Save and exit the file (CTRL + X , press Y and Enter).

monit config file for plex

Finally restart the monit service so the changes take place

sudo service monit restart

You can test if this works by stopping the ‘plexmediaserver’ service and check that it’s turned back on after 60 seconds.


Monitoring your Pi

Here are some handy commands to monitor your RPi:

Check CPU usage:
top
processes running on the raspbian

(CTRL+C to clear and get a new command line)

Check RAM usage
watch free -m
ram usage/availability | 204 is used while 768 is available (MB)

(CTRL+C to clear and get a new command line)

Check Plex service status
sudo service plexmediaserver status
plex service details

(to restart the plexmediaserver service use ‘restart’ at the end instead of ‘status’)

Check disk space
df -h
/dev/root/ is the SD Card while /dev/sda1 is the external disk
Check board temperature
/opt/vc/bin/vcgencmd measure_temp
board temperature

Updating Plex Media Server

If you get a notification that a newer version of Plex Media Server is out, we can update to the latest one by doing the following:

SSH to you RPi and do the following set of commands, one by one:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install --only-upgrade plexmediaserver-installer
sudo reboot

If the above doesn’t work (it should), try the following set instead:

sudo apt-get update 
sudo apt-get upgrade 
sudo service plexmediaserver stop 
sudo apt-get remove plexmediaserver 
cd /tmp 
wget https://dev2day.de/pms/pool/main/p/plexmediaserver/plexmediaserver_0.9.12.3.1173-937aac3-1~wheezy_armhf.deb 
sudo dpkg -i plex* 
sudo reboot

After restarting confirm you updated Plex Media Server and that everything works fine.


Raspberry Pi as a client

If you have a ‘dumb’ TV with HDMI, you can use another (older)RPi to use as client. Install http://www.rasplex.com / and attach it to the TV and use your mobile phone with the official Plex app as a remote.

I won’t go into detail but it’s really easy:

  1. Download Rasplex Installer from the link above
  2. Use the installer to write the Rasplex OS on the SD Card
  3. Once done, put the SD Card in the RPi and turn it on (keyboard attached first run, later you can use the mobile as remote)
  4. Connect to a network and follow the wizard. (Pair Rasplex with your account — make sure you’re logged in the browser, setup network and preferences & enable pre-caching)
  5. Attach to TV via HDMI and reboot.
  6. Use the official Plex app for iOS, Android or Windows as a remote.

Update: After 3 weeks running, the RPi never failed and worked flawlessly providing many devices (Rasplex attached to TV, Xbox, mobile phones & laptop) with movies and tv shows. 
Some things to note: If you are copying files on the external drive via network or Plex is busy indexing and getting metadata for the new media added, I highly recommend letting the process finish first before streaming/watching anything. Failure to do so might result in the RPi freezing.
Regarding streaming to multiple devices at the same time, I tested for 10 minutes playing two different videos to two different devices. Quality was set at 3Mbps 720p. Video quality was good & remained constant and never stopped waiting for any buffer. CPU usage varied, sometimes using 20% sometimes 100%. Memory was used in full. Results below:

cpu usage 20%
cpu usage 100%

Happy Media Watching!


Troubleshooting

I’m getting ‘Permission denied’ when I run commands.

You need more rights to run this command. Add ‘sudo’ infornt of your command, if this doesn’t work you can switch to root user with:

su root

I’m getting a ‘cannot set LC_TYPE to default locale’ message when installing packages.

edit the bashrc:

sudo nano ~./bashrc

at the end of the file add


export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8

save the file (CTRL + X , press Y and Enter), than run

source ~./bashrc

test if works

 locale -a

Commanded Aliases

Big thanks to Ray Anderson which suggested the following Aliases (shortcut to commands which you can add) in his response found below:

ssh into the pi
Then create and edit the alias profile with

sudo nano ~/.bash_aliases

Now add the commands for your aliases

Editor for aliases

alias aliasEdit=‘sudo nano ~/.bash_aliases’

Preferred ‘cp’ implementation

alias cp=’cp -iv’

Preferred ‘mv’ implementation

alias mv=’mv -iv’

Preferred ‘mkdir’ implementation

alias mkdir=’mkdir -pv’

Preferred ‘ls’ implementation

alias ll=’ls -FGlAhp’

Preferred ‘less’ implementation

alias less=’less -FSRXc’

View of processes

alias stats=’top’

Load Raspbian GUI

alias Raspbian=’startx’

SSH logout

alias x=’exit’

System halt and shutdown process

alias shutDown=’sudo shutdown –h now’

Rasp config alternate command

alias raspConfig=’sudo raspi-config’

Reboot system

alias reboot=’sudo reboot’

Monitor CPU usage

alias cpu=’top’

Monitor RAM usage

alias ram=’watch free -m’

Check Plex service status

alias service=’sudo service plexmediaserver status’

Restart Plex service

alias serviceRestart=’sudo service plexmediaserver status restart’

Check disk space

alias chkdsk=’df -h’

Check board temperature

alias temp=’/opt/vc/bin/vcgencmd measure_temp’

Update the server step 1

alias update1=’sudo apt-get update’

Update the server step 2

alias update2=’sudo apt-get upgrade’

Update the server step 3

alias update3=’sudo apt-get install — only-upgrade plexmediaserver-installer’

control x
y (if satisfied) else n
enter

sudo touch ~/.bash_aliases

type alias into the command line for a quick list of your commands.

You may have to logout of you ssh session then login again before the alias commands will work