ros2_install_localhost.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. # Enable Ubuntu Universe repository
  78. echo "Enabling Ubuntu Universe repository..."
  79. sudo apt install software-properties-common -y
  80. sudo add-apt-repository universe -y
  81. # Add the ROS 2 GPG key with apt
  82. echo "Adding the ROS 2 GPG key with apt..."
  83. sudo apt update && sudo apt install curl -y
  84. sudo curl -sSL http://hualiyun.cc:7000/codeFromRos/rosdistro/raw/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
  85. # Add the repository to your sources list
  86. echo "Adding the ROS 2 repository to your sources list..."
  87. # 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
  88. echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://localhost/ros2/ubuntu/ $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
  89. # Update apt repository caches
  90. echo "Updating software caches..."
  91. sudo apt update
  92. echo "Upgrading software..."
  93. sudo apt upgrade -y
  94. # Install ROS
  95. echo "Installing ROS2 Humble..."
  96. # ROS Desktop Install(ROS, RViz, demos, tutorials)
  97. sudo apt install ros-humble-desktop -y
  98. # ROS Base Install(Communication libraries, message packages, command line tools but no GUI tools)
  99. sudo apt install ros-humble-ros-base -y
  100. # Development tools(Compilers and other tools to build ROS packages)
  101. sudo apt install ros-dev-tools -y
  102. # Install build tool
  103. sudo apt install python3-colcon-common-extensions -y
  104. # Environment setup
  105. if ! grep -q "source /opt/ros/humble/setup.bash" ~/.bashrc; then
  106. echo "# ROS2 HUMBLE ENVIRONMENT SETTINGS" | sudo tee -a ~/.bashrc
  107. echo "source /opt/ros/humble/setup.bash" | sudo tee -a ~/.bashrc
  108. echo "ROS2 Humble environment setup added to ~/.bashrc"
  109. else
  110. echo "ROS2 Humble environment is already set in ~/.bashrc"
  111. fi
  112. source ~/.bashrc
  113. # Create your ROS2 workspace
  114. if [ -d "$workspace_dir" ]; then
  115. echo " ROS2 workspace already exists, skip creating."
  116. else
  117. echo "Creating ROS2 workspace"
  118. cd ~
  119. mkdir -p ros2_workspace/src
  120. cd ~/ros2_workspace
  121. # Install package dependencies
  122. echo "Installing package dependencies..."
  123. sudo pip install rosdep
  124. sudo pip install rosdepc
  125. sudo rosdepc init > /dev/null
  126. rosdepc update > /dev/null
  127. rosdep install --from-paths src --ignore-src --rosdistro humble -y
  128. echo "Building workspace..."
  129. colcon build
  130. cd ~
  131. fi
  132. # System update again
  133. sudo apt update
  134. sudo apt dist-upgrade -y
  135. # install cartographer
  136. sudo apt-get install ninja-build -y
  137. sudo apt-get install libgflags-dev -y
  138. sudo apt-get install libgoogle-glog-dev -y
  139. sudo apt-get install stow -y
  140. sudo apt-get install libgtest-dev -y
  141. sudo apt-get install libceres-dev -y
  142. 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
  143. ABSEIL_DIR="abseil-cpp"
  144. if test ! -d "$ABSEIL_DIR"; then
  145. set -o errexit
  146. set -o verbose
  147. git clone http://hualiyun.cc:3567/carto/abseil-cpp.git
  148. cd abseil-cpp
  149. git checkout d902eb869bcfacc1bad14933ed9af4bed006d481
  150. mkdir build
  151. cd build
  152. cmake -G Ninja \
  153. -DCMAKE_BUILD_TYPE=Release \
  154. -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
  155. -DCMAKE_INSTALL_PREFIX=/usr/local/stow/absl \
  156. ..
  157. sudo sed -i 's/std::max(SIGSTKSZ, 65536)/std::max<size_t>(SIGSTKSZ, 65536)/g' ../absl/debugging/failure_signal_handler.cc
  158. sudo sed -i '/#include/a #include <limits>' ../absl/synchronization/internal/graphcycles.cc
  159. ninja
  160. sudo ninja install
  161. cd /usr/local/stow
  162. sudo stow absl
  163. cd ~
  164. rm -rf abseil-cpp
  165. echo "abseil install succ"
  166. else
  167. echo "abseil is already installed"
  168. fi
  169. PROTOBUF_DIR="protobuf"
  170. if test ! -d "$PROTOBUF_DIR"; then
  171. set -o errexit
  172. set -o verbose
  173. VERSION="v3.4.1"
  174. # Build and install proto3.
  175. git clone http://hualiyun.cc:3567/carto/protobuf.git
  176. cd protobuf
  177. git checkout tags/${VERSION}
  178. mkdir build
  179. cd build
  180. cmake -G Ninja \
  181. -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
  182. -DCMAKE_BUILD_TYPE=Release \
  183. -Dprotobuf_BUILD_TESTS=OFF \
  184. ../cmake
  185. ninja
  186. sudo ninja install
  187. cd ~
  188. rm -rf protobuf
  189. echo "protobuf install succ"
  190. else
  191. echo "protobuf is already installed"
  192. fi
  193. # Verifying ROS2 installation
  194. # clear
  195. # Define the variables to be printed
  196. TEXT1="ROS2 Humble installation completed!"
  197. TEXT2="Please open new terminals and run commands to verify the installation:"
  198. TEXT3="ros2 run demo_nodes_cpp talker"
  199. TEXT4="ros2 run demo_nodes_py listener"
  200. # Define the colors
  201. RED='\033[0;31m'
  202. BLUE='\033[0;34m'
  203. GREEN='\033[1;32m'
  204. NC='\033[0m'
  205. # Calculate the center of the terminal window
  206. TERMINAL_WIDTH=$(tput cols)
  207. TEXT1_PADDING=$((($TERMINAL_WIDTH-${#TEXT1})/2))
  208. TEXT2_PADDING=$((($TERMINAL_WIDTH-${#TEXT2})/2))
  209. TEXT3_PADDING=$((($TERMINAL_WIDTH-${#TEXT3})/2))
  210. TEXT4_PADDING=$((($TERMINAL_WIDTH-${#TEXT4})/2))
  211. # Print the text in the center of the screen in the desired colors
  212. echo ""
  213. echo ""
  214. echo ""
  215. echo ""
  216. echo ""
  217. echo ""
  218. echo ""
  219. echo ""
  220. echo ""
  221. echo ""
  222. echo -e "${GREEN}$(printf '%*s' $TEXT1_PADDING)${TEXT1} ${NC}"
  223. echo -e "${NC}$(printf '%*s' $TEXT2_PADDING)${TEXT2} ${NC}"
  224. echo -e "${RED}$(printf '%*s' $TEXT3_PADDING)${TEXT3} ${NC}"
  225. echo -e "${RED}$(printf '%*s' $TEXT4_PADDING)${TEXT4} ${NC}"
  226. echo ""
  227. echo ""
  228. echo ""
  229. echo ""
  230. echo ""
  231. echo ""
  232. echo ""
  233. echo ""
  234. echo ""
  235. echo ""
  236. # # Remove ROS2
  237. # sudo apt remove ~nros-humble-* && sudo apt autoremove
  238. # sudo rm /etc/apt/sources.list.d/ros2.list
  239. # sudo apt update
  240. # sudo apt autoremove
  241. # # Consider upgrading for packages previously shadowed.
  242. # sudo apt upgrade