安装WSL

硬件:16G内存

系统版本:windows11

启用WSL

wsl --install

安装Linux发行版Ubuntu20.0

wsl --install Ubuntu-20.04

配置国内镜像源

  1. 编辑/etc/apt/sources.list文件,原有内容注释,添加以下内容到文件中:

    # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    deb http://security.ubuntu.com/ubuntu focal-security main restricted universe multiverse
    # deb-src http://security.ubuntu.com/ubuntu focal-security main restricted universe multiverse
    

WSL配置Swap

  1. 在 Windows PowerShell 或 CMD 中执行

    notepad "$env:USERPROFILE\.wslconfig"
    
  2. 添加以下内容(推荐配置)

    [wsl2]
    # 如果物理内存是 16GB,可以设置 12GB 给 WSL
    memory=12GB
    
    # 使用所有可用核心
    processors=8
    
    # Swap 设置为物理内存的 1-2 倍
    swap=8GB
    
    # 启用嵌套虚拟化(如果需要)
    nestedVirtualization=true
    
    # 启用 DNS 隧道
    dnsTunneling=true
    
  3. 创建Swap文件(推荐配置)

    # 创建 8GB 的 Swap 文件
    sudo fallocate -l 8G /swapfile
    sudo chmod 600 /swapfile
    sudo mkswap /swapfile
    sudo swapon /swapfile
    
    # 设置 swappiness(控制 swap 使用倾向,值越小越少使用 swap)
    sudo sysctl vm.swappiness=10
    # 永久生效
    echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
    

安装nodejs

  1. 安装nvm

    # 下载最新版本到nodejs官网查看
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
    
  2. 使用nvm安装nodejs

    nvm install 24
    
  3. 验证安装结果

    node -v; npm -v
    

安装库和工具集

在Ubuntu20.04中安装如下库和工具集

sudo apt-get update; sudo apt-get install -y binutils; sudo apt-get install -y binutils-dev; sudo apt-get install -y git; sudo apt-get install -y git-lfs; sudo apt-get install -y gnupg; sudo apt-get install -y flex; sudo apt-get install -y bison; sudo apt-get install -y gperf; sudo apt-get install -y build-essential; sudo apt-get install -y zip; sudo apt-get install -y curl; sudo apt-get install -y zlib1g-dev; sudo apt-get install -y gcc-multilib; sudo apt-get install -y g++-multilib; sudo apt-get install -y libc6-dev-i386; sudo apt-get install -y libc6-dev-amd64; sudo apt-get install -y lib32ncurses5-dev; sudo apt-get install -y x11proto-core-dev; sudo apt-get install -y libx11-dev; sudo apt-get install -y lib32z1-dev; sudo apt-get install -y ccache; sudo apt-get install -y libgl1-mesa-dev; sudo apt-get install -y libxml2-utils; sudo apt-get install -y xsltproc; sudo apt-get install -y unzip; sudo apt-get install -y m4; sudo apt-get install -y bc; sudo apt-get install -y gnutls-bin; sudo apt-get install -y python3.9; sudo apt-get install -y python-is-python3; sudo apt-get install -y python3-pip; sudo apt-get install -y ruby; sudo apt-get install -y genext2fs; sudo apt-get install -y device-tree-compilersudo apt-get install -y make; sudo apt-get install -y libffi-dev; sudo apt-get install -y e2fsprogs; sudo apt-get install -y pkg-config; sudo apt-get install -y perl; sudo apt-get install -y openssl; sudo apt-get install -y libssl-dev; sudo apt-get install -y libelf-dev; sudo apt-get install -y libdwarf-dev; sudo apt-get install -y u-boot-tools; sudo apt-get install -y mtd-utils; sudo apt-get install -y cpio; sudo apt-get install -y doxygen; sudo apt-get install -y liblz4-tool; sudo apt-get install -y openjdk-8-jre; sudo apt-get install -y gcc; sudo apt-get install -y g++; sudo apt-get install -y texinfo; sudo apt-get install -y dosfstools; sudo apt-get install -y mtools; sudo apt-get install -y default-jre; sudo apt-get install -y default-jdk; sudo apt-get install -y libncurses5; sudo apt-get install -y apt-utils; sudo apt-get install -y wget; sudo apt-get install -y scons; sudo apt-get install -y python3.9-distutils; sudo apt-get install -y tar; sudo apt-get install -y rsync; sudo apt-get install -y git-core; sudo apt-get install -y libxml2-dev; sudo apt-get install -y lib32z-dev; sudo apt-get install -y grsync; sudo apt-get install -y xxd; sudo apt-get install -y libglib2.0-dev; sudo apt-get install -y libpixman-1-dev; sudo apt-get install -y kmod; sudo apt-get install -y jfsutils; sudo apt-get install -y reiserfsprogs; sudo apt-get install -y xfsprogs; sudo apt-get install -y squashfs-tools; sudo apt-get install -y pcmciautils; sudo apt-get install -y quota; sudo apt-get install -y ppp; sudo apt-get install -y libtinfo-dev; sudo apt-get install -y libtinfo5; sudo apt-get install -y libncurses5-dev; sudo apt-get install -y libncursesw5; sudo apt-get install -y libstdc++6; sudo apt-get install -y gcc-arm-none-eabi; sudo apt-get install -y vim; sudo apt-get install -y ssh; sudo apt-get install -y locales; sudo apt-get install -y libxinerama-dev; sudo apt-get install -y libxcursor-dev; sudo apt-get install -y libxrandr-dev; sudo apt-get install -y libxi-dev

缺少依赖导致编译失败

sudo apt-get install autoconf libtool libtool-bin

检查python版本,需要大于等于3.8

配置pip源

mkdir ~/.pip
vim ~/.pip/pip.conf

# 以下内容添加到~/.pip/pip.conf
[global]
index-url = https://repo.huaweicloud.com/repository/pypi/simple/
trusted-host = repo.huaweicloud.com
timeout = 120

获取OpenHarmony源码

  1. 注册gitcode账号

  2. gitcode配置ssh公钥

    • 生成公钥:ssh-keygen,后面可以无脑回车
    • cat ~/.ssh/id_rsa.pub查看公钥,并配置到gitcode中
  3. 安装git和git-lfs(如果未安装)

    sudo apt-get update
    sudo apt-get install git git-lfs
    
  4. git配置用户信息

    git config --global user.name Leal
    git config --global user.email lealtan@163.com
    git config --global credential.helper store
    
  5. 安装repo工具

    mkdir ~/bin
    curl https://gitee.com/oschina/repo/raw/fork_flow/repo-py3 -o ~/bin/repo 
    chmod a+x ~/bin/repo
    pip3 install -i https://repo.huaweicloud.com/repository/pypi/simple requests
    
    # 设置环境变量
    vim ~/.bashrc
    # 文件末尾添加repo路径信息
    export PATH=~/bin:$PATH
    # 应用环境变量
    source ~/.bashrc
    
    # 验证安装结果
    repo --version
    
  6. 获取OpenHarmony主干源码

    # 在根目录新建源码路径
    cd ~
    mkdir ohos_master
    cd ohos_master
    
    # 使用repo + ssh下载
    repo init -u https://gitcode.com/openharmony/manifest.git -b master --no-repo-verify
    repo sync -c
    repo forall -c 'git lfs pull'
    
  7. 执行prebuilts脚本,安装编译器及二进制工具

    bash build/prebuilts_download.sh
    

安装编译工具

  1. 使用pip安装hb

    python3 -m pip install --user build/hb
    
  2. 配置环境变量:~/.local/bin

  3. 检查hb安装结果

    hb help
    
Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