Skip to content

Instantly share code, notes, and snippets.

@jc-torresp
Last active August 3, 2024 18:54
Show Gist options
  • Save jc-torresp/fa303e1888e93cb51a407273d7636dda to your computer and use it in GitHub Desktop.
Save jc-torresp/fa303e1888e93cb51a407273d7636dda to your computer and use it in GitHub Desktop.
Setup a Raspberry Pi Plex Media Server (Including external storage media and Windows to Raspbian migration)

Raspberry Pi Plex Server

Installation

Ensure our operating system is entirely up to date:

sudo apt-get update
sudo apt-get upgrade

Install the package apt-transport-https that allows the apt package manager to retrieve packages over https protocol that the Plex repository uses:

sudo apt-get install apt-transport-https

Add the Plex repositories to the apt package managers key list:

curl https://downloads.plex.tv/plex-keys/PlexSign.key | sudo apt-key add -

Add the official plex repository to the sources list:

echo deb https://downloads.plex.tv/repo/deb public main | sudo tee /etc/apt/sources.list.d/plexmediaserver.list

Refresh the package list:

sudo apt-get update

Install the Plex Media Server:

sudo apt-get install plexmediaserver

Setting Up

Fix permissions (Optional)

NOTE: In my case Plex worked perfectly configured with its default user.

By default, the Plex Media Server package will utilize a user named plex. To reduce the chances of dealing with permission issues, we could change the server’s default file:

sudo nano /etc/default/plexmediaserver

change the PLEX_MEDIA_SERVER_USER line from plex to pi:

export PLEX_MEDIA_SERVER_USER=pi

Restart the plexmediaserver service:

sudo systemctl restart plexmediaserver

Setup an Static IP

We should make sure the Pi has a static IP, so it’s easy to remember the IP address. To get your current IP address, enter the following command:

hostname -I

Open up thecmdline.txt file:

sudo nano /boot/cmdline.txt

At the bottom of this file, add the following line (Replacing “YOUR IP” with the IP you got from using hostname -I):

ip=YOUR IP

Restart the Raspberry Pi:

sudo reboot

Mount USB Storage

If you only have one external hard drive connected to the Pi, then it should be attached to /dev/sda1 – additional drives will use /dev/sdb1 and /dev/sdc1 etc.

Prepare the Mount Point

Make a directory in which to mount the USB drive:

sudo mkdir /mnt/mediastorage

Make pi the owner of the mounted drive and make its permissions read, write and execute for it:

sudo chown -R pi:pi /mnt/mediastorage
sudo chmod -R 775 /mnt/mediastorage

Set all future permissions for the mount point to pi user and group:

sudo setfacl -Rdm g:pi:rwx /mnt/mediastorage
sudo setfacl -Rm g:pi:rwx /mnt/mediastorage

Determine the USB Hard Drive Format

sudo blkid

You will see something like this. Again it is the sda1 line we are interested in (In this case we are working with only one external hard drive). We need to take note in the attribute TYPE (NTFS for me) because we will need it to configure the fstab file.

/dev/mmcblk0p1: LABEL="boot" UUID="27D9-A951" TYPE="vfat" PARTUUID="d95dc960-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="db9fbdec-9f10-4008-95da-5062491e0659" TYPE="ext4" PARTUUID="d95dc960-02"
/dev/mmcblk0: PTUUID="d95dc960" PTTYPE="dos"
/dev/sda1: LABEL="Elements" UUID="328C83488C830597" TYPE="ntfs" PARTLABEL="Elements" PARTUUID="0f291c06-b24b-47f3-815d-fe34d120459d"

If the hard drive is NTFS we will need to install some utilities first:

sudo apt-get install ntfs-3g -y

If the drive is exfat install these utilities:

sudo apt-get install exfat-utils -y

For all drive types mount the hard drive with this command, -o insures pi is the owner which should avoid permission issues:

sudo mount -o uid=pi,gid=pi /dev/sda1 /mnt/mediastorage

If you get an error use this syntax:

sudo mount -t uid=pi,gid=pi /dev/sda1 /mnt/mediastorage

If the mount -t command returns an error then use this syntax:

sudo mount uid=pi,gid=pi /dev/sda1 /mnt/mediastorage

If you are getting this drive is already mounted errors then you are probably using a distro which automounts the drives. Unmount the hard drive with the next command before to mount it again to the right mount point (/mnt/mediastorage) with one of the previous commands:

sudo umount /dev/sda1

Automount the USB Hard Drive on Boot

