# Install Lemmy Self-Hosted Reddit Alternative on Linux

## What is Lemmy?

Lemmy is an open-source, federated link aggregator similar to Reddit and built with Rust. -https://lemmy.ml/

1. Log into the Linux device
2. Run the following commands in a terminal window
   # update software repositories
   sudo apt update
   # install available software updates
   sudo apt upgrade -y
   # install prerequisites
   sudo apt install git build-essential gcc libssl-dev pkg-config libpq-dev curl gnupg2 espeak postgresql -y
   # enable the postgresql service and start it
   sudo systemctl enable postgresql --now
   # connect to postgresql
   sudo -u postgres psql postgres
   # create lemmy database user
   create user lemmy with password 'L3mmy' superuser;
   # create lemmy database
   create database lemmy with owner lemmy;
   # close postgresql connection
   exit
   # add nodejs software repository
   curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -
   # install nodejs
   sudo apt install nodejs -y
   # install yarn
   sudo npm install -g yarn
   # create lemmy user
   sudo useradd -m -d /opt/lemmy lemmy
   # install rust, enter 1 at the prompt
   curl https://sh.rustup.rs -sSf | sh
   # configure the shell
   source $HOME/.cargo/env
   # clone the lemmy git repository
   git clone https://github.com/LemmyNet/lemmy.git ./server
   # change directory to the source code
   cd server
   # build lemmy
   cargo build --release
   # change directory out of lemmy server
   cd ..
   # move lemmy to /opt/lemmy
   sudo mv ./server /opt/lemmy/
   # switch user to lemmy
   sudo su lemmy
   # change directory to lemmy home
   cd ~
   # print working directory, should output /opt/lemmy
   pwd
   # clone lemmy frontend
   git clone https://github.com/LemmyNet/lemmy-ui.git --recurse-submodules ./lemmy-ui
   # change directory to lemmy-ui
   cd lemmy-ui
   # clean npm cache
   npm cache clean --force
   # install npm dependencies
   npm install
   # fix npm vulnerabilities
   npm audit fix
   # build lemmy-ui
   yarn build:prod
   # exit lemmy shell
   exit
   # create lemmy service file
   sudo nano /etc/systemd/system/lemmy.service
3. Paste the following configuration into lemmy.service

   [Unit]
   Description=Lemmy

   [Service]
   User=lemmy
   Group=lemmy
   Environment=LEMMY_DATABASE_URL=postgres://lemmy:L3mmy@localhost:5432/lemmy
   ExecStart=/opt/lemmy/server/target/release/lemmy_server
   WorkingDirectory=/opt/lemmy/server

   [Install]
   WantedBy=multi-user.target

4. Press CTRL+O, Enter, CTRL+X to write the changes
5. Continue with the following commands
   # create lemmy-ui service bash file
   sudo nano /opt/lemmy/lemmy-ui/lemmy-ui.sh
6. Paste the following configuration into lemmy-ui.sh

   #!/usr/bin/bash
   /usr/bin/node /opt/lemmy/lemmy-ui/dist/js/server.js

7. Continue with the following commands
   # make lemmy-ui.sh executable
   sudo chmod +x /opt/lemmy/lemmy-ui/lemmy-ui.sh
   # create lemmy service file
   sudo nano /etc/systemd/system/lemmy-ui.service
8. Paste the following configuration into lemmy-ui.service

   [Unit]
   Description=Lemmy-UI

   [Service]
   ExecStart=/opt/lemmy/lemmy-ui/lemmy-ui.sh
   Restart=always
   User=lemmy
   Group=lemmy
   Environment=PATH=/usr/bin:/usr/local/bin
   Environment=NODE_ENV=production
   WorkingDirectory=/opt/lemmy/lemmy-ui

   [Install]
   WantedBy=multi-user.target

9. Press CTRL+O, Enter, CTRL+X to write the changes
10. Continue with the following commands
    # reload systemd services
    sudo systemctl daemon-reload
    # start lemmy service on boot and now
    sudo systemctl enable lemmy --now
    # start lemmy-ui service on boot and now
    sudo systemctl enable lemmy-ui --now
11. Open a web browser and navigate to http://DNSorIP:1234
12. Enter a username, email and password to create a site administrator account > Click Sign Up
13. Enter a site name and any additional optional values > Click Create
14. Welcome to Lemmy

Sources: https://join-lemmy.org/docs/en/contributing/local_development.html,
https://join-lemmy.org/docs/en/administration/from_scratch.html