Skip to content

Manual Installation Guide

evazion edited this page Jan 28, 2024 · 26 revisions

This guide explains how to install Danbooru manually, without using Docker. Manual installation is not recommended or supported, since it requires installing numerous dependencies from source, which is time-consuming and error-prone. Try the Docker image first if you're just getting started.

The canonical installation process is listed in the Dockerfile. This guide is adapted from the Dockerfile. When in doubt, refer to the Dockerfile.

Prerequisites

You will need a Linux machine and some Linux familiarity. If you don't have a Linux machine, you can follow the virtual machine installation guide to install Ubuntu in a virtual machine.

This guide has been tested on Ubuntu 22.10. Other distros will work, but you will have to adapt it for your distro's package manager.

Danbooru requires 1GB of RAM to run. For best performance, you should use a SSD for the database and thumbnail images.

Dependencies

Danbooru has the following OS-level dependencies:

  • Ruby 3.2.3
  • Node.js 20.10.0 (for building the JS and CSS files)
  • Postgres 14+ (for the database)
  • Mozjpeg 4.1.5 (for generating thumbnails)
  • Libvips 8.14.2 (for generating thumbnails)
  • FFmpeg 6.1.1 (for processing videos and GIFs)
  • ExifTool 12.70 (for extracting image metadata)
  • Perl (for ExifTool)
  • Mkvtoolnix (for converting Pixiv ugoira files to .webm format)
  • Rclone (optional, for storing images on cloud storage)
  • OpenResty (optional, for serving images)

These exact versions must be installed. Most distros don't ship these exact versions, so you will need to build them from source. Do not use the versions provided by your distro, as difficult-to-debug problems will arise if you don't have the correct versions of each dependency.

Installation

Install OS dependencies

First install various libraries and programs needed later on:

sudo apt-get update
sudo apt-get install curl ca-certificates build-essential cmake nasm git pkg-config perl perl-modules \
  meson libgirepository1.0-dev libarchive-zip-perl mkvtoolnix rclone gnupg openssl libpq5 libpq-dev \
  libgmpxx4ldbl zlib1g zlib1g-dev libfftw3-3 libwebp7 libwebpmux3 libwebpdemux2 liborc-0.4.0 liblcms2-2 \
  libpng16-16 libexpat1 libglib2.0 libgif7 libexif12 libheif1 libvpx7 libvpx-dev libdav1d6 libdav1d-dev \
  libseccomp2 libseccomp-dev libjemalloc2 libarchive13 libfftw3-dev libwebp-dev liborc-dev liblcms2-dev \
  libpng-dev libexpat1-dev libglib2.0-dev libgif-dev libexif-dev libheif-dev libssl-dev libgmp-dev \
  libyaml-dev libffi-dev libreadline-dev libsodium23

These are the requirements for Ubuntu 22.10. If you're running another distro or another version of Ubuntu, you may have to adapt the names or versions of some of these packages.

Install asdf

Asdf is a package manager used to install Ruby and Node.

git clone https://github.com/asdf-vm/asdf.git ~/.asdf
echo ". $HOME/.asdf/asdf.sh" >> ~/.bashrc
exec bash # Restart the shell so that the above line loads.

asdf --version # Test that it works

See the asdf documentation for additional help.

Install Ruby

Ruby is the language Danbooru is written in.

You can optionally install Rust to enable YJIT support for Ruby. YJIT may improve performance (but probably not noticeably for a single-user booru).

apt-get install rustc # Optional. Installing Rust will enable YJIT support.

asdf plugin add ruby
asdf install ruby 3.2.3
asdf global ruby 3.2.3

export RUBY_YJIT_ENABLE=1 # Optional, to enable YJIT if YJIT support was built.
ruby --version

Install Node.js

Node.js is used to run Javascript tools to process the CSS and Javascript files.

asdf plugin add nodejs
asdf install nodejs 20.10.0
asdf global nodejs 20.10.0
npm install -g yarn

node --version

Install MozJPEG

MozJPEG is a libjpeg replacement that has better quality and compression performance than libjpeg. MozJPEG is used for generating thumbnails.

curl -L https://github.com/mozilla/mozjpeg/archive/refs/tags/v4.1.5.tar.gz | tar -C $HOME -xzvf -
cd ~/mozjpeg-4.1.5

mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr/local ..
make -j
sudo make install
sudo ldconfig

cjpeg -version

See the MozJPEG documentation for additional help.

Install libvips

Libvips is the image library used for processing images and generating thumbnails.

