# Updated - 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"
   # lookup the latest tagged release
   regex='\/([0-9|\.]*)<\/id>' && response=$(curl -s https://github.com/LemmyNet/lemmy/releases.atom) && [[ $response =~ $regex ]] && latestTag="${BASH_REMATCH[1]}"
   # clone the lemmy git repository
   git clone https://github.com/LemmyNet/lemmy.git ./server
   # change directory to the source code
   cd server
   # checkout the latest tag
   git checkout $latestTag
   # update submodules
   git submodule update --init --recursive
   # generate a random 32 character string
   openssl rand -base64 32
   # edit the lemmy config file
   nano ./config/config.hjson
3. Set the following values in the conf file

   database: {
   password: L3mmy
   }
   hostname: <% DNS or IP of host %>
   bind: "127.0.0.1"
   tls_enabled: false
   jwt_secret: "<% Random string from above %>"

4. Press CTRL+O, Enter, CTRL+X to write the changes
5. Continue with the following commands
   # set the database connection variable
   LEMMY_DATABASE_URL=postgres://lemmy:L3mmy@localhost:5432/lemmy
   # build lemmy
   cargo build --release
   # change directory out of lemmy server
   cd ..
   # write the version to .txt
   echo $latestTag > /tmp/lemmy_version.txt
   # 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
   # checkout the latest tag
   git checkout $(cat /tmp/lemmy_version.txt)
   # update submodules
   git submodule update --init --recursive
   # install npm dependencies
   npm install --force
   # build lemmy-ui
   yarn build:prod
   # exit lemmy shell
   exit
   # set owner of /opt/lemmy
   sudo chown lemmy:lemmy /opt/lemmy/ -R
   # create lemmy service file
   sudo nano /etc/systemd/system/lemmy.service
6. 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

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

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

10. 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
11. 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

12. Press CTRL+O, Enter, CTRL+X to write the changes
13. 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
14. Open a web browser and navigate to http://DNSorIP:1234
15. Enter a username, email and password to create a site administrator account > Click Sign Up
16. Enter a site name and any additional optional values > Click Create
17. Welcome to Lemmy

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