Installing the Discourse development environment on Ubuntu

<div></div>

:warning: This guide is only for configuring the Discourse development environment. If you need to install Discourse in a production environment, please refer to the content on the page: Install Discourse in production with the official supported instructions.

:information_source: For the original English content on setting up the development environment, please refer to the content on the page: Set up a local Discourse Development Environment?.


This article focuses on setting up the development environment on Ubuntu. Since Discourse is developed using Ruby, setting up the Ruby development environment is most straightforward on Ubuntu. Therefore, Discourse’s production environment is also officially recommended to run on Ubuntu. Although our community runs on a REHL environment without issues due to the use of Docker containers, if you intend to develop Discourse, it is recommended to use an Ubuntu environment.

If you are using a Windows system, you will need to install the WSL environment. WSL is an Ubuntu-based environment provided by Windows, primarily used to solve the problem of developing Linux applications on Windows.

This article assumes you have not yet installed Ruby/Rails/Postgres/Redis in your Ubuntu environment. Let’s begin configuring the development environment!

Although this development guide assumes you are using an Ubuntu development environment, any Debian-based Linux distribution can be used.

This development guide has been verified on Ubuntu 18 and requires no additional steps to complete the development environment setup.

:exclamation: Due to differences in using the development environment on Ubuntu/Debian, you may need to refer to the following information when developing on Ubuntu/Debian:

Setting up the Discourse testing environment on Ubuntu 20.04 and later versions

On default Ubuntu 20.04 and later versions, OpenSSL 3.0.2 will be installed by default. You may need to compile from an earlier OpenSSL version and replace the current version (the official recommendation is OpenSSL-1.1.1n). You also need to check your Ruby version. You can view the Ruby version using the command ruby --version. Discourse currently still uses Ruby 2. If you are using Ruby 3.x or a later version, you will need to downgrade your Ruby version. The official recommendation is to use a Ruby version management tool for downgrading. For Discourse’s current operating environment, the official recommendation is Ruby 2.7.6 as the most suitable version for Discourse. Please refer to: Notes about Installing Discourse on Ubuntu 20.04 - #2 by SaraDev for more information.

Setting up the Discourse testing environment on Ubuntu versions below 20.04

The following section was added by @pfaffman on 22.05.16. This might cause an error, but you might not encounter it.

NOTE: Please refer to: Failed to build ruby 2.7.5 on ubuntu 22.04 · rbenv/ruby-build · Discussion #1940 · GitHub for relevant information.

End of @pfaffman additions.

Based on our testing experience, Ubuntu 22 may have compilation and package not found issues. We have only completed the local development environment setup on 20.04.

Install Discourse Dependencies

As a general user, you can run the following command in the console: this script. The command above will help you quickly set up Rails in your local development environment.

The command to run is:

bash <(wget -qO- https://raw.githubusercontent.com/discourse/install-rails/master/linux)

The command above will install the following packages on your local system:

If you already have some software installed on your local operating system, or if you do not wish to install all the software, please refer to the content in script and select the software you do not wish to install at this time. The installation script above will install all the software required for Discourse to run, which will provide support for Discourse’s operation.

Once you have finished installing all Discourse dependencies, we can proceed with installing Discourse.


The image above shows the installation interface on Ubuntu. The entire software installation process is quite time-consuming, potentially taking more than 5 minutes, depending on your system.

Clone Discourse

Clone Discourse into the ~/discourse folder.

git clone https://github.com/discourse/discourse.git ~/discourse

~ defines the current Home folder, meaning the Discourse program will be copied into your home folder.

Since we are using the WSL subsystem, we are actually cloning the files to my D drive.

Setup Database

Create a user with the same name as your Ubuntu system username:

sudo -u postgres createuser -s "$USER"

If you receive an error when running the command above:

createuser: error: could not connect to database template1: could not connect to server: No such file or directory

Please refer to the content on the page: iSharkFly - 飞鲨.

Start Discourse

Switch to your Discourse clone directory:

cd ~/discourse

Install the required gems.

source ~/.bashrc
bundle install

Install the required JS dependencies as well.

yarn install

At this point, you have installed all the necessary gems and dependencies. Please try running the following commands:

bundle exec rake db:create
bundle exec rake db:migrate
RAILS_ENV=test bundle exec rake db:create db:migrate

If you encounter errors during execution, please refer to the content on the page: iSharkFly - 飞鲨.

Try running the following command:

bundle exec rake autospec

Your project should pass all tests.

This test is very time-consuming and can be skipped. Otherwise, it might take several hours.

Run the following command to start the server:

bundle exec rails server

After completing the installation steps above, you should be able to access your locally installed Discourse via http://localhost:3000.

Starting from Discourse 2.5+, EmberCLI is a mandatory option for the local development environment:

If you access port 3000 directly through the interface, you will see the following prompt:


In your console terminal, navigate to the (cd ~/discourse) folder and run:

bin/ember-cli

You should be able to view your local Discourse installation by visiting http://localhost:4200.

:warning: It is important to note that both of these servers need to be started simultaneously. EmberCLI accesses port 3000 through a reverse proxy.

Create a New Admin Account

Before logging into the development environment, you need to create an administrator account. Run the following command to create one:

RAILS_ENV=development bundle exec rake admin:create

Please follow the prompts in the command line to create this administrator account.

You will need to enter an email address and password.

Configure Email

Run MailHog:

mailhog

After completing all the steps above, your Discourse local development environment will be configured. You can now log in to Discourse as an administrator.

If you wish to develop plugins for Discourse, please refer to the guide on the page: Developing Discourse Plugins - Part 1 - Create a basic plugin.


2 Likes

Hello, what user does the Ubuntu system username refer to?

You don’t need to find it yourself. This command means setting the environment variable $USER to the username. You can execute it directly. That sentence explains what this command does.

3 Likes

Correct, thank you.

That’s exactly what it means.

1 Like

Okay, thank you for your patient reply

It actually translated Chinese, I came specifically to praise it.