ros2_humble_install.sh 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #!/bin/bash
  2. # Warning: This script is ONLY for ROS2 Humble in ubuntu 22.04
  3. # set -x
  4. set -e
  5. # Get script directory
  6. SCRIPT_DIR=$(dirname "$0")
  7. # Get the username of the non-root user
  8. USERNAME=$USERNAME
  9. echo "Current user is: $USERNAME"
  10. echo "Script directory is: $SCRIPT_DIR"
  11. # Save logs to files
  12. LOG_FILE="${SCRIPT_DIR}/ros2_humble_install.log"
  13. ERR_FILE="${SCRIPT_DIR}/ros2_humble_install.err"
  14. echo "Cleaning up traces of last installation..."
  15. rm -f ${LOG_FILE}
  16. rm -f ${ERR_FILE}
  17. # Redirect output to console and log files
  18. exec 1> >(tee -a ${LOG_FILE} )
  19. exec 2> >(tee -a ${ERR_FILE} >&2)
  20. # Output log info to console
  21. echo "Installation logs will be saved to ${LOG_FILE}"
  22. echo "Installation errors will be saved to ${ERR_FILE}"
  23. # Waiting to start
  24. echo "Start to install ROS2 Humble..."
  25. sleep 3
  26. # No Password sudo config
  27. echo "Setting no-passwd sudo"
  28. sudo sed -i 's/^%sudo.*/%sudo ALL=(ALL) NOPASSWD:ALL/g' /etc/sudoers
  29. # Get architecture of the system
  30. if [ $(uname -m) = "x86_64" ]; then
  31. MIRROR="https://mirrors.tuna.tsinghua.edu.cn/ubuntu/"
  32. else
  33. MIRROR="https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/"
  34. fi
  35. echo "Current system architecture is: $(uname -m)"
  36. echo "Current mirror is: $MIRROR"
  37. # Backup original software sources
  38. echo "Backing up sources.list to /etc/apt/sources.list.backup ..."
  39. sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
  40. # Clear original software sources
  41. echo "# Ubuntu Mirror Settings" | sudo tee /etc/apt/sources.list
  42. # Replace software sources using tee
  43. echo "deb $MIRROR jammy main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list
  44. echo "deb $MIRROR jammy-updates main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list
  45. echo "deb $MIRROR jammy-backports main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list
  46. if [ $(uname -m) = "x86_64" ]; then
  47. echo "deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list
  48. else
  49. echo "deb http://ports.ubuntu.com/ubuntu-ports/ jammy-security main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list
  50. fi
  51. # System update
  52. echo "Start to update software..."
  53. sudo apt update
  54. echo "Start to upgrade software..."
  55. sudo apt upgrade -y
  56. # Install pip
  57. echo "Installing pip..."
  58. sudo apt install python3-dev -y
  59. sudo apt install pip -y # If you haven't already installed pip
  60. # Set default pip source
  61. echo "configuring pip source..."
  62. pip config set global.index-url http://pypi.tuna.tsinghua.edu.cn/simple
  63. pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn
  64. # check for UTF-8
  65. echo "Checking locale..."
  66. echo "Current locale:"
  67. locale
  68. # Set language
  69. echo "Setting language..."
  70. sudo apt update && sudo apt install locales
  71. sudo locale-gen en_US en_US.UTF-8
  72. sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
  73. export LANG=en_US.UTF-8
  74. # verify settings
  75. echo "Checking locale..."
  76. locale
  77. sudo apt install vim -y
  78. # Enable Ubuntu Universe repository
  79. echo "Enabling Ubuntu Universe repository..."
  80. sudo apt install software-properties-common -y
  81. sudo add-apt-repository universe -y
  82. # Add the ROS 2 GPG key with apt
  83. echo "Adding the ROS 2 GPG key with apt..."
  84. sudo apt update && sudo apt install curl -y
  85. sudo curl -sSL http://hualiyun.cc:7000/codeFromRos/rosdistro/raw/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
  86. # Add the repository to your sources list
  87. echo "Adding the ROS 2 repository to your sources list..."
  88. # echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
  89. echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] https://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
  90. # Update apt repository caches
  91. echo "Updating software caches..."
  92. sudo apt update
  93. echo "Upgrading software..."
  94. sudo apt upgrade -y
  95. # Install ROS
  96. echo "Installing ROS2 Humble..."
  97. if [ $(uname -m) = "aarch64" ]; then
  98. sudo apt install libicu70=70.1-2 -y
  99. fi
  100. # ROS Desktop Install(ROS, RViz, demos, tutorials)
  101. sudo apt install ros-humble-desktop -y
  102. # ROS Base Install(Communication libraries, message packages, command line tools but no GUI tools)
  103. sudo apt install ros-humble-ros-base -y
  104. # Development tools(Compilers and other tools to build ROS packages)
  105. sudo apt install ros-dev-tools -y
  106. # nav2
  107. sudo apt install ros-humble-nav2-common ros-humble-navigation2 -y
  108. # Install build tool
  109. sudo apt install python3-colcon-common-extensions -y
  110. sudo apt install ros-humble-pcl-* -y
  111. # Environment setup
  112. if ! grep -q "source /opt/ros/humble/setup.bash" ~/.bashrc; then
  113. echo "# ROS2 HUMBLE ENVIRONMENT SETTINGS" | sudo tee -a ~/.bashrc
  114. echo "source /opt/ros/humble/setup.bash" | sudo tee -a ~/.bashrc
  115. echo "ROS2 Humble environment setup added to ~/.bashrc"
  116. else
  117. echo "ROS2 Humble environment is already set in ~/.bashrc"
  118. fi
  119. source ~/.bashrc
  120. # Create your ROS2 workspace
  121. if [ -d "$workspace_dir" ]; then
  122. echo " ROS2 workspace already exists, skip creating."
  123. else
  124. echo "Creating ROS2 workspace"
  125. cd ~
  126. mkdir -p ros2_workspace/src
  127. cd ~/ros2_workspace
  128. # Install package dependencies
  129. echo "Installing package dependencies..."
  130. sudo pip install rosdep
  131. sudo pip install rosdepc
  132. sudo rosdepc init > /dev/null
  133. rosdepc update > /dev/null
  134. rosdep install --from-paths src --ignore-src --rosdistro humble -y
  135. echo "Building workspace..."
  136. colcon build
  137. cd ~
  138. fi
  139. # System update again
  140. sudo apt update
  141. sudo apt dist-upgrade -y
  142. # install cartographer
  143. echo "Installing cartographer package dependencies..."
  144. sudo apt-get install ninja-build -y
  145. sudo apt-get install libgflags-dev -y
  146. sudo apt-get install libgoogle-glog-dev -y
  147. sudo apt-get install stow -y
  148. sudo apt-get install libgtest-dev -y
  149. sudo apt-get install libceres-dev -y
  150. sudo apt-get install -y google-mock libboost-all-dev libeigen3-dev liblua5.2-dev libprotobuf-dev libsuitesparse-dev libwebp-dev protobuf-compiler libatlas-base-dev libsuitesparse-dev liblapack-dev ros-humble-tf2-eigen libcairo2-dev libabsl-dev
  151. PROTOBUF_DIR="protobuf"
  152. if test ! -d "$PROTOBUF_DIR"; then
  153. set -o errexit
  154. set -o verbose
  155. VERSION="v3.4.1"
  156. # Build and install proto3.
  157. git clone http://hualiyun.cc:7000/codeFromRos/protobuf.git
  158. cd protobuf
  159. git checkout tags/${VERSION}
  160. mkdir build
  161. cd build
  162. cmake -G Ninja \
  163. -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
  164. -DCMAKE_BUILD_TYPE=Release \
  165. -Dprotobuf_BUILD_TESTS=OFF \
  166. ../cmake
  167. ninja
  168. sudo ninja install
  169. cd ~
  170. rm -rf protobuf
  171. echo "protobuf install succ"
  172. else
  173. echo "protobuf is already installed"
  174. fi
  175. echo "Install cartographer package dependencies succ"
  176. # Verifying ROS2 installation
  177. # clear
  178. # Define the variables to be printed
  179. TEXT1="ROS2 Humble installation completed!"
  180. TEXT2="Please open new terminals and run commands to verify the installation:"
  181. TEXT3="ros2 run demo_nodes_cpp talker"
  182. TEXT4="ros2 run demo_nodes_py listener"
  183. # Define the colors
  184. RED='\033[0;31m'
  185. BLUE='\033[0;34m'
  186. GREEN='\033[1;32m'
  187. NC='\033[0m'
  188. # Calculate the center of the terminal window
  189. TERMINAL_WIDTH=$(tput cols)
  190. TEXT1_PADDING=$((($TERMINAL_WIDTH-${#TEXT1})/2))
  191. TEXT2_PADDING=$((($TERMINAL_WIDTH-${#TEXT2})/2))
  192. TEXT3_PADDING=$((($TERMINAL_WIDTH-${#TEXT3})/2))
  193. TEXT4_PADDING=$((($TERMINAL_WIDTH-${#TEXT4})/2))
  194. # Print the text in the center of the screen in the desired colors
  195. echo ""
  196. echo ""
  197. echo ""
  198. echo ""
  199. echo ""
  200. echo ""
  201. echo ""
  202. echo ""
  203. echo ""
  204. echo ""
  205. echo -e "${GREEN}$(printf '%*s' $TEXT1_PADDING)${TEXT1} ${NC}"
  206. echo -e "${NC}$(printf '%*s' $TEXT2_PADDING)${TEXT2} ${NC}"
  207. echo -e "${RED}$(printf '%*s' $TEXT3_PADDING)${TEXT3} ${NC}"
  208. echo -e "${RED}$(printf '%*s' $TEXT4_PADDING)${TEXT4} ${NC}"
  209. echo ""
  210. echo ""
  211. echo ""
  212. echo ""
  213. echo ""
  214. echo ""
  215. echo ""
  216. echo ""
  217. echo ""
  218. echo ""
  219. # # Remove ROS2
  220. # sudo apt remove ~nros-humble-* && sudo apt autoremove
  221. # sudo rm /etc/apt/sources.list.d/ros2.list
  222. # sudo apt update
  223. # sudo apt autoremove
  224. # # Consider upgrading for packages previously shadowed.
  225. # sudo apt upgrade