How to Host Your Own Youtube with Peertube

A person in front of a desktop computer editing a video.

Peertube is a federated and open source video hosting platform that you can run from your own computer. Iit allows you to completely control all the content that you host and share from your website. This tutorial shows you how to install and host Peertube on Ubuntu.

Tip: if you just want to watch YouTube offline, there is no need to install Peertube. Check out all the ways to watch YouTube offline.

Why Host and Use Peertube

One of the most attractive features of Peertube is its ability to load videos from other instances, making it possible to view content from outside your website but still retain the control over your data.

A screenshot of a web browser showing a Peertube page playing remote video.

Another advantage of Peertube over Youtube is that it is entirely open source. (Learn all about open source licenses here.) This makes it easy for anyone to scrutinize the program’s codebase, which can be helpful for users that are concerned about their data security.

Good to know: learn more about protecting your data online by installing privacy and security extensions in Chrome.

Installing Peertube

Before you can install Peertube, you need to make sure that you have a server ready. This could be your personal PC or a rented server from a web host. This tutorial is done on an Ubuntu VPS from Digitalocean.

A section of a Digitalocean page showing the Peertube droplet.
  1. Set up a new user account for Peertube. This will allow you to easily control what the program can do inside your system:
sudo useradd -b /bin/bash -m -d /var/www/peertube -G sudo peertube
sudo passwd peertube
A terminal window showing the user creation process for Peertube.

Creating a new user account also allows you to set the $HOME variable under “/var/www/.” This is important, as the Web backend for Peertube will not be able to traverse the default “/home” heirarchy.

  1. Switch to your new user account with the command:
su peertube
  1. Install the dependencies for Peertube:
sudo apt install cron wget curl unzip python3-dev python-is-python3 certbot nginx python3-certbot-nginx ffmpeg postgresql postgresql-contrib openssl g++ make redis-server git
A terminal window showing the installation process for Peertube's dependencies.
  1. Install NodeJS in your machine:
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt update
sudo apt install nodejs
A terminal window showing the installation process for NodeJS.
  1. Install Yarn. This is a powerful yet lightweight package manage for NodeJS:
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install yarn
A terminal window showing the installation process for Yarn.
  1. Once you have installed all of the dependencies for Peertube, configure your system to install the program. First, enable the program’s database backend:
sudo systemctl enable postgresql
sudo systemctl start postgresql
sudo systemctl enable redis-server
sudo systemctl start redis-server
A terminal window showing SystemD commands for enabling PostgreSQL.
  1. Add your Peertube user to PostgreSQL by running the following commands:
cd $HOME
sudo usermod -aG peertube postgres
sudo -u postgres createuser -P peertube
  1. Create the database for the program by running the following commands:
sudo -u postgres createdb -O peertube -E UTF8 -T template0 peertube_run
sudo -u postgres psql -c "CREATE EXTENSION pg_trgm;" peertube_run
sudo -u postgres psql -c "CREATE EXTENSION unaccent;" peertube_run
A terminal window showing the database creation in PostgreSQL.
  1. Create the directory structure of the program in your home directory:
mkdir config storage versions
chmod 750 ./config
Install Peertube Linux 12 Create Peertube Directory Structure
  1. Download the Peertube binary files:
cd ./versions
wget https://github.com/Chocobozzz/PeerTube/releases/download/v5.0.1/peertube-v5.0.1.zip
unzip peertube-v5.0.1.zip
cd ./..
A terminal window showing the unzipping process for Peertube.
  1. Create a symbolic link between your install and your home directory:
ln -s /var/www/peertube/versions/peertube-v5.0.1 /var/www/peertube/peertube-latest
A terminal window showing the creation of symbolic links for Peertube.
  1. Install Peertube using the following Yarn command:
cd ./peertube-latest
yarn install --production --pure-lockfile
A terminal window showing the Peertube installation through Yarn.

Configuring Nginx and SSL

By default, Peertube opens its Internet service on port 9000. While you can access the program from that, it is good practice to create a reverse proxy between the program and a well-known port.

The program’s developers have made a template file that you can use to create your own reverse proxy by running the following command:

sudo cp /var/www/peertube/peertube-latest/support/nginx/peertube /etc/nginx/sites-available/peertube
sudo rm /etc/nginx/sites-enabled/default
A terminal window showing a copy command from Peertube to Nginx.

Configure your new template file by opening it using a text editor:

sudo nano /etc/nginx/sites-available/peertube
A terminal window showing the template file for Peertube.

Inside, change every instance of these two variables: ${WEBSERVER_HOST} and ${PEERTUBE_HOST}.

  • For the ${WEBSERVER_HOST}, replace it with your machine’s FQDN.
  • Meanwhile, replace ${PEERTUBE_HOST} with “127.0.0.1:9000.”