curl -L https://github.com/libvips/libvips/releases/download/v8.14.2/vips-8.14.2.tar.xz | tar -C $HOME -xJvf -
cd ~/vips-8.14.2

meson build --prefix /usr/local --buildtype release
cd build
meson compile
meson install
ldconfig

vips --version

It is very important that you install the correct version of libvips, and don't use the version supplied by your distro. Most distros ship old versions of libvips that have known bugs and incompatibilities with Danbooru.

See the libvips documentation for additional help.

Install FFmpeg

FFmpeg is used for processing video files, Pixiv ugoiras, and animated GIFs.

curl -L https://github.com/FFmpeg/FFmpeg/archive/refs/tags/n6.1.1.tar.gz | tar -C $HOME -xzvf -
cd ~/FFmpeg-n6.1.1

./configure --disable-ffplay --disable-network --disable-doc --enable-libvpx --enable-libdav1d
make -j
sudo cp ffmpeg ffprobe /usr/local/bin

ffmpeg -version
ffprobe -version

See the FFmpeg documentation for additional help.

Install Exiftool

ExifTool is used for extracting metadata from image and video files.

curl -L https://github.com/exiftool/exiftool/archive/refs/tags/12.70.tar.gz | tar -C $HOME -xzvf -
cd ~/exiftool-12.70

perl Makefile.PL
make -j
sudo make install

exiftool -ver
perl -e 'require Compress::Zlib' # Double check that Perl dependencies are installed; these will return an error if not.
perl -e 'require Archive::Zip'
perl -e 'require Digest::MD5'

See the ExifTool documentation for additional help.

Install PostgreSQL client

PostgreSQL is the database used by Danbooru. The PostgreSQL client psql is needed to set up the database.

# This defines $DISTRIB_CODENAME. DISTRIB_CODENAME=kinetic for Ubuntu 22.10.
. /etc/lsb-release

curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee -a /usr/share/keyrings/postgresql.gpg
echo "deb [signed-by=/usr/share/keyrings/postgresql.gpg] http://apt.postgresql.org/pub/repos/apt $DISTRIB_CODENAME-pgdg main" | sudo tee -a /etc/apt/sources.list.d/pgdg.list

sudo apt-get update
sudo apt-get install postgresql-client-15

psql --version

Install OpenResty

OpenResty is a fork of the Nginx web server that includes some additional useful plugins that make configuration easier. Nginx will also work just as well.

You can skip this step if you're running a private Danbooru instance just for yourself, and you don't mind reduced image serving performance (still perfectly fine for a single user).

sudo apt-get install libpcre++-dev

curl -L https://openresty.org/download/openresty-1.21.4.1.tar.gz | tar -C $HOME -xzvf -
cd ~/openresty-1.21.4.1

# https://github.com/openresty/docker-openresty/blob/master/alpine/Dockerfile
./configure -j$(nproc) --prefix=/usr/local \
  --with-threads --with-compat --with-pcre-jit --with-file-aio \
  --with-http_gunzip_module --with-http_gzip_static_module \
  --with-http_realip_module --with-http_ssl_module \
  --with-http_stub_status_module --with-http_v2_module
make -j
sudo make install

Install Danbooru

At this point all the OS-level prerequisites have been installed, and we can move on to installing Danbooru itself.

First download Danbooru's code:

git clone https://github.com/danbooru/danbooru.git ~/danbooru
cd ~/danbooru

Next install Ruby and Javascript libraries needed by Danbooru:

bundle install -j
yarn install

Next compile Danbooru's CSS and Javascript files:

bin/rails assets:precompile

At the point Danbooru itself is runnable, but we still need to set up the database.

Configure PostgreSQL

Next we need to set up PostgreSQL. You can either run PostgreSQL in Docker, or install it manually. Running PostgreSQL in Docker is highly recommended because it's easier to set up, it's easier to upgrade versions later on, and it's easier to start over if you mess up.

Run PostgreSQL in Docker

Run the following command to start PostgreSQL:

sudo docker run --rm -it --shm-size=8g --network host -e POSTGRES_USER=danbooru -e POSTGRES_HOST_AUTH_METHOD=trust -v $HOME/danbooru-postgres:/var/lib/postgresql/data ghcr.io/danbooru/postgres:14.1 -c listen_addresses=localhost

Run the following in a separate terminal to test that it works:

psql -l postgresql://danbooru@localhost

The database files will be stored in $HOME/danbooru-postgres. You can make a backup by shutting down PostgreSQL and copying these files.

This makes it so that any local user can connect to the PostgreSQL server without a password. This is fine if you're just running Danbooru for personal use on your local machine, but it's not very secure for a public-facing site. To require a password, replace -e POSTGRES_HOST_AUTH_METHOD=trust with -e POSTGRES_PASSWORD=your_password_here.

