Installing Forem in Linux
Tags: forem, selfhosting, guide
Published: 2020-11-30 10:35:41

This post is on the request of @benki

In this article, I focus less on my commentary and prefer more clarity in command execution. So if you find any words others than shell commands, take a good look at them.

One more tip while following this guide, single command lies in a single line.

This guide will be useful for anyone who wants to install forem in Linux. In a way, this is an alternative method to install Forem without Docker or Podman

> ## Updating our system and Downloading Forem
```
sudo apt update
sudo apt upgrade
git clone https://github.com/forem/forem.git
cp ~/forem/.env_sample ~/forem/.env
```

> ## Ruby Installation
```
sudo apt install git curl autoconf bison build-essential \
    libssl-dev libyaml-dev libreadline6-dev zlib1g-dev \
    libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev
```

```
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc
exec $SHELL
```
```
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
exec $SHELL
```
```
rbenv install $(cat ~/forem/.ruby-version)
rbenv global $(cat ~/forem/.ruby-version)
```

> ## Install NVM and yarn
```
curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh -o install_nvm.sh
```
```
bash install_nvm.sh
source ~/.profile
nvm install $(cat ~/forem/.nvmrc)
nvm use $(cat ~/forem/.nvmrc)
```
```
npm install -g yarn
```

> ## Installing Postgre SQL


```
sudo apt-get install postgresql postgresql-contrib libpq-dev -y
```
In the coming command, Replace the term "ubuntu" with your own username.
```
sudo -u postgres createuser -s ubuntu
createdb
sudo -u ubuntu psql
```
Now within the psql Use `\password` command to create a password for the user ubuntu, in which we will create a database (later on).

You can exit psql by using the command `\quit`; this will help you get into the original bash/terminal of your VPS.

Let's update the `DATABASE_URL` and the `DATABASE_URL_TEST` accordingly in the `.env` file.

```
nano ~/forem/.env
```
```
DATABASE_URL="postgresql://ubuntu:mypassword@localhost:5432/$DATABASE_NAME"
DATABASE_URL_TEST="postgresql://ubuntu:mypasswordk@localhost:5432/$DATABASE_NAME_TEST"
```

> ## Installing Imagemagick

```
sudo apt update && sudo apt install imagemagick
```

> ## Installing and ConfiguringRedis

```
sudo apt install redis-server -y
```
```
sudo nano /etc/redis/redis.conf
```
Navigate to find the variable `supervised` and change it's value `systemd`
```
sudo systemctl restart redis.service
```

> ## Installing Elastic search

```
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-oss-7.5.2-amd64.deb
```
```
sudo dpkg -i elasticsearch-oss-7.5.2-amd64.deb
```
If you are someone who is trying to experiment with this Forem in a low-end server, maybe around 1GB or 2GB ram; It is mandatory for you to do edit the memory heap. And follow these if you are one among them, or else leave. 

> If you are not sure about this, skip and continue with the next step, but if you are facing any error in starting Elasticsearch try referring to this.

```
sudo nano /etc/elasticsearch/jvm.options
```
* First, un-comment the value of Xmx and Xms.
* Next, modify the value of -Xms and -Xmx to no more than 50% of your physical RAM

```
# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space
# Use 128m if 1GB ram and 256m if 2GB ram

-Xms128m
-Xmx128m
```
Now let's start Elasticsearch
```
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable elasticsearch.service
sudo systemctl start elasticsearch.service
```
> ## Installing ImgProxy
```
git clone https://github.com/imgproxy/imgproxy.git
cd imgproxy
```
```
sudo add-apt-repository ppa:dhor/myway
sudo apt-get update
sudo apt-get install libvips-dev
```
```
sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt-get update
sudo apt-get install golang-go
```
```
sudo CGO_LDFLAGS_ALLOW="-s|-w" \
  go build -o /usr/local/bin/imgproxy
```
Now let's configure the `.env` for Imgproxy
* Generate a key/salt pair by running the following in your terminal twice. Copy those values to your .env in the next step

```
echo $(xxd -g 2 -l 64 -p /dev/random | tr -d '\n')
```
We have one random string; but we need two of them, so let's run this command again.
```
echo $(xxd -g 2 -l 64 -p /dev/random | tr -d '\n')
```
Now we have 2 random strings; we will one of them as `key` and the other as `salt`. This can be done by adding few lines in `.env` file.

```
nano ~/forem/.env
```
Now add these three lines to it and save the file.
```
IMGPROXY_ENDPOINT='http://localhost:8080'
IMGPROXY_KEY='1b1c9aae804e070b0864f2547fba7ce8ff31bf7..........'
IMGPROXY_SALT='8c6d449d4fc2cada5bab538826cae709d2ade9f.........'
```
Replace, the `key` and `salt` with the values you generated in few steps back. (You can interchange those values, but as a convention, I use the first one as key and the later as salt)

Also store these values in this fashion, so that we can use this command to start our Imgproxy.

```
IMGPROXY_KEY='your key' IMGPROXY_SALT='your salt' imgproxy
```

Now we have all the required side packs for our installation
> ## Installing and Starting Forem

If you are using a VPS, it is mandatory for you to update your `APP_DOMAIN` value with your VPS IP address in the `.env` file.

* you can find your VPS IP address by using the command `ifconfig`
* Use this command to edit your **.env** file: `nano ~/forem/.env`


``` 
sudo apt-get install libcurl4 libcurl4-openssl-dev -y
cd ~/forem
gem install bundler
yarn install
bin/setup
rails db:reset
rails data_updates:run
```
> Now open 3 Terminal, the three boxes below indicate 3 different terminals

* Terminal 01
```
IMGPROXY_KEY='your key' IMGPROXY_SALT='your salt' imgproxy
```
* Terminal 02
```
./bin/webpack
bundle exec sidekiq -t 25
```
* Terminal 03
```
bin/rails s -p 3000 -b 0.0.0.0
```