在macOS上安装Discourse以进行开发

:warning: This guide covers installation instructions for a macOS development environment, for production guides see: Install Discourse in production with the official supported instructions

So you want to set up Discourse on macOS to hack on and develop with?

We’ll assume that you don’t have Ruby/Rails/Postgres/Redis installed on your Mac. Let’s begin :rocket: !

Install Discourse Dependencies

You will need the following packages on your system:

** optional

restart your terminal

Now that we have installed Discourse dependencies, let’s move on to install Discourse itself.

Restart your Terminal

Exit your shell and restarting it ensures that the paths to the installed packages are correctly picked up by the Terminal.

Clone Discourse

Clone the Discourse repository in ~/discourse folder:

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

~ indicates home folder, so Discourse source code will be available in your home folder.

Bootstrap Discourse

Switch to your Discourse folder:

cd ~/discourse

Install the needed gems

bundle install

Install the JS dependencies

pnpm install

Next, run these commands to set up your local Discourse instance:

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

Start rails + Ember servers, you have two options here.

Option 1: using two separate Terminal tabs/windows, run Rails and Ember CLI separately via

bundle exec rails server

and

bin/ember-cli

Option 2: using only one Terminal tab/window:

bin/ember-cli -u # will run the Unicorn server in the background

:tada: You should now be able to navigate to http://localhost:4200 to see your local Discourse installation. (Note that the first load can take up to a minute as the server is warmed up.)

You can also try running the specs:

bundle exec rake autospec

All (or almost all) the tests should pass.

Create New Admin

To create a new admin, run the following command:

RAILS_ENV=development bundle exec rake admin:create

Follow the prompts to create an admin account.

Configure Mail

Run MailHog:

mailhog

Congratulations! You are now the admin of your own Discourse installation!

Happy hacking! And to get started with that, see Beginner’s Guide to Creating Discourse Plugins.


This document is version controlled - suggest changes on github.

77 个赞

If anyone is getting failing specs with most of them being .count errors, then try the following:

RAILS_ENV=test bundle exec rake db:reset

I had a problem where development data somehow got into my test database and was causing lots of failures.

7 个赞

Is it possible to reset a discourse completely?

2 个赞

Yes, do:

rake db:drop db:create db:migrate

Note that this will wipe everything and you will have to create your Admin user account again.

11 个赞

Can someone assist me in how I can update my discourse?

/launcher rebuild app

returns an error.

1 个赞

./launcher rebuild app is for discourse_docker based production setup. To update local/development Discourse instance you need:

cd ~/discourse
git pull origin master
8 个赞

I get this error @techAPJ,

error: Your local changes to the following files would be overwritten by merge:
	.gitignore
Please commit your changes or stash them before you merge.
Aborting

The error message is pretty clear:

Please commit your changes or stash them before you merge.

If you want to ignore your local changes, do:

git fetch origin
git reset --hard origin/master
7 个赞

Using the guide from the Ruby on Rails forum, I’m currently stuck on step 7.

This is probably a very “newbie” question, but how do I “point” to the experimental branch that was linked in that topic? I tried a few things and was ultimately giving the Unknown command mini_racer, message every single time.

1 个赞

They’re fine asking here.

There are some specific m1 instructions we wrote that we reference on the rails forum in this guide.

I’d have to look on my m1 air later, and maybe my fellow coworks who recently setup new macs might know better if this is even still needed, but there is an example of how to point to a branch in step 7:

gem 'mini_racer', github: 'rubyjs/mini_racer', branch: 'refs/pull/186/head'

So you would edit this line in your local version of discourse/Gemfile:

and then run bundle install.

4 个赞

It’s linked right at the top of the original post.

The topic from the Ruby on Rails forum has been dead for many months now. The OP hasn’t been active since Feburary and hasn’t replied to any comments there asking for help.

There are more people to help me here. Just saying. :wink:

I tried copying that into Terminal and received the same Unknown command mini_racer, error from before.

Admittedly, the guide from the Ruby on Rails forum isn’t as clear as I’d hope in that regard.

Apologies in advance… :pray:

2 个赞

It’s not a command to run. You will need to EDIT the actual text file called “Gemfile” located inside the discourse directory.

1 个赞

Doh! :facepalm:

Thanks! Let me try that right now.

In my opinion, somebody should edit the guide from the Ruby on Rails forum and provide further clarification regarding step 7. Just my two cents though.

I will be right back. Hold on a second…

1 个赞

Sorry I have to run, but I think that mini_racer fix for m1 macs has been merged in. I just checked my Gemfile on my m1 mac and mine isn’t edited. So I think you can skip step 7.

3 个赞

I ran step 8 and received the Could not locate Gemfile error.

I (think) I was able to figure out step 7, but step 8 is still giving me trouble and is spitting out the error mentioned above.

运行 bundle install 时,您需要位于 discourse 目录下。

@merefield:您能多描述一下您的远程设置吗?

虽然开发主题允许我在实时网站上添加更改,但对于开发插件,我似乎别无选择,只能在我本地机器上进行,将整个 discourse 代码库放在那里(并在其之上放置我的插件)。

结果是,对于编码插件时的任何(非 CSS)更改,当我重新加载它时,1) 会启动我电脑的风扇,2) 需要整整 30 秒才能重新加载。如果我需要重启服务器,则需要几分钟。

这些延迟确实会累加——结果是我可能需要一个小时才能编写完一些东西,而按照我正常的编码流程(有热重载或每次更改最多 2-3 秒)只需要15 分钟

所以,我很想听听任何加快速度的建议。

1 个赞

这或许更适合放在 Ubuntu 主题 上,因为我只是在云服务器上运行该安装,前面有 nginx 和完整的 DNS(无 docker),所以我实际上是在处理域名。一切都从终端运行。我甚至已经让 Ember CLI 在该设置中运行良好。

它也不是很快,但足够快。它的好处是可以运行和测试完整的 https 回调服务。

据我所知,目前最快的插件开发环境是本地的 Docker Dev on WSL2,它运行速度极快。它也非常容易维护。不幸的是,据我所知 discourse_theme 在该环境中还不能正常工作,所以我又回到了我的云服务器上进行这项工作。

苹果在这方面落后真是有点奇怪?微软的工程师们已经证明了他们非常精明。

3 个赞

如果有人在安装 ruby 2.7.3 时遇到错误,显然 Xcode 12 之后的一些更改破坏了 rbenv 的安装过程:

尽管我已安装最新版本的 psych 和 libyaml,但仍收到此错误:

似乎您的 ruby 安装缺少 psych(用于 YAML 输出)。
要消除此警告,请安装 libyaml 并重新安装您的 ruby。

我尝试了指南中建议的解决方法,但仍然收到相同的错误,最后不得不使用 rvm:

rvm install 2.7.3
1 个赞

关于如何更快地进行插件编程,我最近改变了我的方法。而且效果很好。基本上,将任何前端的东西移到一个主题组件中,只在插件本身中编写后端的东西。我知道其他人以前也发现了这一点。但现在我这样做了,编程速度更快,体验也更好。详情请参见此处

3 个赞