Basic OS Environment Setup

Before setting up the environment for a development board, you must set up the basic OS environment for OpenHarmony first. The basic OS environment refers to the building environment and development environment of OpenHarmony. You can choose one of the following methods to set up the basic OS environment based on your preference and the hardware and software used.

  • Method 1

    Install a Windows development environment and use docker to install the Linux environment. For details, see the following sections:

  1. Environment Requirements
  2. Installing a Windows Development Environment
  3. Using Docker to Install the Linux Environment
  • Method 2

    Install a Windows development environment and use an installation package to install the Linux environment. For details, see the following sections:

  1. Environment Requirements
  2. Installing a Windows Development Environment
  3. Using an Installation Package to Install the Linux Environment
  • Method 3

    Install the Linux development and building environment. For details, see Linux Development Environment in HUAWEI DevEco Studio User Guide.

Environment Requirements

Hardware and Software Requirements

Table 1 Development platform requirements

Hardware

Software

Description

Remarks

Windows workstation (host computer)

Operating system

Windows XP/Windows7/Windows10

You can develop programs on the Windows workstation or Linux server via a remote login.

Linux compile server

Operating system

Ubuntu 16.04 64-bit or later is used for the Linux OS, and bash is used as the shell.

NOTE:

Common software such as samba and vim is installed in the OS by default. Adaptation is required to support Linux file sharing.

Development Tools for Windows

Table 2 Development tools and obtaining methods

Development Tool

Description

How to Obtain

Visual Studio Code

Edits code.

https://code.visualstudio.com/

HUAWEI DevEco Device Tool

Supports code editing, compilation, burning, and debugging.

NOTE:

HUAWEI DevEco Device Tool is a one-stop integrated development environment (IDE) provided for developers of OpenHarmony-based smart devices. It supports C and C++ languages and is installed in Visual Studio Code as a plug-in.

https://device.harmonyos.com/cn/ide

IPOP, PuTTY, or other HyperTerminal

Remotely connects to a Linux compile server or a serial port.

Internet (for example, https://www.putty.org/)

Build Tools for Linux

The following table describes the tools required for setting up the general environment for a Linux server and how to obtain these tools.

Table 3 Development tools and obtaining methods

Development Tool

Description

How to Obtain

Python3.7+

Executes script compilation.

Internet

gn

Generates ninja compilation scripts.

https://repo.huaweicloud.com/harmonyos/compiler/gn/1717/linux/gn-linux-x86-1717.tar.gz

ninja

Executes ninja compilation scripts.

https://repo.huaweicloud.com/harmonyos/compiler/ninja/1.9.0/linux/ninja.1.9.0.tar

LLVM

Functions as the compiler toolchain.

https://repo.huaweicloud.com/harmonyos/compiler/clang/9.0.0-36191/linux/llvm-linux-9.0.0-36191.tar

hb

Compiles the source code.

Internet

Installing a Windows Development Environment

The OpenHarmony IDE Development Tool (HUAWEI DevEco Device Tool) integrates the capabilities of terminal tools. For details about how to install the tool, see Preparation and Creating a Project.

Using Docker to Install the Linux Environment

You can use Docker to quickly install the Linux environment. For details, see Setting Up the Docker Environment in Using Docker to Prepare the Build Environment.

Using an Installation Package to Install the Linux Environment

NOTICE:

  • If you acquire the source code using an HPM component or HPM CLI tool, compilation tools like gn and ninja are not required.
  • (Recommended) If you obtain the source code via the mirror site or code repository, install compilation tools such as gn and ninja. When installing gn and ninja, ensure that the environment variable paths of the compilation tools are unique.

Connecting to a Linux Server

Using PuTTY to log in to a Linux server from a PC running Windows

  1. Open PuTTY, enter the IP address of the Linux server, and click Open.

    Figure 1 PuTTY configuration

  2. Click Yes in the PuTTY Security Alert dialog box.

  3. Enter the account and password.

    Figure 2 Login

  4. Check that the login is successful.

    Figure 3 Successful login

Installing and Configuring Python

  1. Start a Linux server.

  2. Check the Python version (Python 3.7 or later is required).

    python3 --version
    

    If Python version is earlier than 3.7, reinstall Python. Do as follows to install Python, for example, Python 3.8.

    1. Check the Ubuntu version.
    cat /etc/issue
    
    1. Install Python based on the Ubuntu version.
      • If the Ubuntu version is 18 or later, run the following command:

        sudo apt-get install python3.8
        
      • If the Ubuntu version is 16, perform the following steps:

        a. Install dependency packages.

        sudo apt update && sudo apt install software-properties-common
        

        b. Add the source of deadsnakes PPA and press Enter.

        sudo add-apt-repository ppa:deadsnakes/ppa
        

        c. Install Python 3.8.

        sudo apt upgrade && sudo apt install python3.8
        
  3. Set the soft link of python and python3 to python3.8.

    sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1
    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
    
  4. Install and upgrade the Python package management tool (pip3) using either of the following methods:

    • Command line:

      sudo apt-get install python3-setuptools python3-pip -y
      sudo pip3 install --upgrade pip
      
    • Installation package:

      curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
      python get-pip.py
      

Installing gn

  1. Start a Linux server.

  2. Download gn.

  3. Create the gn folder in the root directory.

    mkdir ~/gn
    
  4. Decompress the gn installation package to ~/gn.

    tar -xvf gn-linux-x86-1717.tar.gz -C ~/gn
    
  5. Set an environment variable.

    vim ~/.bashrc
    

    Copy the following command to the last line of the .bashrc file, save the file, and exit.

    export PATH=~/gn:$PATH
    
  6. Validate the environment variable.

    source ~/.bashrc
    

Installing ninja

  1. Start a Linux server.

  2. Download ninja.

  3. Decompress the ninja installation package to ~/ninja.

    tar -xvf ninja.1.9.0.tar -C ~/
    
  4. Set an environment variable.

    vim ~/.bashrc
    

    Copy the following command to the last line of the .bashrc file, save the file, and exit.

    export PATH=~/ninja:$PATH
    
  5. Validate the environment variable.

    source ~/.bashrc
    

Installing LLVM

  1. Start a Linux server.

  2. Download LLVM.

  3. Decompress the LLVM installation package to ~/llvm.

    tar -xvf llvm-linux-9.0.0-36191.tar -C ~/
    
  4. Set an environment variable.

    vim ~/.bashrc
    

    Copy the following command to the last line of the .bashrc file, save the file, and exit.

    export PATH=~/llvm/bin:$PATH
    
  5. Validate the environment variable.

    source ~/.bashrc
    

Installing hb

Prerequisites

Installation

  1. Install hb.

    python3 -m pip install --user ohos-build
    
  2. Set an environment variable.

    vim ~/.bashrc
    

    Copy the following command to the last line of the .bashrc file, save the file, and exit.

    export PATH=~/.local/bin:$PATH
    

    Update the environment variable.

    source ~/.bashrc
    
  3. Run the hb -h command. If the following information is displayed, the installation is successful:

    usage: hb
    
    OHOS build system
    
    positional arguments:
      {build,set,env,clean}
        build               Build source code
        set                 OHOS build settings
        env                 Show OHOS build env
        clean               Clean output
    
    optional arguments:
      -h, --help            Show this help message and exit
    

Uninstallation

python3 -m pip uninstall ohos-build

NOTE: If you encounter any problem during the installation, resort to the FAQ.

Obtaining OpenHarmony Source Code

You need to acquire OpenHarmony source code, download it on a Linux server, and decompress it. For more obtaining methods, see Source Code Acquisition.

FAQ

What should I do if garbled characters and segmentation faults occur during hb installation?

  • Symptom

    Garbled characters and segmentation faults occur during the execution of the python3 -m pip install --user ohos-build command.

  • Possible Causes

    pip is of an early version.

  • Solutions

    Upgrade pip.

    python3 -m pip install -U pip
    

What should I do if the message "cannot import 'sysconfig' from 'distutils'" is displayed during hb installation?

  • Symptom

    The message "cannot import 'sysconfig' from 'distutils'" is displayed during the execution of the python3 -m pip install --user ohos-build command.

  • Possible Causes

    The distutils module is unavailable.

  • Solutions

    Install distutils.

    sudo apt-get install python3.8-distutils
    

What should I do if the message "module 'platform' has no attribute 'linux_distribution'" is displayed during hb Installation?

  • Symptom

The message "module 'platform' has no attribute 'linux_distribution'" is displayed during the execution of the python3 -m pip install --user ohos-build command.

  • Possible Causes

    There is a compatibility issue of python3-pip.

  • Solutions

    Reinstall pip.

    sudo apt remove python3-pip
    curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
    python get-pip.py
    

What should I do if the message "ImportError: No module named apt_pkg" is displayed during the execution of an unidentifiable command?

  • Symptom

    The message "ImportError: No module named apt_pkg" is displayed when an unidentifiable command is executed on the Linux server.

  • Possible Causes

    There is a compatibility issue of python3-apt.

  • Solutions

    Reinstall python3-apt.

    sudo apt-get remove  python3-apt
    sduo apt-get install python3-apt