ros2_humble_install.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. # ROS Desktop Install(ROS, RViz, demos, tutorials)
  98. sudo apt install ros-humble-desktop -y
  99. # ROS Base Install(Communication libraries, message packages, command line tools but no GUI tools)
  100. sudo apt install ros-humble-ros-base -y
  101. # Development tools(Compilers and other tools to build ROS packages)
  102. sudo apt install ros-dev-tools -y
  103. # nav2
  104. sudo apt install ros-humble-nav2-common ros-humble-navigation2 -y
  105. # Install build tool
  106. sudo apt install python3-colcon-common-extensions -y
  107. # Environment setup
  108. if ! grep -q "source /opt/ros/humble/setup.bash" ~/.bashrc; then
  109. echo "# ROS2 HUMBLE ENVIRONMENT SETTINGS" | sudo tee -a ~/.bashrc
  110. echo "source /opt/ros/humble/setup.bash" | sudo tee -a ~/.bashrc
  111. echo "ROS2 Humble environment setup added to ~/.bashrc"
  112. else
  113. echo "ROS2 Humble environment is already set in ~/.bashrc"
  114. fi
  115. source ~/.bashrc
  116. # Create your ROS2 workspace
  117. if [ -d "$workspace_dir" ]; then
  118. echo " ROS2 workspace already exists, skip creating."
  119. else
  120. echo "Creating ROS2 workspace"
  121. cd ~
  122. mkdir -p ros2_workspace/src
  123. cd ~/ros2_workspace
  124. # Install package dependencies
  125. echo "Installing package dependencies..."
  126. sudo pip install rosdep
  127. sudo pip install rosdepc
  128. sudo rosdepc init > /dev/null
  129. rosdepc update > /dev/null
  130. rosdep install --from-paths src --ignore-src --rosdistro humble -y
  131. echo "Building workspace..."
  132. colcon build
  133. cd ~
  134. fi
  135. # System update again
  136. sudo apt update
  137. sudo apt dist-upgrade -y
  138. # install cartographer
  139. echo "Installing cartographer package dependencies..."
  140. sudo apt-get install ninja-build -y
  141. sudo apt-get install libgflags-dev -y
  142. sudo apt-get install libgoogle-glog-dev -y
  143. sudo apt-get install stow -y
  144. sudo apt-get install libgtest-dev -y
  145. sudo apt-get install libceres-dev -y
  146. 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
  147. PROTOBUF_DIR="protobuf"
  148. if test ! -d "$PROTOBUF_DIR"; then
  149. set -o errexit
  150. set -o verbose
  151. VERSION="v3.4.1"
  152. # Build and install proto3.
  153. git clone http://hualiyun.cc:7000/codeFromRos/protobuf.git
  154. cd protobuf
  155. git checkout tags/${VERSION}
  156. mkdir build
  157. cd build
  158. cmake -G Ninja \
  159. -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
  160. -DCMAKE_BUILD_TYPE=Release \
  161. -Dprotobuf_BUILD_TESTS=OFF \
  162. ../cmake
  163. ninja
  164. sudo ninja install
  165. cd ~
  166. rm -rf protobuf
  167. echo "protobuf install succ"
  168. else
  169. echo "protobuf is already installed"
  170. fi
  171. echo "Install cartographer package dependencies succ"
  172. # Verifying ROS2 installation
  173. # clear
  174. # Define the variables to be printed
  175. TEXT1="ROS2 Humble installation completed!"
  176. TEXT2="Please open new terminals and run commands to verify the installation:"
  177. TEXT3="ros2 run demo_nodes_cpp talker"
  178. TEXT4="ros2 run demo_nodes_py listener"
  179. # Define the colors
  180. RED='\033[0;31m'
  181. BLUE='\033[0;34m'
  182. GREEN='\033[1;32m'
  183. NC='\033[0m'
  184. # Calculate the center of the terminal window
  185. TERMINAL_WIDTH=$(tput cols)
  186. TEXT1_PADDING=$((($TERMINAL_WIDTH-${#TEXT1})/2))
  187. TEXT2_PADDING=$((($TERMINAL_WIDTH-${#TEXT2})/2))
  188. TEXT3_PADDING=$((($TERMINAL_WIDTH-${#TEXT3})/2))
  189. TEXT4_PADDING=$((($TERMINAL_WIDTH-${#TEXT4})/2))
  190. # Print the text in the center of the screen in the desired colors
  191. echo ""
  192. echo ""
  193. echo ""
  194. echo ""
  195. echo ""
  196. echo ""
  197. echo ""
  198. echo ""
  199. echo ""
  200. echo ""
  201. echo -e "${GREEN}$(printf '%*s' $TEXT1_PADDING)${TEXT1} ${NC}"
  202. echo -e "${NC}$(printf '%*s' $TEXT2_PADDING)${TEXT2} ${NC}"
  203. echo -e "${RED}$(printf '%*s' $TEXT3_PADDING)${TEXT3} ${NC}"
  204. echo -e "${RED}$(printf '%*s' $TEXT4_PADDING)${TEXT4} ${NC}"
  205. echo ""
  206. echo ""
  207. echo ""
  208. echo ""
  209. echo ""
  210. echo ""
  211. echo ""
  212. echo ""
  213. echo ""
  214. echo ""
  215. # # Remove ROS2
  216. # sudo apt remove ~nros-humble-* && sudo apt autoremove
  217. # sudo rm /etc/apt/sources.list.d/ros2.list
  218. # sudo apt update
  219. # sudo apt autoremove
  220. # # Consider upgrading for packages previously shadowed.
  221. # sudo apt upgrade