-
Notifications
You must be signed in to change notification settings - Fork 78
Setting up dev environment
This is a guide for Windows and Mac users on how to set up coursemology on your local machine so that you can contribute as well.
-
Setting up dev environment
- Table of Contents
-
Windows
- 1. Install Ubuntu
- 2. Cloning the Repository
- 3. Setting up Ruby on Rails
- 4. Install Node.js
- 5. Install Yarn
- 6. For pg gem to work
- 7. Install Ruby dependencies
- 8. Install PostgreSQL
- 9. Add role to PSQL
- 10. Create and seed the database
- 11. Access the Coursemology App
- 12. Sign in to Coursemology
- 13. Installing and running Docker
- 14. Installing Imagemagick
- Mac
- Regressions
- Local Testing
- Useful Commands
This is a complete guide for windows setup that can be used independently from the original README in the coursemology repository.
Go to the Microsoft app store and search Ubuntu. Install the application Ubuntu 20.04. There are also other acceptable ways such as using a VM or dual booting, but it is recommended to use WSL2. WSL2 has support for headless Chrome which is needed for some tests, which WSL1 cannot support.
Clone the coursemology2 repository.
cd
git clone https://github.com/Coursemology/coursemology2.git
cd coursemology2
This git repo uses submodules as well, so use this command to update them:
git submodule update --init --recursive
This section is mostly taken from a guide made by the people at GoRails. To make sure we have everything necessary for Webpacker support in Rails, we're first going to start by adding the Node.js and Yarn repositories.
sudo apt install curl
curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update
sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs yarn
Next we install Ruby using a version managed called rbenv
cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.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
exec $SHELL
Then we install Ruby version 3.3.5
rbenv install 3.3.5 --verbose
This commands will take 10-15 mins to complete. The --verbose
flag will show what is going on so we can be sure it has not gotten stuck.
Once installed, we use the next command to tell rbenv
which version to use. We can change the global
flag to local
instead if you have other Ruby projects on your machine that are on different versions.
rbenv global 3.3.5
Similar to Ruby, here we are installing nodenv
, which is a version manager for Node.js
git clone https://github.com/nodenv/nodenv.git ~/.nodenv
cd ~/.nodenv && src/configure && make -C src
echo 'export PATH="$HOME/.nodenv/bin:$PATH"' >> ~/.bashrc
~/.nodenv/bin/nodenv init
Installing a node-build
mkdir -p "$(nodenv root)"/plugins
git clone https://github.com/nodenv/node-build.git "$(nodenv root)"/plugins/node-build
curl -fsSL https://github.com/nodenv/nodenv-installer/raw/master/bin/nodenv-doctor | bash
We can use this command to verify that everything has been properly set up.
curl -fsSL https://github.com/nodenv/nodenv-installer/raw/master/bin/nodenv-doctor | bash
Install Node.js, similar to Ruby
nodenv install 18.12.1 --verbose
nodenv local 18.12.1
Appending the follow to your ~/.bashrc
echo 'eval "$(nodenv init -)"' >> ~/.bashrc
source ~/.bashrc
npm install --global yarn
Installing more dependencies for client application
cd ~/coursemology2/client && yarn; cd -
sudo apt install libpq-dev
cd ~/coursemology2
gem install bundler:2.5.9
bundle config set --local without 'ci:production'
bundle install
If you already have a version of PostgreSQL that is at least version 12, you can skip this section. This is because having two versions of PostgreSQL installed on your system can be problematic when it comes to which version is running on the default port 5432.
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install postgresql
Use this command to check the status of your PSQL clusters.
pg_lsclusters
Use this command to start the cluster with the corresponding version nummber and name.
sudo pg_ctlcluster 16 main start
- Log into psql as postgres.
sudo -u postgres psql
- Create a PostgreSQL user. For example, if your user account is benleong:
CREATE ROLE benleong WITH CREATEDB LOGIN;
- Give the PostgreSQL user superuser status.
ALTER USER benleong WITH SUPERUSER;
- Create a database with the same name as your user account. By default, psql tries to connect to a database with the same name as your user.
CREATE DATABASE benleong WITH OWNER benleong;
- Exit the database by entering exit , then try to log into psql using just psql . If you set everything up correctly, you should now be logged in as a PostgreSQL user with the same name as your user account.
cd ~/coursemology2
bundle exec rake db:setup
bundle exec rake coursemology:seed
RAILS_ENV=test bundle exec rake db:setup
To access the App after having done setting up everything above, you need to run the following command
host lvh.me
on your terminal to bind 127.0.0.1
to lvh.me
. Afterwards, you need to run 2 separate processes using 2 different terminals, each to run the Frontend and Backend, respectively. On the terminal that's supposed for the Frontend one, run the following command
yarn build:development
and on the terminal that's supposed for the Backend one, run the following command
bundle exec rails s -p 5000
Then, you can access the App by visiting http://lvh.me:8080
Email: [email protected]
Password: Coursemology!
- Update the apt package index and install packages to allow apt to use a repository over HTTPS
sudo apt-get update
sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
- Add Docker’s official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
- Use the following command to set up the stable repository
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- Update the apt package index, and install the latest version of Docker Engine, containerd, and Docker Compose
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
- Verify that Docker Engine is installed correctly by running the hello-world image.
sudo docker run hello-world
- To start docker run
sudo service docker start
Note: If you are using WSL1, you may need to follow this when setting up Docker
Set up ImageMagick, which is used for image processing.
sudo apt-get install imagemagick
This is a guide for Mac users on how to set up coursemology on your local machine. Last updated: 5/8/24
git clone https://github.com/Coursemology/coursemology2.git
cd coursemology2
Update the submodules:
git submodule update --init --recursive
Install the latest Xcode from the app store. This process can take up to 15 to 20 minutes. Then, run the following command in your terminal:
xcode-select --install
You may need to click "Install" when prompted.
Homebrew is a package manager for macOS. To install it, run the following command in your terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
The easiest way to set up Ruby is using rbenv
(or rvm
will work as well, but this guide will focus on the former).
brew install rbenv
Then run this command:
rbenv init
You should see this message:
writing ~/.profile: now configured for rbenv.
Now, we can finally install Ruby! Do the following:
rbenv install 3.3.5 --verbose
This command will take 10-15 minutes to complete. The --verbose
flag will show you what’s going on so you can be sure it hasn’t gotten stuck.
Once installed, you can tell rbenv
which version to use. You can either set this globally via:
rbenv global 3.3.5
Or locally (run this command at the project root):
rbenv local 3.3.5
rbenv rehash
Then update shims to recognize the new commands:
rbenv rehash
Check that everything has worked by typing ruby -v
. The version should look something like this:
ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [arm64-darwin24]
Install the bundler gem and use it to install the other gems (specified in the Gemfile
):
gem install bundler:2.5.9
bundle config set --local without 'ci:production'
bundle install
Similar to Ruby, the easiest way to set up and manage multiple versions of Node is using nodenv
(or nvm
will work as well, but this guide will focus on the former).
We can install it using Homebrew:
brew install nodenv
We will then configure the Node version at the project root. You can refer to the latest version at this website here. As of the time of writing, the latest version is v18.12.1
nodenv install 18.12.1 --verbose
nodenv local 18.12.1
Yarn is a package manager for Node.js. You can install it using node package manager (npm):
npm install --global yarn
Then, install the dependencies for the client application:
cd client && yarn; cd -
Finally, we just need to set up ImageMagick or GraphicsMagick. It's generally easier to set up ImageMagick, so we will go ahead with that:
brew install imagemagick
As ImageMagick depends on Ghostscript fonts, we will also install that:
brew install ghostscript
Docker is a platform for developing, shipping, and running applications. You can install it from the official website.
This part is still quite janky, so please bear with us.
To get started with PostgreSQL, the easiest way is to download Postgres.app, which will help you set up everything you need.
Once installed, start the database.
Then run this command to create the coursemology
and coursemology_test
databases:
bundle exec rake db:setup
Don't worry about the other errors you see for now regarding keycloak.
Keycloack is an open-source Identity and Access Management (IAM) solution.
Build a Docker image named coursemology_auth using the Dockerfile located in the authentication
directory
cd authentication
docker build -t coursemology_auth .
Copy the env file:
cp env .env
Create an empty coursemology_keycloak database in postgresql:
psql -c "CREATE DATABASE coursemology_keycloak;" -d postgres
Spin up the Keycloak server:
docker compose up
The authentication pages can be accessed via http://localhost:8443/admin
- Sign-in to authentication pages by inputting the following credentials:
Username:
admin
(whatever defined in KEYCLOAK_ADMIN inside ./.env)Password:
password
(whatever defined in KEYCLOAK_ADMIN_PASSWORD inside ./.env)
-
Navigate to coursemology realm by choosing Coursemology in the top-left dropdown box, or simply access Coursemology realm
-
Navigate to Client, then click on the Client ID in which name is
coursemology-backend
-
Over there, navigate to Credentials and you will see the Client Secret. If whatever is defined there does not match with the Client Secret defined in your environment setup, simply copy-paste the client secret inside the page (you can possibly regenerate it if you want), then copy-paste it to
KEYCLOAK_BE_CLIENT_SECRET
inside../.env
Make sure the env files in the root
directory and the client
directory are set up correctly (copy the env files and add a dot in front of it).
cp env .env
client
env requires and extra GOOGLE_RECAPTCHA_SITE_KEY
as well. Look for a system admin to help with this.
Run the setup again, and we should not get any errors this time:
bundle exec rake db:setup
bundle exec rake coursemology:seed
RAILS_ENV=test bundle exec rake db:setup
To access the App after having done setting up everything above, you need to run the following command:
host lvh.me
on your terminal to bind 127.0.0.1
to lvh.me
. Afterwards, you need to run 2 separate processes using 2 different terminals, each to run the Frontend and Backend, respectively. On the terminal that's supposed for the Frontend one, run the following command:
yarn build:development
and on the terminal that's supposed for the Backend one, run the following command
bundle exec rails s -p 3000
Then, you can access the App by visiting http://lvh.me:8080
Email: [email protected]
Password: Coursemology!
Along the way, you might encounter certain issues. Here are some that we are aware of:
If you get the following error with nokogiri/xcode-select
when running bundle install --without ci:production
:
failed to build gem native extension...
What you can do is to run:
sudo gem uninstall nokogiri
xcode-select –install
sudo gem install nokogiri
Check out this link for more information.
If you get the following error with mimemagic
when running bundle install --without ci:production
:
could not find MIME type database in the following locations: ...
What you can do is to run:
brew install shared-mime-info
sudo gem install mimemagic
Check out this link for more information.
Sometimes while you tried to run either bundle exec rake db:create
or bundle exec rake db:setup
, the error occurs that basically mentions the database "postgres" does not exists. Referring to this link, basically there are 2 approaches that you might consider attempting. The first one is to create the postgres db manually (which is more recommended) by using the following command
createdb postgres
And the second one is to create the database coursemology
and coursemology_test
through psql
, as follows
psql
Then, the interactive command will appear, and then you need to type the following
<username>=# CREATE DATABASE coursemology; CREATE DATABASE coursemology_test;
Here are some known issues during development, all of which causes are still unknown
- The app needs to be restarted after updating any of the controllers under
app/controllers
- The app needs to be restarted after updating any of the view files under
app/views
- The CI test is a little bit flaky with the end to end tests during a PR, specifically
ci/circleci: test
. If any test related to "jobs" does not pass try running it again.
Compile assests (done in coursemology/client
directory):
yarn build:test
Testing rails (Parallel -- much faster):
RAILS_ENV=test bundle exec rake parallel:spec
Running rails test on specific file:
RAILS_ENV=test bundle exec rspec `path to file`
Testing frontend (done in coursemology/client
directory):
yarn test
Linting (done in coursemology/client
directory):
yarn lint
yarn lint-fix
Keeping Ruby gems updated:
bundle install
For debugging (insert anywhere in Ruby code):
byebug