Chapter 2: Development environment setup¶
Depending on the intended use case, there are multiple ways to install Odoo. This tutorial will stick to the source install (running Odoo from the source code), which is best suited for Odoo developers.
Throughout this document, we assume that you are installing your development environment on a laptop provided by Odoo with Linux Mint installed and up-to-date. If that is not the case, switch to the Windows or Mac OS tab if any section of the installation guide, depending on which OS you are on. The steps remain essentially the same.
Set up Git¶
Install and configure Git¶
The very first step of the installation process is to install the Git version control system because the Odoo source code is managed on GitHub.
$ sudo apt install git
Tip
Check if Git is installed by trying to print Git’s version with the following command:
$ git --version
Once installed, register your name and email:
$ git config --global user.name "Your full name (trigram)"
$ git config --global user.email "xyz@odoo.com"
Configure GitHub¶
You need a GitHub account to fetch the sources and contribute to Odoo’s development. If you don’t have one yet, create it. For the username, we recommend using your trigram “xyz” (or quadrigam) followed by ‘-odoo’: ‘xyz-odoo’.
The easiest way to authenticate with GitHub is to use an SSH connection. Using SSH authentication allows you to connect to GitHub without supplying your username and password every time you type a command.
Note
The following step-by-step procedure is based based on the official GitHub documentation.
Generate a new SSH key, add it to the ssh-agent, and copy the SSH key to your clipboard.
$ ssh-keygen -t ed25519 -C "xyz@odoo.com" $ ssh-add ~/.ssh/id_ed25519 $ sudo apt install xclip $ xclip -sel clip < ~/.ssh/id_ed25519.pub
Go to GitHub.com, then click on your profile picture in the upper-right corner of the page and then on Settings.
On the user settings sidebar, click on SSH and GPG keys.
Click on New SSH key or on Add SSH key.
In the Title field, add a descriptive label for the new key.
Paste your key into the Key field.
Click on Add SSH key.
Fetch the sources¶
It is time to fetch the source code of Odoo. First, let’s create a home for the Git repositories in
$HOME/src/
.
$ mkdir -p $HOME/src
$ cd $HOME/src
Then, clone the two repositories with SSH as explained in the Installing Odoo guide.
Tip
Cloning the repositories will take a while, enjoy a cup of coffee while you wait.
Configure the Git repositories¶
To contribute to an Odoo repository, you first need to fork it, then create a branch containing your changes on the fork, and finally submit a Pull Request to the repository.
Tip
If you are lucky enough to work at Odoo, the forks already exist. They are hosted on https://github.com/odoo-dev/odoo and https://github.com/odoo-dev/enterprise.
After your two forks are created, their remote address can be added in your local repositories. In
the commands below, replace odoo-dev/odoo
and odoo-dev/enterprise
with the name of your forks if
needed.
$ cd $HOME/src/odoo
$ git remote add odoo-dev git@github.com:odoo-dev/odoo.git # Add odoo-dev as a new remote.
$ git remote rename origin odoo # Change the name of origin (the odoo repository) to odoo.
$ git remote set-url --push odoo no_push # Remove the possibility to push directly to odoo (you can only push to odoo-dev).
$ cd $HOME/src/enterprise
$ git remote add enterprise-dev git@github.com:odoo-dev/enterprise.git
$ git remote rename origin enterprise
$ git remote set-url --push enterprise no_push
Install the dependencies¶
As seen in Chapter 1: Architecture Overview, Odoo’s server runs on Python and uses PostgreSQL as an RDBMS. In the context of a development machine, the easiest approach is to install everything locally. To do so, follow once again the Installing Odoo guide.
Tip
Some useful SQL commands:
$ createdb $DB_NAME # Create a database.
$ dropdb $DB_NAME # Drop a database.
$ psql $DB_NAME # Connect to a database.
\l #List all the available databases.
\dt #List all the tables of the $DB_NAME database.
\d $TABLE_NAME #Show the structure of the table $TABLE_NAME.
\q #Quit the psql environment (ctrl + d).
Run the server¶
Launch with odoo-bin
¶
Once all dependencies are set up, Odoo can be launched by running odoo-bin
, the command-line
interface of the server.
$ cd $HOME/src/odoo/
$ ./odoo-bin --addons-path="addons/,../enterprise/" -d rd-demo
There are multiple command-line arguments that you can use to run the server. In this training you will only need some of them.
- -d <database>¶
The database that is going to be used.
- --addons-path <directories>¶
A comma-separated list of directories in which modules are stored. These directories are scanned for modules.
- --limit-time-cpu <limit>¶
Prevent the worker from using more than <limit> CPU seconds for each request.
- --limit-time-real <limit>¶
Prevent the worker from taking longer than <limit> seconds to process a request.
Tip
The
--limit-time-cpu
and--limit-time-real
arguments can be used to prevent the worker from being killed when debugging the source code.- You may face an error similar to
AttributeError: module '<MODULE_NAME>' has no attribute '<$ATTRIBUTE'>
. In this case, you may need to re-install the module with $ pip install --upgrade --force-reinstall <MODULE_NAME>.If this error occurs with more than one module, you may need to re-install all the requirements with $ pip install --upgrade --force-reinstall -r requirements.txt.You can also clear the python cache to solve the issue:$ cd $HOME/.local/lib/python3.8/site-packages/ $ find -name '*.pyc' -type f -delete
Other commonly used arguments are:
Log in to Odoo¶
Open http://localhost:8069/ on your browser. We recommend using Chrome, Firefox, or any other browser with development tools.
To log in as the administrator user, use the following credentials:
email:
admin
password:
admin
Enable the developer mode¶
The developer or debug mode is useful for training as it gives access to additional (advanced) tools. In the next chapters, we will always assume that you have enabled the developer mode.
Enable the developer mode now. Choose the method that you prefer; they are all equivalent.
Note
The main page of the Settings screen is only accessible if at least one application is installed. You will be led into installing your own application in the next chapter.
Extra tools¶
Useful Git commands¶
Here are some useful Git commands for your day-to-day work.
- Switch branches:When you switch branches, both repositories (odoo and enterprise) must be synchronized, i.e. both need to be in the same branch.
$ cd $HOME/src/odoo $ git switch master $ cd $HOME/src/enterprise $ git switch master
Fetch and rebase:
$ cd $HOME/src/odoo $ git fetch --all --prune $ git rebase --autostash odoo/master $ cd $HOME/src/enterprise $ git fetch --all --prune $ git rebase --autostash enterprise/master
Code Editor¶
If you are working at Odoo, many of your colleagues are using VSCode, VSCodium (the open source equivalent), PyCharm, or Sublime Text. However, you are free to choose your preferred editor.
It is important to configure your linters correctly. Using a linter helps you by showing syntax and semantic warnings or errors. Odoo source code tries to respect Python’s and JavaScript’s standards, but some of them can be ignored.
For Python, we use PEP8 with these options ignored:
E501
: line too longE301
: expected 1 blank line, found 0E302
: expected 2 blank lines, found 1
For JavaScript, we use ESLint and you can find a configuration file example here.
Administrator tools for PostgreSQL¶
You can manage your PostgreSQL databases using the command line as demonstrated earlier or using a GUI application such as pgAdmin or DBeaver.
To connect the GUI application to your database we recommend you connect using the Unix socket.
Host name/address:
/var/run/postgresql
Port:
5432
Username:
$USER
Python Debugging¶
When facing a bug or trying to understand how the code works, simply printing things out can go a long way, but a proper debugger can save a lot of time.
You can use a classic Python library debugger (pdb, pudb or ipdb), or you can use your editor’s debugger.
In the following example we use ipdb, but the process is similar with other libraries.
Install the library:
pip install ipdb
Place a trigger (breakpoint):
import ipdb; ipdb.set_trace()
Example
def copy(self, default=None): import ipdb; ipdb.set_trace() self.ensure_one() chosen_name = default.get('name') if default else '' new_name = chosen_name or _('%s (copy)') % self.name default = dict(default or {}, name=new_name) return super(Partner, self).copy(default)
Here is a list of commands:
- h(elp) [command]¶
Print the list of available commands if not argument is supplied. With a command as an argument, print the help about that command.
- pp expression¶
The value of the
expression
is pretty-printed using thepprint
module.
- w(here)¶
Print a stack trace with the most recent frame at the bottom.
- d(own)¶
Move the current frame one level down in the stack trace (to a newer frame).
- u(p)¶
Move the current frame one level up in the stack trace (to an older frame).
- n(ext)¶
Continue the execution until the next line in the current function is reached or it returns.
- c(ontinue)¶
Continue the execution and only stop when a breakpoint is encountered.
- s(tep)¶
Execute the current line. Stop at the first possible occasion (either in a function that is called or on the next line in the current function).
- q(uit)¶
Quit the debugger. The program being executed is aborted.
Now that your server is running, it’s time to start writing your own application!