How to Install Lemmy on Ubuntu Server Latest

Lemmy is an open-source, self-hosted, federated, Reddit-like platform for discussions, analysis, and sharing links. It is a great alternative to centralized platforms like Reddit and allows users to create their own communities, and manage their discussions.

In this tutorial, we will show you how to install Lemmy on Ubuntu Server Latest.

Prerequisites

Before starting, make sure you have met the following prerequisites:

Step 1 - Update the Server

Before installing any packages, it is recommended to update your server to the latest version. To do this, log in to your server as a non-root user with sudo privileges and run the following command:

sudo apt update && sudo apt upgrade

Step 2 - Install Dependencies

Lemmy requires some dependencies to be installed on the server. You can install them using the following command:

sudo apt install -y cargo git nginx certbot python3-certbot-nginx postgresql postgresql-contrib libssl-dev pkg-config build-essential

Step 3 - Clone the Lemmy Repository

Next, you need to clone the Lemmy repository from GitHub. To do this, run the following command:

git clone https://github.com/LemmyNet/lemmy.git

This will download the latest version of the Lemmy source code to your server.

Step 4 - Build Lemmy

Now, you can build the Lemmy Rust binaries using the cargo package manager.

First, navigate to the lemmy directory:

cd lemmy

Then, run the following command:

RUSTFLAGS="-C target-cpu=native" cargo build --release

This will take some time, as it will compile all the Rust dependencies and the Lemmy source code. Once completed, the binaries will be stored in the target/release directory.

Step 5 - Configure Nginx

Now, you need to configure Nginx as a reverse proxy server for Lemmy. Create a new Nginx virtual host configuration file:

sudo nano /etc/nginx/sites-available/lemmy.example.com

Paste the following Nginx configuration:

server {
    listen 80;
    listen [::]:80;

    server_name lemmy.example.com;

    # Redirect HTTP requests to HTTPS
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name lemmy.example.com;

    ssl_certificate /etc/letsencrypt/live/lemmy.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/lemmy.example.com/privkey.pem;

    location / {
        # Proxy settings
        proxy_pass http://localhost:8536;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # Websocket support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

Make sure you replace lemmy.example.com with your domain name.

Save and close the file.

Now, create a symbolic link to enable the virtual host configuration:

sudo ln -s /etc/nginx/sites-available/lemmy.example.com /etc/nginx/sites-enabled/

Test the Nginx configuration and restart the server:

sudo nginx -t && sudo systemctl restart nginx.service

Step 6 - Setup PostgreSQL

Lemmy requires a PostgreSQL database to store the data. Create a new database and user for Lemmy using the following commands:

sudo -u postgres createuser -P lemmy
sudo -u postgres createdb -O lemmy lemmy

You will be prompted to enter a password for the user.

Step 7 - Configure Lemmy

In this step, you need to configure the Lemmy server by creating a .env file.

Navigate to the lemmy directory:

cd /home/your_username/lemmy

Create a new .env file and paste the following configuration:

ROCKET_ADDRESS=localhost
DATABASE_URL=postgres://lemmy:lemmy_password@localhost/lemmy
FRONT_END_ADDRESS=https://lemmy.example.com
SERVER_ADMIN_EMAIL=your_email_address
SITE_NAME=Lemmy
SITE_HOST=https://lemmy.example.com
SITE_PORT=80
SECRETKEY=your_secret_key
REQUIRE_ACCOUNT_CONFIRMATION=false
ALLOW_ANONYMOUS_POSTING=true
SMTP_TRANSPORT=smtp
SMTP_SERVER=smtp.gmail.com
SMTP_PORT=465
SMTP_USERNAME=your_gmail_address
SMTP_PASSWORD=your_gmail_password
DEFAULT_FROM_EMAIL=your_gmail_address
GLOBAL_OAUTH_CLIENT_ID=your_oauth_client_id
GLOBAL_OAUTH_CLIENT_SECRET=your_oauth_client_secret
GLOBAL_OAUTH_REDIRECT_URI=https://lemmy.example.com/api/v3/auth/callback

Make sure you replace the values with your own values:

Save and close the file.

Step 8 - Start Lemmy

Now, you can start the Lemmy server using the following command:

sudo RUST_BACKTRACE=1 nohup ./target/release/lemmy &> /dev/null &

This will start the Lemmy server in the background with logging disabled. You can now access your Lemmy site by visiting https://lemmy.example.com.

Step 9 - Obtain SSL Certificate

To enable HTTPS on your Lemmy site, you need to obtain an SSL certificate from Let's Encrypt using Certbot.

To obtain a certificate, run the following command:

sudo certbot --nginx -d lemmy.example.com

This will guide you through the process of obtaining a certificate and configuring Nginx to use it.

Conclusion

In this tutorial, you have learned how to install and configure Lemmy on Ubuntu Server Latest. You can now create communities and manage your discussions on your self-hosted platform.

If you want to self-host in an easy, hands free way, need an external IP address, or simply want your data in your own hands, give IPv6.rs a try!