Lemmy Server 0.16.7 on OpenBSD 7.2: Install
Tags: lemmy, fediverse, rust, actix
Published: 2023-01-15 11:17:32

## Summary

This post is a part of [Lemmy 0.16.7 on OpenBSD 7.2](https://obsd.solutions/en/blog/2023/01/15/lemmy-0167-on-openbsd-72/index.html) about [Lemmy](https://join-lemmy.org/).

### Premise

The server is [OpenBSD](https://www.openbsd.org/) and [Rust (rustlang)](https://www.rust-lang.org/) is installed. Also, there is `_lemmy` user whose home directory is `/var/lemmy`.

### Environment

- OS: OpenBSD [7.2](https://www.openbsd.org/72.html)
- Database: [PostgreSQL](https://www.postgresql.org/) [14.5](https://www.postgresql.org/docs/release/14.5/) (_released on_ Aug 11, 2022)
- App
  - Lemmy: 0.16.7
    - based on Rust [1.63](https://blog.rust-lang.org/2022/08/11/Rust-1.63.0.html) + Actix Web [4](https://github.com/actix/actix-web/wiki/v4-Announcement) + Actix 0.13

## Tutorial

### Lemmy server

#### Build Lemmy server

First, get the source of lemmy server:

```console
$ git clone https://github.com/LemmyNet/lemmy.git --recursive --branch 0.16.7 --single-branch
```

The output was:

```console
Cloning into 'lemmy'...
remote: Enumerating objects: 39173, done.
remote: Counting objects: 100% (84/84), done.
remote: Compressing objects: 100% (38/38), done.
remote: Total 39173 (delta 57), reused 46 (delta 46), pack-reused 39089
Receiving objects: 100% (39173/39173), 10.10 MiB | 14.84 MiB/s, done.
Resolving deltas: 100% (27466/27466), done.
Note: switching to '31530e1be409930c6be3e15106991ae35cd6a847'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
(...)
Submodule 'crates/utils/translations' (https://github.com/LemmyNet/lemmy-translations.git) registered for path 'crates/utils/translations'
Cloning into '/var/lemmy/lemmy/crates/utils/translations'...
remote: Enumerating objects: 2208, done.        
remote: Counting objects: 100% (546/546), done.        
remote: Compressing objects: 100% (151/151), done.        
Receiving objects: 100% (2208/2208), 678.71 KiB | 4.06 MiB/s, done.
remote: Total 2208 (delta 439), reused 464 (delta 395), pack-reused 1662        
Resolving deltas: 100% (1820/1820), done.
Submodule path 'crates/utils/translations': checked out '454debaede4cc932ac15fea9bf620cf1daf1ae4c'
```

Go inside:

```console
$ cd lemmy
```

You are now in `/var/lemmy/lemmy` and have lemmy server code written in Rust.

Wait, please. You cannot compile it on OpenBSD soon. There are two (or three) problems to be fixed beforehand.

##### Fix `cargo build` failed due to ssl

First, you have to modify Cargo.lock:

```console
$ nvim Cargo.lock
```

to delete the lines about three packages. They were in 2481-2512 lines in my case:

```diff
  (...)
- [[package]]      
- name = "openssl"
- (...)
- [[package]]
- name = "openssl-probe"
- (...)
- [[package]]
- name = "openssl-sys"
- (...)
- dependencies = [
- (...)
- ]
  (...)
```

Otherwise, the error below will occur in running `cargo build`:

```console
error: failed to run custom build command for `openssl-sys v0.9.72`

Caused by:
  process didn't exit successfully: `/var/lemmy/lemmy/target/release/build/openssl-sys-e37967ef67b14e0e/build-script-main` (exit status: 101)
  --- stdout
  cargo:rustc-cfg=const_fn
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_OPENBSD_OPENSSL_LIB_DIR
  X86_64_UNKNOWN_OPENBSD_OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
  OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_OPENBSD_OPENSSL_INCLUDE_DIR
  X86_64_UNKNOWN_OPENBSD_OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
  OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_OPENBSD_OPENSSL_DIR
  X86_64_UNKNOWN_OPENBSD_OPENSSL_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_DIR
  OPENSSL_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_NO_PKG_CONFIG
  (...)
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
  cargo:rustc-link-lib=ssl
  cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-openbsd
  cargo:rustc-link-lib=crypto
  (...)
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
  cargo:rerun-if-changed=build/expando.c
  OPT_LEVEL = Some("3")
  TARGET = Some("x86_64-unknown-openbsd")
  HOST = Some("x86_64-unknown-openbsd")
  CC_x86_64-unknown-openbsd = None
  CC_x86_64_unknown_openbsd = None
  HOST_CC = None
  CC = None
  CFLAGS_x86_64-unknown-openbsd = None
  CFLAGS_x86_64_unknown_openbsd = None
  HOST_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("false")
  CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2")
  running: "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-Wall" "-Wextra" "-E" "build/expando.c"
  exit status: 0
  cargo:rustc-cfg=osslconf="OPENSSL_NO_BUF_FREELISTS"
  (...)
  cargo:rustc-cfg=osslconf="OPENSSL_NO_SEED"
  cargo:conf=OPENSSL_NO_BUF_FREELISTS,OPENSSL_NO_COMP,OPENSSL_NO_KRB5,OPENSSL_NO_PSK,OPENSSL_NO_SRP,OPENSSL_NO_SSL3_METHOD,OPENSSL_NO_SEED
  cargo:rustc-cfg=libressl
  cargo:rustc-cfg=libressl251
  (...)
  cargo:rustc-cfg=libressl332
  cargo:libressl_version_number=3060000f

  --- stderr
  thread 'main' panicked at '

  This crate is only compatible with OpenSSL (version 1.0.1 through 1.1.1, or 3.0.0), or LibreSSL 2.5
  through 3.4.1, but a different version of OpenSSL was found. The build is now aborting
  due to this version mismatch.

  ', /home/nabbisen/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.72/build/main.rs:295:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
```

##### Fix `cargo build` failed due to `rust-fmt`

Next, switch back to you own user and install the package:

```console
$ exit

$ doas pkg_add rust-rustfmt
```

The output was:

```console
quirks-6.42 signed on 2023-01-13T18:22:41Z
rust-rustfmt-1.63.0p0: ok
```

Then, switch to `_lemmy` again:

```console
$ doas su - _lemmy

$ cd lemmy
```

and edit `Cargo.toml`:

```console
$ nvim Cargo.toml
```

to add the dependency to `rustfmt`:

```diff
  (...)
  [dependencies]
  (...)
+  rustfmt = "*"
```

Besides, in my case, the actual version was "0.10.0".

Otherwise, `rustfmt` doesn't work in compiling `translations` in `lemmy_utils`. Therefore you will meet the error below in running `cargo build`:

```console
error: failed to run custom build command for `lemmy_utils v0.16.7 (/var/lemmy/lemmy/crates/utils)`

Caused by:
  process didn't exit successfully: `/var/lemmy/lemmy/target/release/build/lemmy_utils-c59f10bc7bf13f2b/build-script-build` (exit status: 1)
  --- stdout
  cargo:rerun-if-changed=translations/email/en.json
  cargo:rerun-if-changed=translations/email/ko.json
  cargo:rerun-if-changed=translations/email/fi.json
  cargo:rerun-if-changed=translations/email/pt.json

  --- stderr
  Error: Fmt(Os { code: 2, kind: NotFound, message: "No such file or directory" })
warning: build failed, waiting for other jobs to finish...
```

##### Fix `cargo build` failed due to PostgreSQL libraries

```console
$ exit

$ doas pkg_add postgresql-client postgresql-contrib
```

The output was:

```console
quirks-6.42 signed on 2023-01-13T18:22:41Z
postgresql-client-14.5: ok
postgresql-contrib-14.5: ok
```

Besides, as to `postgresql-client`, possibly it has been already installed.

Then switch to `_lemmy` again:

```
$ doas su - _lemmy

$ cd lemmy
```

Well, here are supplementary descriptions.

If `postgresql-client` is not installed, you will probably meet the error below.
The lack of `libpg` causes it:

```console
$ cargo build --release
(...)
   Compiling lemmy_server v0.16.7 (/var/lemmy/lemmy)
error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "/tmp/rustcSracqC/symbols.o" 
  (...)
   "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-Wl,-O1" "-nodefaultlibs"
  = note: ld: error: unable to find library -lpq
          cc: error: linker command failed with exit code 1 (use -v to see invocation)
          

error: could not compile `lemmy_server` due to previous error
```

If `postgresql-contrib` is not installed, it will print the error below:

```console
$ cargo run --release
(...)
    Finished release [optimized] target(s) in 0.37s
     Running `target/release/lemmy_server`
Error: LemmyError { message: None, inner: Failed with: could not open extension control file "/usr/local/share/postgresql/extension/pgcrypto.control": No such file or directory, context: "SpanTrace" }
```

##### Compile the server

Now `cargo build` is ready. Let's run:

```console
$ cargo build --release
```

It will work and end with success :)

```console
(...)
   Compiling pq-sys v0.4.6
(...)
   Compiling diesel v1.4.8
   Compiling lemmy_server v0.16.7 (/var/lemmy/lemmy)
    Finished release [optimized] target(s) in 14m 36s
```

###### In case of failure on missing PostgreSQL libraries (Optional)

I have met error which seemed to have something to do with missing PostgreSQL libraries.

In case that you doubt you meet the same error, using `env` to specify the location might solve it:

```console
$ env PQ_LIB_DIR=/usr/local/lib \
      cargo build --release
```

#### Configure (Optional)

```console
$ cp -p config/config.hjson config/config.hjson.org
```

Edit as needed:

```console
$ nvim config/config.hjson
```

For example, overwrite host (FQDN), database connection and pict-rs server:

```diff
-   host: lemmy-alpha
+   host: <your-fqdn>
  (...)
+   bind: 127.0.0.1
+   #port: 8536
+   database: {
+     # Username to connect to postgres
+     user: "lemmy"
+     # Password to connect to postgres
+     password: "lemmy"
+     # Host where postgres is running
+     host: "127.0.0.1"
+     # Port where postgres can be accessed
+     port: 5432
+     # Name of the postgres database for lemmy
+     database: "lemmy"
+     # Maximum number of active sql connections
+     pool_size: 5
+   }
+   pictrs_url: "http://127.0.0.1:8080"
  }
```

##### Too few `pool_size` may cause server failure due to SpanTrace

Be careful you may have to set "3" or greater at at `pool_size` in above.

When I set "2", the server couldn't return response to client (of `curl`) and I got the error below:

```console
$ cargo run --release
    Finished release [optimized] target(s) in 0.34s
     Running `target/release/lemmy_server`
(...)
2023-01-14T07:23:24.946120Z  INFO blocking operation: lemmy_server::code_migrations: Running user_updates_2020_04_02
(...)
2023-01-14T07:23:24.953272Z  INFO lemmy_server::scheduled_tasks: Updating active site and community aggregates ...
Starting http server at 0.0.0.0:8536
2023-01-14T07:23:24.977263Z  INFO lemmy_server::scheduled_tasks: Done.
2023-01-14T07:23:24.977313Z  INFO lemmy_server::scheduled_tasks: Updating banned column if it expires ...
2023-01-14T07:23:24.977616Z  INFO lemmy_server::scheduled_tasks: Reindexing table concurrently post_aggregates ...
(...)
2023-01-14T07:23:54.980299Z  WARN background_jobs_actix::server: Not restarting ticker, arbiter is dead
2023-01-14T07:23:54.980444Z  INFO Worker{worker.id=ef80d3fd-8ffb-4a12-8917-f82d1a26dc90 worker.queue=default worker.operation.id=417634dd-b934-4c31-bde3-ed38c515a22a worker.operation.name=closing}: background_jobs_actix::worker: Worker closing
(...)
2023-01-14T07:23:55.003481Z  INFO Worker{worker.id=145bad22-bc83-4ba8-9155-0a77b86cf4b5 worker.queue=default worker.operation.id=432facee-ed0c-471c-bb7f-d19389fa53a8 worker.operation.name=closing}: background_jobs_actix::worker: Worker closing
2023-01-14T07:23:55.003562Z  WARN background_jobs_actix::worker: Not restarting worker, Arbiter is dead
2023-01-14T07:23:55.003643Z  INFO Worker{worker.id=4626ccc8-48a9-41ac-b128-9c23b62b33cb worker.queue=default worker.operation.id=32df578b-c3ea-4fad-bdeb-02ffeb295f5b worker.operation.name=closing}: background_jobs_actix::worker: Worker closing
2023-01-14T07:23:55.003717Z  WARN background_jobs_actix::worker: Not restarting worker, Arbiter is dead
Error: LemmyError { message: None, inner: timed out waiting for connection, context: "SpanTrace" }
```

#### Run Lemmy server

Now you are ready to start the server. Let's run:

```console
$ cargo run --release
```

In my case, it started with

```console
2023-01-14T07:24:11.308525Z  INFO blocking operation: lemmy_server::code_migrations: Running user_updates_2020_04_02
(...)
```

It was followed by "Starting http server at 0.0.0.0:8536":

```console
(...)
2023-01-14T07:24:11.315945Z  INFO lemmy_server::scheduled_tasks: Updating active site and community aggregates ...
Starting http server at 0.0.0.0:8536
2023-01-14T07:24:11.339697Z  INFO actix_server::builder: Starting 1 workers
2023-01-14T07:24:11.340446Z  INFO actix_server::server: Actix runtime found; starting in Actix runtime
federation enabled, host is lemmy-alpha
2023-01-14T07:24:11.342172Z  INFO lemmy_server::scheduled_tasks: Done.
(...)
```

Yay. Building Lemmy server is completed !!

## Conclusion

Well done :) Go on to the next step.

<center>[Return](https://obsd.solutions/en/blog/2023/01/15/lemmy-0167-on-openbsd-72/index.html)<center>