Installing Odoo in a Python virtual environment
It is a common case among Odoo developers to maintain code for several Odoo versions. Some organizational effort is needed to keep these projects alongside each other and working on the same development machine. And, there is some context switching needed when changing from one version to another. For example, the Odoo start executable is now odoo-bin, but in older versions it was odoo.py, and you need to remember that. With the move to Python 3, this can be even more confusing; you need to choose whether to use python / pip or python3 / pip3, depending on the Odoo server version you are working on at that moment.
Python includes virtualenv, a tool to manage independent Python environments on the same machine. Each environment has its own Python executable and installed libraries. You just need to activate the environment you want to work with; from that point on, the python and pip commands will run the executable from that environment, and the Python libraries installed in it.
To ensure that virtualenv is available in our Debian/Ubuntu, run the following command:
$ sudo apt install virtualenv
Suppose we are working in the ~/odoo-dev directory, and we have Odoo 12 source code cloned at ~/odoo-dev/odoo. We will now create an virtual environment for our Odoo 12 project:
$ virtualenv -p python3 ~/odoo-dev/odoo12env
$ source ~/odoo-dev/odoo12env/bin/activate
The first command creates the odoo12env virtual environment, which uses Python 3. The corresponding directory is created, to store the corresponding files. The environment directory has a bin/ subdirectory including, among other things, the scripts to activate and deactivate this environment.
The second command uses the source Linux command to execute the environment's activate script. This will change the current context, so that the commands available from the environment's bin/ subdirectory are used first. It also changes Command Prompt, so that it shows the current activated environment. At this point, your terminal session should look like this:
(odoo12denv) $ python --version
Python 3.5.2
(odoo12denv) $ which python
/home/daniel/odoo-dev/odoo12env/bin/python
Once we have a virtual environment activated, we can install Odoo in it. This can be done using pip with the Odoo source code:
(odoo12denv) $ pip install -e ~/odoo-dev/odoo
The preceding command will install the Odoo source code at ~/odoo-dev/odoo, and the corresponding dependencies, in this virtual environment.
The -e option is important; it makes it an editable installation. Instead of copying a snapshot of the Odoo code into the virtual environment, it just keeps a reference to the original location of the Odoo code. Since the original source code is being used, we see in this environment the effect of changes in the source code.
The Odoo Python dependencies are automatically installed, so there is no need to manually install the packages in the requirements.txt file.
And, we can use pip to install any additional Python libraries we might need:
(odoo12denv) $ pip install phonenumbers num2words psycopg2-binary watchdog xlwt
Notice that we don't need to remember whether we are working with Python 2 or Python 3. In a virtual environment, the pip command will always point to the correct version.
Next, we can run Odoo. The pip installation creates a bin/odoo command, which can be executed from anywhere, without the need to reference the directory where the source code is.
The following command will start and stop your installed version of Odoo, printing a few log messages that can be useful to confirm things such as the Odoo version and the add-ons paths being used:
(odoo12denv) $ odoo --stop-after-init
A good practice is to keep the configuration files for your projects inside the virtual directory environment. This will initialize a 12-library database for our project and create the corresponding 12-library.conf file:
(odoo12denv) $ odoo -c ~/odoo-dev/odoo12-env/12-library.conf \
-d 12-library --addons-path=~/odoo-dev/library --save --stop
From now on, to start an Odoo server for our Library project, we use the following:
(odoo12denv) $ odoo -c ~/odoo-dev/odoo12-env/12-library.conf
Finally, when we are done, we can deactivate the environment, running the corresponding command:
(odoo12denv) $ deactivate
Now, suppose we also want to work on an Odoo 10 project on the same machine, which uses Python 2.7. Let's create that environment and install Odoo 10 in it:
$ cd ~/odoo-dev
$ git clone https://github.com/odoo/odoo.git -b 10.0 --depth=1 odoo10
$ virtualenv odoo10env
$ source odoo10env/bin/activate
(odoo10env) $ pip install -e ./odoo10
(odoo10env) $ odoo --version
(odoo10env) $ deactivate # To finish working with this env.
To make it easier to switch between Odoo versions, we make another clone of the Odoo source, for branch 10.0, in the ~/odoo-dev/odoo10 directory. We then create the virtual environment, activate it, and use pip to make an editable installation of Odoo 10. virtualenv was used with no specific Python version (-p option), and so it defaults to Python 2, the version needed for Odoo 10.
If Python 2 is not available in your system, which is the case for out-of-the-box Ubuntu 18.04, you should install it in order to be able to run older Odoo versions:
$ sudo apt install python-dev