Skip to content

Development Environment Setup: Ubuntu

Konrad edited this page Dec 27, 2023 · 57 revisions

Intro

This guide will help you set up a development environment for OFN on Ubuntu 22.04 LTS or Ubuntu 20.04 LTS.

Step 1. Install Ruby, Node.js, Yarn, and rbenv

This section is based on instructions here (22.04) and here (20.04). Those instructions include a section on configuring git. That is optional, but may be useful to you. Rails does not need to be installed now. It is installed by a setup script in a later step.

The version of Ruby used in OFN is shown here. At the time of writing it is 3.1.4. If it has changed, the commands below starting with rbenv will need to be adjusted (and this guide updated).

The version of Node is shown here. It's recommended to install nodenv to ensure you have the matching version. Here is a great script to install it: Installing nodenv on Ubuntu. Alternatively you can install as per below. We are using imagemagick and libvips for image processing.

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 update
sudo apt 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 imagemagick libvips
   
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
   
rbenv install 3.1.4
rbenv global 3.1.4
gem install bundler
rbenv rehash

Step 2. Install PostgreSQL and Redis

This will install PostgreSQL, the database that OFN uses, and Redis, which is used for caching. It also installs a supporting library for PostgreSQL.

sudo apt install postgresql redis-server libpq-dev

Step 3. Clone the OFN codebase and set it up

As per the Getting Started document, fork the OFN repo then enter the following to clone it and set up the database superuser.

cd
git clone https://github.com/YOUR_GITHUB_USERNAME_HERE/openfoodnetwork
cd openfoodnetwork
git remote add upstream https://github.com/openfoodfoundation/openfoodnetwork
git fetch upstream master
sudo -u postgres psql -c "CREATE USER ofn WITH SUPERUSER CREATEDB PASSWORD 'f00d'"

Step 4. Run the setup script and final steps

The next step is to run the setup script. This currently gives some error messages about missing dependencies, however these do not prevent you starting the dev server. If they bother you, you can avoid them with:

yarn add @babel/core
yarn add @hotwired/stimulus
yarn add @babel/plugin-transform-runtime

Then run the setup script

script/setup

Check the console output for error messages. Here are hints how to fix them. Then run the script again.

  • Unable to connect to server: could not connect to server: Connection refused : Is the server running on host "localhost" and accepting TCP/IP connections on port 5432?
    • Look for the port number in the message and make sure that the same port is set in the postgresql.conf file. You should find the file in /etc/postgresql/<version>/main.
    • Restart PostgreSQL by running sudo service postgresql restart
  • Error: EACCES: permission denied, access '<SOME_PATH>'
    • Maybe the best way is to run the script as sudo, I haven't tried that. For me the steps below worked - probably not recommended.
    • You can check the access rights with ls -la '<SOME_PATH>'.
    • Change ownership to your user with sudo chown -R <$USER> <SOME_PATH>.

The script above does not install foreman, which is used to start the dev server. Install it with

gem install foreman

Node.js needs an option to be set to tell it to use an older SSL provider, otherwise foreman start will throw a load of errors and then exit.

echo 'export NODE_OPTIONS=--openssl-legacy-provider' >> ~/.bashrc
exec $SHELL

Exit your shell and reopen it for the setting to take effect. You can then run the dev server with

foreman start

Step 5. Install Chromium and driver

Chrome or Chromium is used for UI testing. This is automated using a driver. Install them with

sudo apt install -y chromium-browser
sudo apt install -y chromium-chromedriver

Step 6. Prepare the Database for Testing

Tests, both unit and integration, are based on RSpec. To run the test suite, first prepare the test database:

bundle exec rake db:test:prepare

For more details on testing, please check that section of the GETTING_STARTED guide.

Clone this wiki locally