A terminal window showing a modified template file.

Press Ctrl + O, then Ctrl + X to save your file to disk and exit the text editor.

Enable the Peertube Nginx config file and restart Nginx.

sudo ln -s /etc/nginx/sites-available/peertube /etc/nginx/sites-enabled/
sudo systemctl reload nginx

Creating Your SSL Certificate

We are obtaining a new SSL certificate using the free Certbot utility from Let’s Encrypt. (You can also create a wildcard SSL certificate if you intend to use it on multiple (sub)domains.)

sudo certbot

Certbot will scan your Nginx configuration and bring up the list of domains hosted on your server. Enter the number beside the domain for which you want to obtain a new SSL certificate.

Once the SSL certificate is issued, certbot will auto-update your Nginx config file with the correct entry. You just need to reload your Nginx configuration to make sure all is running well.

sudo systemctl reload nginx

Tip: enabling SSL will encrypt all TCP connections to your instance; however, it is better practice to secure your Linux server from the get go.

Configuring and Running Peertube

  1. With both your Nginx server and SSL certificate done, you can now configure your Peertube instance. You can use a template that the developers have made to streamline this process. Run the following commands:
cd $HOME
cp /var/www/peertube/peertube-latest/config/default.yaml /var/www/peertube/config/default.yaml
cp /var/www/peertube/peertube-latest/config/production.yaml.example /var/www/peertube/config/production.yaml
A terminal window showing both the default and production config files.
  1. Open the “production.yaml” file in a text editor:
nano /var/www/peertube/config/production.yaml
  1. Change the hostname: variable to your machine’s FQDN:
A section of a terminal window that show a modified hostname variable.
  1. Generate a random secret for your instance with the following command:
openssl rand -hex 32
A terminal window showing a randomly generated string of characters.

Go back to your “production.yaml” file and paste your random secret beside the peertube: variable.

A section of a terminal window that shows the secret in the config file.
  1. Look for the database: block. Change the suffix: block to “_run.”
A section of a terminal window showing a modified suffix variable.
  1. Change the password: variable to your database account’s password.
A terminal section showing a modified password variable.
  1. Go to the smtp: block and find the hostname: variable. Change that to the hostname of your mail server. Also, change both the username: and password: variables to the credentials of your email account.
A section of a terminal that shows the credentials for my Peertube email account.
  1. Replace the from_address: variable with the email address of your email account.
A section of a terminal window that shows a modified From address.

Once you are done making the changes, press Ctrl + o to save the file and Ctrl + x to exit the file.

Creating a Peertube Service File

To make Peertube run automatically at startup, we are creating a systemd service file for Peertube.

  1. Run the following command to copy the template systemd file to the system:
sudo cp /var/www/peertube/peertube-latest/support/systemd/peertube.service /etc/systemd/system/
A terminal window that shows the template service file being copied to the system.
  1. Reload systemd to apply your new service file.
sudo systemctl daemon-reload
sudo systemctl enable peertube
sudo systemctl start peertube
A terminal window that shows that Peertube is currently running.

Using Peertube

If everything is configured properly, you should be able to access Peertube from your own domain name.

A browser screenshot showing a Peertube instance's first run.

By default, every new Peertube instance creates a root account that you can use as the site’s administrator. To use this, run the following command:

sudo journalctl -u peertube | grep "User password:"
A terminal window that show the randomly generated password for this instance.

Go back to your Peertube website and press the “Login” button on the page’s upper-left corner. Write “root” as your username and paste its password.

A screenshot of a web browser showing the login page for Peertube.

Peertube will greet you with a brief message that contains links to the program’s documentation.

A screenshot of a web browser that shows the initial login message for Peertube.

Once you have reviewed the content of the message, press X on the window’s upper- right corner to start using your Peertube website.

A screenshot of a web browser that shows the Peertube dashboard.

Frequently Asked Questions

Is it possible to use Peertube without a domain name?

No. Peertube requires you to have a valid SSL certificate in your instance. While it is possible to create your own SSL certificate without a domain name, doing this will make your site insecure for other users.

Can I copy the default.yaml file while configuring Peertube?

Peertube depends on the “default.yaml” file for some of its core settings. Without the “default.yaml” file, your instance will most likely render it inaccessible.

Why am I getting a blank page when I open my Peertube website?

This issue is most likely due to a permissions issue with your root peertube directory. By default, Nginx requires every Web folder, as well as its root, to be world readable.

You can fix this issue by running the following command: sudo chmod 755 /var/www/peertube.

Image credit: Unsplash. All alterations and screenshots by Ramces Red.

Is this post useful?
Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Ramces Red
Ramces Red - Staff Writer

Ramces is a technology writer that lived with computers all his life. A prolific reader and a student of Anthropology, he is an eccentric character that writes articles about Linux and anything *nix.