See the PostgreSQL Docker documentation for additional help.

Run PostgreSQL manually

First install the PostgreSQL server:

sudo apt-get install postgresql

Next we need to configure authentication so that users can connect to the server. Edit the /var/lib/postgresql/15/main/pg_hba.conf file (the location of this file may vary) and replace it with the following:

local all all           trust
host  all all localhost trust

This makes it so that any local user can connect to the PostgreSQL server without a password. To require a password, replace trust with md5. To make your server accessible outside of localhost (DO NOT do this without a password), change localhost to all.

See the PostgresSQL documentation for details on the pg_hba.conf file.

Now start the server:

sudo systemctl enable postgresql
sudo systemctl start postgresql

Test that it works:

psql -l -U postgres

Now create a new user and database:

createuser -U postgres --superuser danbooru --pwprompt
createdb -U danbooru danbooru2

This will create a new database called danbooru2, owned by the user danbooru. Now test it:

psql -U danbooru -l

Initialize the Danbooru database

Now that the PostgreSQL server is running (whether in Docker or installed manually), we need to initialize the Danbooru database:

bin/rails db:prepare

Running Danbooru

Now that Danbooru is installed and the database is configured, you're ready to run Danbooru:

bin/rails server

Open http://localhost:3000 in your browser. You should see an empty front page.

Go to http://localhost:3000/users/new to create a new account. The first account you create will be set as the Owner user.

Configuring Danbooru

Now you're ready to configure your installation. Copy config/danbooru_default_config.rb to config/danbooru_local_config.rb and edit it:

cp config/danbooru_default_config.rb config/danbooru_local_config.rb

Alternatively, you can use environment variables to set config options. You can copy .env to .env.local and edit that, or you can set e.g. DANBOORU_APP_NAME=MyBooru bin/rails server on the command line.

At the very minimum, you need to change secret_key_base to a random secret password, otherwise users will be logged out on every restart.

If you're running a public website, then you need to change canonical_url to the URL of your website. For example, if your site is https://booru.example.com, then you need to change canonical_url to that.

If you plan to upload from sites like Pixiv, Twitter, etc, you will need to configure passwords and API keys, otherwise uploads from these sites may not work. See the default config file for details.

By default, image files will be stored in public/data. See the storage_manager option to store files in a different location, or on cloud storage or a remote machine.

After editing your config, be sure to restart the Danbooru server.

Updating Danbooru

Periodically you will need to update your Danbooru installation. Do the following to update your install:

git pull
bin/bundle install
bin/rails db:migrate

This will pull the latest code, and update Ruby gems and run database migrations if necessary. See below for more instructions on these commands.

You may also need to update external dependencies like Ruby, libvips, Exiftool, etc, occasionally. This will have to be done by hand, by repeating the installation instructions above.

Updating the code

Run git pull to fetch the latest code.

If you've edited the code, then running git pull may create merge conflicts that you will have to resolve by hand. It's recommended to keep your changes on a separate branch, that way you can rebase your changes on top of master every time you pull an update.

You should read the git commit logs to see if there are any changes that impact you, or if there are any manual steps you need to take after the upgrade.

Updating Ruby gems

Updating your Ruby gems is necessary whenever Gemfile or Gemfile.lock is updated. Run bin/bundle install to update your gems.

Running database migrations

Running database migrations is necessary whenever the database schema changes. This happens when a new migration is added in db/migrate. Run bin/rails db:migrate to migrate your database.

You can run bin/rails db:rollback to undo the migration if there's a problem. You might also want to take a backup before running migrations, just in case.

In some cases a migration might also require running a fix script in script/fixes to populate or fix data after running the migration. Read the git commit logs to know when this is necessary.

Updating Ruby

Do this when updating to a new Ruby version:

# Update asdf to be able to install the latest Ruby version
asdf update
asdf plugin update ruby

# Install latest Ruby version
asdf install ruby 3.2.3
asdf shell ruby 3.2.3

# Reinstall Ruby gems
bin/bundle install

Troubleshooting

If you've installed everything and you can't get it to work, try the following commands to make sure Rails can run:

bin/rails --version
bin/rails about
bin/rails console

If you're having database problems, try the following commands to make sure you can connect to the database:

bin/rails dbconsole -p
psql postgresql://danbooru@localhost/danbooru2 -c "\d+" # This should output a big list of tables.

If you still need help, ask in the #technical channel of the Danbooru Discord or in the discussions area on Github.