Skip to content

Instantly share code, notes, and snippets.

@Timothy-Liuxf
Last active January 23, 2023 12:30
Show Gist options
  • Save Timothy-Liuxf/a5aa3da80beeda3a5f24d2bad8bd81f1 to your computer and use it in GitHub Desktop.
Save Timothy-Liuxf/a5aa3da80beeda3a5f24d2bad8bd81f1 to your computer and use it in GitHub Desktop.
Install gRPC for Python and C++ on Linux
#!/bin/bash
# Install gRPC SDK for C++ or Python3 v1.0.1
# MIT License
# Copyright (c) 2022 Timothy Liu
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
VERSION='1.0.1'
NO_PROMPT='false'
INSTALL_VERSION='1.49.1'
CPP_LANG='cpp'
PYTHON3_LANG='python3'
INSTALL_LANG="$PYTHON3_LANG"
HELP_TEXT=$(cat <<EOF
Install gRPC for C++ and Python, v$VERSION
MIT LICENSE
Timothy Liu, Copyright (C) 2022
Usage: script [-h] [-y] [-l <language>] [-i <version>]
Options:
-h Display help text
-i The version of gRPC to install
-l The language to install: $PYTHON3_LANG, $CPP_LANG. Default: $INSTALL_LANG
-v Display version information
-y Do not ask before installation
EOF
)
while getopts ':hvyl:i:' 'OPT'; do
case ${OPT} in
'h')
echo "$HELP_TEXT"
exit
;;
'v')
echo v"$VERSION"
exit
;;
'y')
NO_PROMPT='true'
;;
'l')
INSTALL_LANG="${OPTARG}"
;;
'i')
INSTALL_VERSION="${OPTARG}"
;;
esac
done
install_grpc_cpp() {
# Check for cmake
if ! command -v cmake &> /dev/null
then
echo "[ERROR] CMake is required!" >&2
exit 1
fi
# Check for g++
if ! command -v g++ &> /dev/null
then
echo "[ERROR] g++ is required!" >&2
exit 1
fi
TMP_DIR="./tmp"
mkdir -p $TMP_DIR
pushd $TMP_DIR
INSTALL_DIR="$HOME/.local/grpc"
mkdir -p $INSTALL_DIR
sudo apt install -y build-essential autoconf libtool pkg-config make
git clone --recurse-submodules -b v$INSTALL_VERSION --depth 1 --shallow-submodules https://github.com/grpc/grpc
cd grpc
mkdir -p cmake/build
pushd cmake/build
cmake -DgRPC_INSTALL=ON \
-DgRPC_BUILD_TESTS=OFF \
-DCMAKE_INSTALL_PREFIX=$MY_INSTALL_DIR \
../..
make -j$(nproc)
sudo make install
export PATH="$INSTALL_DIR/bin:$PATH"
echo "export PATH=\"$INSTALL_DIR/bin:\$PATH\"" >> $HOME/.bashrc
popd
}
install_grpc_python3() {
# python3 -m pip install -r requirements.txt
python3 -m pip install grpcio==$INSTALL_VERSION grpcio_tools==$INSTALL_VERSION
}
if [ "$INSTALL_LANG" = "$CPP_LANG" ]
then
install_grpc_cpp
exit
fi
if [ "$INSTALL_LANG" != "$PYTHON3_LANG" ]
then
echo "Invalid argument: -l $INSTALL_LANG" >&2
echo >&2
echo "$HELP_TEXT" >&2
exit 1
fi
# Check for python protobuf
python3 -c "import grpc; import grpc_tools" > /dev/null 2&>1
# if ! command -v protoc &> /dev/null
if [ $? -ne 0 ]
then
# If -y is not specified, ask for installation
if ! $NO_PROMPT
then
while true; do
read -p "Will install grpc. Continue? (Y/n) " yn
case $yn in
[Yy]* )
break
;;
[Nn]* )
echo "Intsallation interrupted!"
exit 1
;;
* ) echo "Please answer Y or n.";;
esac
done
fi
install_grpc_python3
fi
echo "OK!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment