Rosanswers logo

(This is a basic quest. I'm new to ROS.)

I know that environment set up is setting up the environment, but what environment? What is actually set up there? Any example?...(1)

From this ros tutorial page:

If roscd says similar to roscd: No such package/stack 'beginner_tutorials' , you will need to source the environment setup file like you did at the end of the create_a_workspace tutorial:

$ cd ~/catkin_ws $ source devel/setup.bash $ roscd beginner_tutorials

Why do I have to setup again if previously I have set it up?...(2)

Before setup , roscd beginner_tutorials can't be executed, but after set up, it can. What does set up actually do here?...(3)

Lastly, when installing ROS according to this Ros installation page ,we set up the environment for ros. My quests are: Again, what environment is set up/what is actually being set up here?...(4)

Why for this one we don't need to set up the ros env everytime open a new session, unlike the previous one where we have to set up again eventhough we once set it up?...(5)

Originally posted by alienmon on ROS Answers with karma: 582 on 2016-08-10

Post score: 1


1 Answer 1


Rosanswers logo

The primary thing that happens when you source your catkin workspace's devel/setup.bash file is that you are setting environment variables.

For example, you'll notice a difference in many environment variables before & after sourcing the setup file:

  • ROS_PACKAGE_PATH: This tells ROS where to find packages, so that rosrun <pkg> <node> actually works.
  • CMAKE_PREFIX_PATH: This tells the compiler a bunch of stuff, including where to find the libraries defined in your package.
  • LD_LIBRARY_PATH: This is used at execution time to dynamically load shared libraries.
  • PYTHONPATH: This tells python where to look for any python modules defined in your package.

(There are others, there are just the important ones).

As an example, this is my ROS_PACKAGE_PATH before and after sourcing the setup file in some random catkin workspace:

$ cd /path/to/my/catkin_ws
$ source devel/setup.bash

Note the addition of my catkin workspace's src/ directory to the ROS_PACKAGE_PATH. This enables me to run any nodes I have defined in my workspace, otherwise only the system-installed packages are available. (Side note: /opt/ros/<distro>/ basically acts as one big catkin workspace too.)

Normally, you always source the global /opt/ros/<distro>/setup.bash every time you open a terminal because it's in your .bashrc file. However, since new terminal sessions don't carry over environment variables, you need to source your workspace's setup file again each time you open a new session.

If you do not want to have to source the workspace setup file yourself each time you open a new terminal, you can always add it to your .bashrc file:

echo "source /path/to/your/workspace/devel/setup.bash" >> ~/.bashrc

Obviously this only works if you have a single catkin workspace.

Originally posted by Felix Duvallet with karma: 539 on 2016-08-11

This answer was ACCEPTED on the original site

Post score: 7

Original comments

Comment by jarvisschultz on 2016-08-11:
Very helpful answer! Technically the LD_LIBRARY_PATH is not for any compiler, it's for used by ld.so at execution time to dynamically load shared libraries. Without it, you could have a piece of code that compiles but doesn't run.

Comment by jarvisschultz on 2016-08-11:
That situation would depend on things like ldconfig and /etc/ld.so.cache, and the -rpath flag of gcc/g++ (see this tutorial, step 4). The CMAKE_PREFIX_PATH is like what you described.

Comment by jarvisschultz on 2016-08-11:
It is important for catkin/CMake to be able to find ROS libraries, and it is controlled by the setup.bash scripts.

Comment by Felix Duvallet on 2016-08-11:
Thanks for the comment, you're absolutely right. I've updated the answer. I think the link to the shared libraries tutorial is still helpful as a comment.

Comment by alienmon on 2016-08-14:
So everytime a new terminal is opened, it automatically source what's in bashrc file. And /opt/ros//setup.bash is in bashrc file, that's why I don't have to source it everytime i open terminal. While source devel/setup.bash is not, and that's why I have to source evertytime i open new term.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.