/mnt/mediastorage will be the folder in which you store your media. We want it to be automounted on boot The best way to do this is through the UUID. Get the UUID by using this commmand:

sudo ls -l /dev/disk/by-uuid/

You will see some output like this. The UUID you want is formatted like this XXXX-XXXX for the sda1 drive. If the drive is NTFS it can have a longer format like UUID="328C83488C830597".

total 0
lrwxrwxrwx 1 root root 15 Sep 10 10:17 27D9-A951 -> ../../mmcblk0p1
lrwxrwxrwx 1 root root 10 Sep 10 11:02 328C83488C830597 -> ../../sda1
lrwxrwxrwx 1 root root 15 Sep 10 10:17 db9fbdec-9f10-4008-95da-5062491e0659 -> ../../mmcblk0p2

Now we will edit fstab to mount the USB by UUID on boot:

sudo nano /etc/fstab

Add the next line to the bottom, replace XXXX-XXXX with your UUID and TYPE with the correct type (e.g. ntfs, exfat, vfat, ext4). You may or may not need the quotation marks wrapped around the UID, you do not need quotation marks wrapped around the file system type (ext4, vfat, NTFS etc):

UUID=XXXX-XXXX  /mnt/mediastorage TYPE   nofail,uid=pi,gid=pi   0   0

The umask 0002 sets 775 permissions so the pi user and group can read, write and execute files on the external USB drive. To completely eliminate permission issues you can set the umask to 0000 which equals 777 permissions so anybody can read, write and execute. Note that 777 permissions are considered a security risk.

If you have issues here then try replacing uid=pi,gid=pi with just the word defaults (typical for ext4). You can also try replacing the UUID with the /dev/sda1 line.

In my case, for NTFS:

/dev/mmcblk0p1 /boot vfat defaults 0 2
/dev/mmcblk0p2 / ext4 errors=remount-ro,noatime 0 1

UUID=328C83488C830597    /mnt/mediastorage    ntfs   nofail,uid=pi,gid=pi    0   0

If you get any errors you can replace uid=pi,gid=pi with defaults or remove it entirely:

/dev/mmcblk0p1 /boot vfat defaults 0 2
/dev/mmcblk0p2 / ext4 errors=remount-ro,noatime 0 1

UUID=328C83488C830597    /mnt/mediastorage    ntfs   nofail,defaults    0   0

For using /dev/sda1 and defaults if you have troubles with UUID

/dev/mmcblk0p1 /boot vfat defaults 0 2
/dev/mmcblk0p2 / ext4 errors=remount-ro,noatime 0 1

/dev/sda1    /mnt/mediastorage    ntfs   nofail    0   0

Now test the fstab file works:

sudo mount -a

If you didn’t get errors then reboot, otherwise try the suggestions above to get it working then mount -a again until it succeeds

sudo reboot

You should be able to access the mounted USB drive and list its contents

cd /mnt/mediastorage
ls

Every time you reboot, the drives will be mounted as long as the UUID remains the same. If you delete the partitions or format the USB hard drive or stick the UUID changes so bear this in mind. You can always repeat the process for additional hard drives in the future.

If you have multiple hard drives you will have to make separate mount points (e.g. /mnt/mediastorage2) for each drive’s partition.

Connecting a Browser

To connect to the browser, enter the IP followed by the port 32400 and /web/. For example, in my case:

192.168.1.95:32400/web/

You will be prompted to log in, simply sign up or sign in to an existing plex account. Next, you will need to set up your music, movie, and TV show libraries:

  1. First select add library in the left-hand side column.
  2. Next, select the type of media that is in the folder. If you have more than one type, then you will need to add a new library for each type of media.
  3. Next, you will need to select the folder that has all your media in it. For example, mine is on /mnt/mediastorage/Media.
  4. Once added the library, Plex will now organize the files on its interface.

Transfer Plex Library from Windows to Raspberry Pi (Raspbian)

  1. Go to %LOCALAPPDATA%\Plex Media Server on Windows.
  2. Backup the folders: Media, Metadata, Plug-in Support, Plugins. These folders contain the data and the media images for the library.
  3. Configure Plex on Linux via the web interface. At this point you’ll have an empty library.
  4. Shutdown Plex on Linux:
sudo systemctl stop plexmediaserver
  1. Copy the backed up media files to Linux:
/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/
  1. Fix permissions in the copied folders (Repeat this step with the folders: Metadata, Plug-in Support and Plugins):
sudo chown -R plex:plex /var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Media
sudo chmod -R 755 /var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Media
  1. Start Plex:
sudo systemctl start plexmediaserver
  1. Open Plex web interface and change the Library paths to new paths. This will trigger a rescan of the files.
  2. After complete, re fresh the plex website.

Completely Uninstall Plex Media Server

sudo apt-get purge plexmediaserver

What it does:

  1. Uninstall the package
  2. Removes the user plex
  3. Deletes its home directory /var/lib/plexmediaserver

If you’re uncertain and/or want to be certain about user plex and its data (home directory):

sudo userdel plex
sudo rm -rf /var/lib/plexmediaserver
@jc-torresp
Copy link
Author

Thanks so much for the tutorial, it worked perfectly and helped me tremendously. All of your directions reference setting up the drive to mount to /mnt/mediastorage except for one line about 2/3 of the way down where it has /mnt/usbstorage. Is that supposed to be usbstorage? I think that's where I ran into an issue. As soon as I changed it to /mnt/mediastorage everything worked perfectly. Thanks again for your work, it's greatly appreciated.

Hello @imjuschilling, I am glad that my tutorial helped you! It was my mistake, I will fix the gist, thank you for your feedback.

@michael10c
Copy link

@jc-torresp thanks for the guide. I am actually using a Beaglebone black wireless and this tutorial worked just fine (running debian).

@jc-torresp
Copy link
Author

@jc-torresp thanks for the guide. I am actually using a Beaglebone black wireless and this tutorial worked just fine (running debian).

Oh that's great @michael10c , I am glad this guide helped you!

@muscaiu
Copy link

muscaiu commented Dec 24, 2021

Any guide for how to make the usb device shared over the network?
I want to use my windows laptop to get movies from torrent and download them directly on to the rpi usb device so they will be always available from my Android Tv Plex.

What i've tried:
I'm able to access a shared folder from the rpi's Desktop using samba, but nothing i try seems to work when i try to access a shared folder from the usb stick.

@Fjaoos
Copy link

Fjaoos commented Jan 5, 2022

@jc-torresp thank you so much for this guide!

Do you have any information about plex options and drive sleep? It does seem like my drive is spinning constantly.

@ellieb010
Copy link

I followed this guide and I think I have it almost working. I have an external hdd connected to my raspberry pi. I can add my media files from this drive to plex and everything works fine. But when I reboot my ext hdd, plex cannot connect to the drive anymore. Only when I reboot my rpi it all works again. Does anyone know where it goes wrong? Thank you

@michael10c
Copy link

@ellieb010 do you have your pi set up for auto-mount ing the HDD?

@ellieb010
Copy link

@michael10c yes I did that. And after rebooting my rpi it automounts the HDD to the location I assigned. But only when I reboot the HDD itself, it doesn't mount to this location

@LordFader
Copy link

I had everything working during the last 18 months.
During the last Plex Server update in April my TV Shows Library:
Something went wrong
An unexpected error occurred.

All my Media library are in the same network drive and is working... only TV Shows show that error.
TV Shows show up and play in main page but not in the TV Shows Library Tab.
I did all the clean & optimize database...
Still the same.

@SmokeShine
Copy link

Worked. Thanks

@rootbeerdave
Copy link

I set this up quite some time ago using your tutorial and it has worked great! I'm having a bit of an issue as of late. I have a USB drive hooked up to my pi in a basement closet and add movies and shows from a windows PC via the network. I noticed the items I add don't show up in PLEX. I figured maybe it was a naming issue but did some extra effort and did some name and folder edits in file explorer, again, via the PC. Still nothing. What's odd is that none of my edits show up in PLEX when I look at the info for the shows within PLEX. Everything that's in there still plays but the names don't correlate and new items don't show up. I have yet to plug a screen into the pi to see what I can find there but I'm wondering where to start on getting the libraries to update to the modified folder and file name data.

@IsShai
Copy link

IsShai commented Mar 10, 2023

thank you so much for this!

@hivian
Copy link

hivian commented Nov 3, 2023

When you type this command:

sudo mount -o uid=pi,gid=pi /dev/sda1 /mnt/mediastorage

And get this error:

"The disk contains an unclean file system (0, 0).
Metadata kept in Windows cache, refused to mount.
Failed to mount '/dev/sd1': Operation not permitted
Falling back to read-only mount because the NTFS partition is in an
unsafe state."

I tried this command and it fixed the disk:

sudo ntfsfix -b -d /dev/sda1

This one finally worked:

sudo mount -o uid=pi,gid=pi /dev/sda1 /mnt/mediastorage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment