前言

本文全篇使用国内镜像源来辅助安装,若不需要则可以自行修改掉哦。

当然,若某一天镜像源失效了,就需要自行寻找其他的镜像源来代替力。

相关信息

  • Ubuntu 20.04.6 LTS
  • Python 3.11.6

系统配置

创建密钥且仅密钥登录(可选)

bhao@ubuntu-2004:~$ ssh-keygen -t rsa # 生成密钥
bhao@ubuntu-2004:~$ ssh-copy-id bhao@ubuntu-2004 # 将公钥复制到服务器

通过 sudo vim /etc/ssh/sshd_config 修改 sshd 配置内容,确保以下内容的配置(开启密钥认证,关闭密码认证)。

PubkeyAuthentication yes
PasswordAuthentication no

通过 sudo systemctl restart ssh 重启 SSH 服务,并通过 SFTP 或其他方式下载私钥(别忘了下)

配置 sudo 免登录(可选)

通过 sudo visudo 编辑 /etc/sudoers ,添加以下内容,注意更换成自己所需的用户名。

bhao ALL=(ALL) NOPASSWD:ALL

通过 Ctrl+X 进行推出,输入 Y 进行保存,输入 Ctrl+T 选择保存到的地方,选择 sudoers 并回车,检查无误多次按 Y 确认即可。

使用 chsrc 更换合适的镜像源

https://gitee.com/RubyMetric/chsrc
bhao@ubuntu-2004:~$ curl -L https://gitee.com/RubyMetric/chsrc/releases/download/pre/chsrc-x64-linux -o chsrc; chmod +x ./chsrc
bhao@ubuntu-2004:~$ sudo ./chsrc set ubuntu

安装 pyenv

安装环境依赖

https://github.com/pyenv/pyenv/wiki#suggested-build-environment
bhao@ubuntu-2004:~$ sudo apt update; sudo apt install build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl git \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

安装 pyenv

官方提供的自动安装脚本如下。

https://github.com/pyenv/pyenv?tab=readme-ov-file#automatic-installer
curl https://pyenv.run | bash

考虑到官方的安装脚本中 raw.githubusercontent.com 在大陆可能无法正常访问,故直接前往该项目获取内容并修改如下内容以使用 githubfast 来加速克隆。

GITHUB="https://github.com/" => GITHUB="https://githubfast.com/"

完整脚本内容如下。

#!/usr/bin/env bash

set -e
[ -n "$PYENV_DEBUG" ] && set -x

if [ -z "$PYENV_ROOT" ]; then
  if [ -z "$HOME" ]; then
    printf "$0: %s\n" \
      "Either \$PYENV_ROOT or \$HOME must be set to determine the install location." \
      >&2
    exit 1
  fi
  export PYENV_ROOT="${HOME}/.pyenv"
fi

colorize() {
  if [ -t 1 ]; then printf "\e[%sm%s\e[m" "$1" "$2"
  else echo -n "$2"
  fi
}

# Checks for `.pyenv` file, and suggests to remove it for installing
if [ -d "${PYENV_ROOT}" ]; then
  { echo
    colorize 1 "WARNING"
    echo ": Can not proceed with installation. Kindly remove the '${PYENV_ROOT}' directory first."
    echo
  } >&2
    exit 1
fi

failed_checkout() {
  echo "Failed to git clone $1"
  exit -1
}

checkout() {
  [ -d "$2" ] || git -c advice.detachedHead=0 clone --branch "$3" --depth 1 "$1" "$2" || failed_checkout "$1"
}

if ! command -v git 1>/dev/null 2>&1; then
  echo "pyenv: Git is not installed, can't continue." >&2
  exit 1
fi

# Check ssh authentication if USE_SSH is present
if [ -n "${USE_SSH}" ]; then
  if ! command -v ssh 1>/dev/null 2>&1; then
    echo "pyenv: configuration USE_SSH found but ssh is not installed, can't continue." >&2
    exit 1
  fi

  # `ssh -T [email protected]' returns 1 on success
  # See https://docs.github.com/en/authentication/connecting-to-github-with-ssh/testing-your-ssh-connection
  ssh -T [email protected] 1>/dev/null 2>&1 || EXIT_CODE=$?
  if [[ ${EXIT_CODE} != 1 ]]; then
      echo "pyenv: github ssh authentication failed."
      echo
      echo "In order to use the ssh connection option, you need to have an ssh key set up."
      echo "Please generate an ssh key by using ssh-keygen, or follow the instructions at the following URL for more information:"
      echo
      echo "> https://docs.github.com/en/repositories/creating-and-managing-repositories/troubleshooting-cloning-errors#check-your-ssh-access"
      echo
      echo "Once you have an ssh key set up, try running the command again."
    exit 1
  fi
fi

if [ -n "${USE_SSH}" ]; then
  GITHUB="[email protected]:"
else
  GITHUB="https://githubfast.com/"
fi

checkout "${GITHUB}pyenv/pyenv.git"            "${PYENV_ROOT}"                           "${PYENV_GIT_TAG:-master}"
checkout "${GITHUB}pyenv/pyenv-doctor.git"     "${PYENV_ROOT}/plugins/pyenv-doctor"      "master"
checkout "${GITHUB}pyenv/pyenv-update.git"     "${PYENV_ROOT}/plugins/pyenv-update"      "master"
checkout "${GITHUB}pyenv/pyenv-virtualenv.git" "${PYENV_ROOT}/plugins/pyenv-virtualenv"  "master"

if ! command -v pyenv 1>/dev/null; then
  { echo
    colorize 1 "WARNING"
    echo ": seems you still have not added 'pyenv' to the load path."
    echo
  } >&2

  { # Without args, `init` commands print installation help
    "${PYENV_ROOT}/bin/pyenv" init || true
    "${PYENV_ROOT}/bin/pyenv" virtualenv-init || true
  } >&2
fi

添加至系统环境

安装完成后,添加以下内容到 ~/.profile (若 ~/.bash_profile 存在则添加至 ~/.bash_profile )中。

export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

添加以下内容到 ~/.bashrc

export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

通过 source ~/.bashrc 重载即可。

配置镜像源

添加以下内容到 ~/.bashrc

export PYTHON_BUILD_MIRROR_URL="https://registry.npmmirror.com/-/binary/python"
export PYTHON_BUILD_MIRROR_URL_SKIP_CHECKSUM=1

安装 Python 3.11.6

bhao@ubuntu-2004:~$ pyenv install 3.11.6
bhao@ubuntu-2004:~$ pyenv global 3.11.6
bhao@ubuntu-2004:~$ python -V
Python 3.11.6

安装 BCC

安装依赖

https://github.com/iovisor/bcc/blob/master/INSTALL.md#ubuntu---source

通过安装文档选择合适的命令安装依赖。

bhao@ubuntu-2004:~$ sudo apt install -y zip bison build-essential cmake flex git libedit-dev \
  libllvm12 llvm-12-dev libclang-12-dev python zlib1g-dev libelf-dev libfl-dev python3-setuptools \
  liblzma-dev arping netperf iperf

创建 Python 虚拟环境

bhao@ubuntu-2004:~$ mkdir test
bhao@ubuntu-2004:~$ cd test
bhao@ubuntu-2004:~/test$ python -m venv venv

安装并编译 BCC

bhao@ubuntu-2004:~/test$ cd ~
bhao@ubuntu-2004:~$ git clone https://githubfast.com/iovisor/bcc.git
bhao@ubuntu-2004:~$ mkdir bcc/build; cd bcc/build
bhao@ubuntu-2004:~/bcc/build$ cmake ..
bhao@ubuntu-2004:~/bcc/build$ make
bhao@ubuntu-2004:~/bcc/build$ sudo make install
bhao@ubuntu-2004:~/bcc/build$ cmake .. -DPYTHON_CMD=/home/bhao/test/venv/bin/python3
bhao@ubuntu-2004:~/bcc/build$ pushd src/python/
bhao@ubuntu-2004:~/bcc/build/src/python$ make
Built target bcc_py_-home-bhao-test-venv-bin-python3
bhao@ubuntu-2004:~/bcc/build/src/python$ sudo make install
bhao@ubuntu-2004:~/bcc/build/src/python$ popd
bhao@ubuntu-2004:~/bcc/build/src/python$ cd ~/test
bhao@ubuntu-2004:~/test$ source venv/bin/activate
(venv) bhao@ubuntu-2004:~/test$ pip list
Package    Version
---------- ---------------
bcc        0.31.0+3130fe8c
bcc        0.31.0+3130fe8c
bcc        0.31.0+3130fe8c
pip        23.2.1
setuptools 65.5.0

测试 BCC

测试文件 hello_world.py 内容如下。

#!/home/bhao/test/venv/bin/python3
from bcc import BPF

program = r"""
int hello(void *ctx) {
    bpf_trace_printk("Hello World!\n");
    return 0;
}
"""

b = BPF(text=program)
syscall = b.get_syscall_fnname("execve")
b.attach_kprobe(event=syscall, fn_name="hello")

b.trace_print()

运行测试文件。

(venv) bhao@ubuntu-2004:~/test$ sudo hello_world.py

若没有输出,另起一个 SSH 连接并随意输入命令即可发现输出。

后续

若出现了如下报错

bhao@ubuntu-2004:~/bcc/build/src/python$ sudo make install
Built target bcc_py_-home-bhao-test-venv-bin-python3
Install the project...
-- Install configuration: "Release"
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: setup.py --help [cmd1 cmd2 ...]
   or: setup.py --help-commands
   or: setup.py cmd --help

可尝试以下方法解决。

cmake .. -DPYTHON_PREFIX=/home/bhao/test/venv/