# openGauss   Compiling the Version

A [**build.sh**](http://build.sh) script is provided for compiling openGauss and generating the installation package. You can compile openGauss by using the script. You can also configure environment variables and run commands to compile openGauss.

* [Preparation Before Compiling](https://docs.opengauss.org/en/docs/latest/docs/CompilationGuide/compiling-the-version.html#preparation-before-compiling)
    
* [Software Compilation and Installation](https://docs.opengauss.org/en/docs/latest/docs/CompilationGuide/compiling-the-version.html#software-compilation-and-installation)
    
* [Compiling the Installation Package](https://docs.opengauss.org/en/docs/latest/docs/CompilationGuide/compiling-the-version.html#compiling-the-installation-package)
    
* \[Verification After Build\](#Verification After Build)
    
* \[openGauss-OM Build\](#openGauss-OM Build)
    

This section describes the prerequisites and procedure for openGauss compilation. The following figure shows the compilation process.

![](https://docs.opengauss.org/en/docs/latest/docs/CompilationGuide/figures/%E7%BB%98%E5%9B%BE1.png align="left")

## **Preparation Before Compiling**

### **Downloading Code**

**Prerequisites**

The git and git-lfs have been installed and configured on the local host.

**Procedure**

1. Run the following command to download the code and open-source and third-party software repository. *\[git ssh address\]* indicates the actual code download address. You can obtain them from the openGauss community.
    
    ```plaintext
    ""[user@linux sda]$ git clone [git ssh address] openGauss-server
    [user@linux sda]$ git clone [git ssh address] openGauss-third_party
    [user@linux sda]$ # mkdir binarylibs        For details about this comment, see the following note.
    ```
    
    > **NOTE:**
    > 
    > * **openGauss-server**: openGauss code repository.
    >     
    > * **openGauss-third\_party**: open-source third-party software repository on which openGauss depends.
    >     
    > * **binarylibs**: package for storing the built open-source third-party software. You can obtain the package by referring to \[Compiling Open-source Software\](#Compiling Open-source Software) . Since compiling and building open-source software take a long time, we have compiled and built **binarylibs** using **openGauss-third\_party** and compress and upload **binarylibs** to the Internet.  
    >     The community provides binary files compiled on three platforms. The download links are as follows:  
    >     **openEuler\_arm:** [https://opengauss.obs.cn-south-1.myhuaweicloud.com/3.1.0/binarylibs/openGauss-third\_party\_binarylibs\_openEuler\_arm.tar.gz](https://opengauss.obs.cn-south-1.myhuaweicloud.com/3.1.0/binarylibs/openGauss-third_party_binarylibs_openEuler_arm.tar.gz) **openEuler\_x86:** [https://opengauss.obs.cn-south-1.myhuaweicloud.com/3.1.0/binarylibs/openGauss-third\_party\_binarylibs\_openEuler\_x86\_64.tar.gz](https://opengauss.obs.cn-south-1.myhuaweicloud.com/3.1.0/binarylibs/openGauss-third_party_binarylibs_openEuler_x86_64.tar.gz) **Centos\_x86:** [https://opengauss.obs.cn-south-1.myhuaweicloud.com/3.1.0/binarylibs/openGauss-third\_party\_binarylibs\_Centos7.6\_x86\_64.tar.gz](https://opengauss.obs.cn-south-1.myhuaweicloud.com/3.1.0/binarylibs/openGauss-third_party_binarylibs_Centos7.6_x86_64.tar.gz)
    >     
    
    > After the download is complete, decompress and rename the package: **mv openGauss-third\_party\_binarylibs binarylibs**.
    
2. When the progress of each download reaches 100%, the download is successful.
    

### **Compiling Open-source Software**

**Compiling Open-source Software**

Before compiling the openGauss, compile and build the open-source and third-party software on which the openGauss depends. These open-source and third-party software is stored in the **openGauss-third\_party** code repository. After downloading the software, you can use **git lfs pull** to obtain large files from the code repository. Generally, you only need to build the software once. If the open-source software is updated, rebuild the software.

Since this step takes a long time, we have compiled and built **binarylibs** using **openGauss-third\_party**. You can download the package by referring to \[Downloading Code\](#Downloading Code).

**Table 1** openGauss open-source third-party software required before build

| **Software** | **Recommended Version** |
| --- | --- |
| python3 | 3.6 |
| python3-devel | 3 |
| setuptools | 36.6.1 |
| libaio-devel | 0.3.109-13 |
| flex | 2.5.31 or later |
| ncurses-devel | 5.9-13.20130511 |
| lsb\_release | 4.1 |
| pam-devel | 1.1.8-1.3.1 |
| libffi-devel | 3.1 |
| patch | 2.7.1-10 |
| golang | 1.13.3 or later |
| autoconf | 2.69 |
| automake | 1.13.4 |
| cmake | 3.18 |
| diffutils | 3.7 |
| openssl-devel | 1.1.1 |
| libtool | 2.4.2 or later |
| libtool-devel | 2.4.2 or later |

Prepare GCC 7.3 before building the third-party libraries. You are advised to use the released and built third-party library GCC and configure environment variables.

After installing the software listed in [Table 1](https://docs.opengauss.org/en/docs/latest/docs/CompilationGuide/compiling-the-version.html#en-us_topic_0283136302_table1212531681911), point the default Python version to **python3.x** and perform the following operations:

1. Run the following commands to go to the directory of the open-source and third-party software on which the kernel depends, compile and build the open-source and third-party software, and generate binary programs or library files. **/sda/openGauss-third\_party** is the directory for downloading open-source third-party software.
    
    ```plaintext
    ""[user@linux sda]$ cd /sda/openGauss-third_party/build
    [user@linux build]$ sh build_all.sh
    ```
    
2. After the preceding commands are executed, the open-source third-party software required for openGauss build is automatically generated. To generate any open-source third-party software independently, go to the corresponding directory and run the [**build.sh**](http://build.sh) script. For example:
    
    ```plaintext
    ""[user@linux sda]$ cd /sda/openGauss-third_party/dependency/openssl
    [user@linux openssl]$ sh build.sh
    ```
    
    The openssl is generated.
    
3. > **NOTE:** For error logs, you can view the corresponding log in the build directory and the log in the corresponding module. For example, you can view the OpenSSL compilation and installation logs in the **dependency** module.
    > 
    > * /sda/openGauss-third\_party/build/dependency\_build.log
    >     
    > * /sda/openGauss-third\_party/dependency/build/openssl\_build.log
    >     
    > * /sda/openGauss-third\_party/dependency/openssl/build\_openssl.log
    >     
    

**Compilation and Build Result**

After the preceding script is executed, the final compilation and build result is stored in the **output** directory under the **openGauss-third\_party** directory These files will be used during the compilation of **openGauss-server**.

### **Introduction to compile script**

**openGauss-server/build.sh** is an important script tool during compilation. It integrates software installation and compilation and product installation package compilation functions to quickly compile and package code.

The following table describes the parameters.

**Table 2** [build.sh](http://build.sh) parameters

| **Option** | **Default Value** | **Parameter** | **Description** |
| --- | --- | --- | --- |
| \-h | Do not use this option. | \- | Help menu |
| \-m | release | \[debug | release | memcheck\] | Selects the target version. |
| \-3rd | ${*Code directory*}/binarylibs | \[binarylibs path\] | Specifies the path of **binarylibs**. The path must be an absolute path. |
| \-pkg | Do not use this option. | \- | Compresses the code compilation result into an installation package. |
| \-nopt | Do not use this option. | \- | If this option is used, the CPU of the Kunpeng platform is not optimized. |

> !\[\] (public\_sys-resources/icon-note.gif) **NOTE:**
> 
> 1. **\-m \[debug | release | memcheck\]** indicates that three target versions can be selected:
>     
> 
> * **release**: indicates that the binary program of the release version is generated. During compilation of this version, the GCC high-level optimization option is configured to remove the kernel debugging code. This option is usually used in the production environment or performance test environment.
>     
> * **debug**: indicates that a binary program of the debug version is generated. During compilation of this version, the kernel code debugging function is added, which is usually used in the development self-test environment.
>     
> * **memcheck**: indicates that a binary program of the memcheck version is generated. During compilation of this version, the ASAN function is added based on the debug version to locate memory problems.
>     
> 
> 1. **\-3rd \[binarylibs path\]** is the path of **binarylibs**. By default, **binarylibs** exists in the current code folder. If **binarylibs** is moved to **openGauss-server** or a soft link to **binarylibs** is created in **openGauss-server**, you do not need to specify the parameter. However, if you do so, please note that the file is easy to be deleted by the **git clean** command.
>     
> 2. Each option in this script has a default value. The number of options is small and the dependency is simple. Therefore, this script is easy to use. If the required value is different from the default value, set this parameter based on the actual requirements.
>     

## **Software Compilation and Installation**

Software build and installation are to build code to generate software and install the software on a computer. The one-click build script [**build.sh**](http://build.sh) is provided. You can also manually configure environment variables. The two methods are described below in this section.

### **Prerequisites**

* The software and hardware have been prepared based on the requirements in [Setting up the Compilation Environment](https://docs.opengauss.org/en/docs/latest/docs/CompilationGuide/setting-up-the-compilation-environment.html), and the code has been downloaded by referring to \[Downloading Code\](#Downloading Code).
    
* Open-source software has been compiled and built. For details, see \[Compiling Open-source Software\](#Compiling Open-source Software). GCC 7.3 has been placed in the **output** directory based on the directory structure of the released and compiled third-party library.
    
* You are familiar with the parameter options and functions of the \[Introduction to [build.sh](http://build.sh)\](#Introduction to compile script) script.
    
* The code environment is clean, and no file is generated before the current compilation. For details, see [FAQ](https://docs.opengauss.org/en/docs/latest/docs/CompilationGuide/faqs.html).
    

### **Compilation Using the One-click Script**

1. Run the following command to go to the directory where the software code compilation script is stored:
    
    ```plaintext
    ""[user@linux sda]$ cd /sda/openGauss-server
    ```
    
2. Run the following command to compile openGauss:
    
    ```plaintext
    ""[user@linux openGauss-server]$ sh build.sh -m [debug | release | memcheck] -3rd [binarylibs path]
    ```
    
    For example:
    
    ```plaintext
    ""sh build.sh      # Compile and install openGauss of the release version. binarylibs or its soft link must exist in the code directory. Otherwise, the operation fails.
    sh build.sh -m debug -3rd /sdc/binarylibs            # Compile and install openGauss of the debug version.
    ```
    
3. If the following information is displayed, the compilation is successful:
    
    ```plaintext
    ""make compile sucessfully!
    ```
    
    * The software installation path after compilation is **/sda/openGauss-server/mppdb\_temp\_install**.
        
    * The compiled binary files are stored in **/sda/openGauss-server/mppdb\_temp\_install/bin**.
        
    * Compilation log: **./build/script/makemppdb\_pkg.log**
        
4. Import environment variables to initialize and start the database.
    
    ```plaintext
    ""export CODE_BASE=________     # openGauss-server path
    export GAUSSHOME=$CODE_BASE/mppdb_temp_install/
    export LD_LIBRARY_PATH=$GAUSSHOME/lib::$LD_LIBRARY_PATH
    export PATH=$GAUSSHOME/bin:$PATH
    ```
    

### **Manual Compilation**

1. Run the following command to go to the software code directory:
    
    ```plaintext
    ""[user@linux sda]$ cd /sda/openGauss-server
    ```
    
2. Obtain the third-party library binary file of the corresponding platform.
    
3. Configure environment variables, add **\_\_\_\_** based on the code download location.
    
    ```plaintext
    ""export CODE_BASE=________     # Path of the openGauss-server file
    export BINARYLIBS=________    # Path of the binarylibs file
    export GAUSSHOME=$CODE_BASE/dest/
    export GCC_PATH=$BINARYLIBS/buildtools/gcc7.3/
    export CC=$GCC_PATH/gcc/bin/gcc
    export CXX=$GCC_PATH/gcc/bin/g++
    export LD_LIBRARY_PATH=$GAUSSHOME/lib:$GCC_PATH/gcc/lib64:$GCC_PATH/isl/lib:$GCC_PATH/mpc/lib/:$GCC_PATH/mpfr/lib/:$GCC_PATH/gmp/lib/:$LD_LIBRARY_PATH
    export PATH=$GAUSSHOME/bin:$GCC_PATH/gcc/bin:$PATH
    ```
    
4. Select a version and configure it.
    
    **debug** version:
    
    ```plaintext
    ""./configure --gcc-version=7.3.0 CC=g++ CFLAGS='-O0' --prefix=$GAUSSHOME --3rd=$BINARYLIBS --enable-debug --enable-cassert --enable-thread-safety --with-readline --without-zlib
    ```
    
    **release** version:
    
    ```plaintext
    ""./configure --gcc-version=7.3.0 CC=g++ CFLAGS="-O2 -g3" --prefix=$GAUSSHOME --3rd=$BINARYLIBS --enable-thread-safety --with-readline --without-zlib
    ```
    
    **memcheck** version:
    
    ```plaintext
    ""./configure --gcc-version=7.3.0 CC=g++ CFLAGS='-O0' --prefix=$GAUSSHOME --3rd=$BINARYLIBS --enable-debug --enable-cassert --enable-thread-safety --with-readline --without-zlib --enable-memory-check
    ```
    
    > **NOTE:**
    > 
    > * *\[debug | release | memcheck\]* indicates that three target versions are available. The three target versions are as follows:
    >     
    >     * **release**: indicates that the binary program of the release version is generated. During this version build, the GCC high-level optimization option is configured to remove the kernel debugging code. This option is usually used in the production environment or performance test environment.
    >         
    >     * **debug**: indicates that a binary program of the debug version is generated. During this version build, the kernel code debugging function is added, which is usually used in the development self-test environment.
    >         
    >     * **memcheck**: indicates that a binary program of the memcheck version is generated. During this version build, the ASAN function is added based on the debug version to locate memory problems.
    >         
    > * On the ARM-based platform, **\-D\_\_USE\_NUMA** needs to be added to **CFLAGS**.
    >     
    > * On the **ARMv8.1** platform or a later version (for example, Kunpeng 920), **\-D\_\_ARM\_LSE** needs to be added to **CFLAGS**.
    >     
    > * If **binarylibs** is moved to **openGauss-server** or a soft link to **binarylibs** is created in **openGauss-server**, you do not need to specify the **–3rd** parameter. However, if you do so, please note that the file is easy to be deleted by the **git clean** command.
    >     
    > * To use the MOT, you need to add **–enable-mot** to the command.
    >     
    
5. Run the following commands to compile openGauss:
    
    ```plaintext
    ""[user@linux openGauss-server]$ make -sj
    [user@linux openGauss-server]$ make install -sj
    ```
    
6. If the following information is displayed, the compilation and installation are successful:
    
    ```plaintext
    ""openGauss installation complete.
    ```
    
    * The software installation path after compilation is *$GAUSSHOME*.
        
    * The compiled binary files are stored in *$GAUSSHOME*\*\*/bin\*\*.
        

## **Compiling the Installation Package**

To compile the installation package is to compile the code and generate the software installation package. The compilation and packaging process of the installation package is also integrated in [**build.sh**](http://build.sh).

### **Prerequisites**

* TThe software and hardware have been prepared based on the requirements for setting up the compilation environment, and the code has been downloaded by referring to [Downloading Code](https://docs.opengauss.org/en/docs/latest/docs/CompilationGuide/compiling-the-version.html#Downloading-Code).
    
* The open-source software has been compiled and built. For details, see \[Compiling Open-source Software\](#Compiling Open-source Software).
    
* You are familiar with the parameter options and functions of the \[Introduction to [build.sh](http://build.sh)\](#Introduction to compile script) script.
    
* The code environment is clean, and no file is generated before the current compilation. For details, see [FAQ](https://docs.opengauss.org/en/docs/latest/docs/CompilationGuide/faqs.html).
    

### **Procedure**

1. Run the following command to go to the code directory:
    
    ```plaintext
    ""[user@linux sda]$ cd /sda/openGauss-server
    ```
    
2. Run the following command to compile the openGauss installation package:
    
    ```plaintext
    ""[user@linux openGauss-server]$ sh build.sh -m [debug | release | memcheck] -3rd [binarylibs path] -pkg
    ```
    
    For example:
    
    ```plaintext
    ""sh build.sh -pkg      # Generate the openGauss installation package of the release version. binarylibs or its soft link must exist in the code directory. Otherwise, the operation fails.
    sh build.sh -m debug -3rd /sdc/binarylibs -pkg            # Generate the openGauss installation package of the debug version.
    ```
    
    Compared with [Software Compilation and Installation](https://docs.opengauss.org/en/docs/latest/docs/CompilationGuide/compiling-the-version.html#software-compilation-and-installation), this operation involves the process of generating software by one-click compilation and the process of encapsulating the software into an installation package. Compared with the [**build.sh**](http://build.sh) command in [Software Compilation and Installation](https://docs.opengauss.org/en/docs/latest/docs/CompilationGuide/compiling-the-version.html#software-compilation-and-installation), only the **\-pkg** option is added.
    
3. If the following information is displayed, the installation package compilation is successful:
    
    ```plaintext
    ""success!
    ```
    
    * The build and packaging log file is stored in **./build/script/makemppdb\_pkg.log**.
        

## **Verification After Build**

After the build is complete, perform the following steps to verify openGauss:

1. Run the following command to create user **omm** as user **root**:
    
    ```plaintext
    ""[user@linux sda]# useradd omm -g dbgrp
    [user@linux sda]# passwd omm
    ```
    
2. Add the following environment variables to **~/.bashrc** as user **omm**:
    
    ```plaintext
    ""export GAUSSHOME=/root/openGauss-server/dest/ ## Path of the build result. You can change the path as required.
    export LD_LIBRARY_PATH=$GAUSSHOME/lib:$LD_LIBRARY_PATH
    export PATH=$GAUSSHOME/bin:$PATH
    ```
    
    Run the following command for environment variables to take effect:
    
    ```plaintext
    ""  [user@linux sda]$ source ~/.bashrc
    ```
    
3. Create the data directory and log directory.
    
    ```plaintext
    ""[user@linux sda]$ mkdir ~/data
    [user@linux sda]$ mkdir ~/log
    [user@linux sda]$ chown -R omm:dbgrp /root/openGauss-server
    ```
    
4. Initialize the database.
    
    ```plaintext
    ""[user@linux sda]$ gs_initdb -D /home/omm/data --nodename=db1 
    ```
    
5. Start the database.
    
    ```plaintext
    ""[user@linux sda]$ gs_ctl start -D /home/omm/data -Z single_node -l /home/omm/log/opengauss.log
    ```
    
    After the database is started, you can run the **ps -ef | grep gaussdb** command to check the database process status, run the **gs\_ctl query -D /home/omm/data** command to check the database status, or run the **gsql -d postgres** command to enter the **gsql** command line to view database information.
    

## **openGauss-OM Build**

After the openGauss-server code repository is built using the source code, gs\_om does not exist. To use gs\_om, you need to compile openGauss-OM separately, copy the built **openGauss-*xxx*\-om.tar.gz** package to the directory where the openGauss-server installation package is located, and install the openGauss-OM in the same way as the enterprise edition.

## **Procedure**

1. Run the **git clone** command to clone the OM code repository:
    
    ```plaintext
    ""[user@linux sda]$ git clone https://gitee.com/opengauss/openGauss-OM.git
    ```
    
2. Run the following build command:
    
    ```plaintext
    ""[user@linux sda]$ cd openGauss-OM
    [user@linux sda]$ chmod +x build.sh
    [user@linux sda]$ export BINARYLIBS_PATH=/root/binarylibs (Enter the directory generated after the third-party software package is decompressed.)
    [user@linux sda]$ ./build.sh -3rd $BINARYLIBS_PATH
    ```
    
3. The Gauss-OM is built successfully if the following information is displayed:
    
    ```plaintext
    ""ROOT_DIR: /root/binarylibs
    Everything is ready.
    success!
    ```
