In your ~/.bashrc or ~/.bash_profile Simply source the "enable" script provided with the devtoolset. For example, with the Devtoolset 2, the command is:
source /opt/rh/devtoolset-2/enable
or
source scl_source enable devtoolset-2
Lot more efficient: no forkbomb, no tricky shell
Answer from Destroyica on Stack ExchangeIn your ~/.bashrc or ~/.bash_profile Simply source the "enable" script provided with the devtoolset. For example, with the Devtoolset 2, the command is:
source /opt/rh/devtoolset-2/enable
or
source scl_source enable devtoolset-2
Lot more efficient: no forkbomb, no tricky shell
An alternative of source /opt/rh/devtoolset-4/enable is
source scl_source enable devtoolset-4
The above shell script scl_source is more elegant than using a hard coded path (may be different on another machine). However scl_source does less because /opt/rh/devtoolset-4/enable uses scl_source and other stuff.
To use scl_source you may have to upgrade package scl-utils
yum update scl-utils # old scl-utils versions miss scl_source
Quick copy-paste
echo 'source scl_source enable devtoolset-4' >> ~/.bashrc
# Do not forget to change the version ↑
Source code for curious people
An example of scl_source source code:
https://gist.github.com/bkabrda/6435016
The scl_source installed on my Red Hat 7.1
#!/bin/bash
_scl_source_help="Usage: source scl_source <action> [<collection> ...]
Don't use this script outside of SCL scriptlets!
Options:
-h, --help display this help and exit"
if [ $# -eq 0 -o $1 = "-h" -o $1 = "--help" ]; then
echo "$_scl_source_help"
return 0
fi
if [ -z "$_recursion" ]; then
_recursion="false"
fi
if [ -z "$_scl_scriptlet_name" ]; then
# The only allowed action in the case of recursion is the same
# as was the original
_scl_scriptlet_name=$1
fi
shift 1
if [ -z "$_scl_dir" ]; then
# No need to re-define the directory twice
_scl_dir=/etc/scl/conf
if [ ! -e $_scl_dir ]; then
_scl_dir=/etc/scl/prefixes
fi
fi
for arg in "$@"; do
_scl_prefix_file=$_scl_dir/$arg
_scl_prefix=`cat $_scl_prefix_file 2> /dev/null`
if [ $? -ne 0 ]; then
echo "Can't read $_scl_prefix_file, $arg is probably not installed."
return 1
fi
# First check if the collection is already in the list
# of collections to be enabled
for scl in ${_scls[@]}; do
if [
scl ]; then
continue 2
fi
done
# Now check if the collection isn't already enabled
/usr/bin/scl_enabled $arg > /dev/null 2> /dev/null
if [ $? -ne 0 ]; then
_scls+=($arg)
_scl_prefixes+=($_scl_prefix)
fi;
done
if [ $_recursion == "false" ]; then
_i=0
_recursion="true"
while [
{#_scls[@]} ]; do
_scl_scriptlet_path="${_scl_prefixes[$_i]}/${_scls[$_i]}/${_scl_scriptlet_name}"
source "$_scl_scriptlet_path"
if [ $? -ne 0 ]; then
echo "Can't source $_scl_scriptlet_name, skipping."
else
export X_SCLS="${_scls[$_i]} $X_SCLS"
fi;
_i=
_i+1))
done
_scls=()
_scl_prefixes=()
_scl_scriptlet_name=""
_recursion="false"
fi
Scl enable devtoolset-7 bash on Rocky 9
centos7 - How to `scl enable devtoolset-8 bash` when login in? - Stack Overflow
scripting - Bash - how to run scl_devtool-9 and get the gcc --version output - Unix & Linux Stack Exchange
linux - Can not find required gcc version after devtoolset installation - Stack Overflow
I had this problem as well. I don't know why scl was failing to enable the environment, but I was able to get it to load with source /opt/rh/devtoolset-7/enable.
It is because of your settings in .bashrc and/or .bash_profile. scl appends its path to gcc7 before starting your new bash shell, but the path to gcc is default to the old gcc4 again because your .bashrc appends /usr/bin to the path when starting the new bash, and it supersedes the scl settings. The solution is to use the "enable" script directly. That way no further path settings annoy you.
The scl enable ... command creates a new shell; you've probably ended up with several nested layers of shells if you've been testing this. If you exit, you'll probably see the gcc --version output from the base system. See how deeply-nested you are with something like pstree -s $$.
To run gcc --version with scl, just put the command on the scl line:
scl enable devtoolset-9 'gcc --version'
Reference: The Red Hat Developer Toolset 9 User Guide (pdf).
to get the script to act as you want it to, you need the scl command to modify the current bash session rather than spawn a new one.
you can do it this way
#!/usr/bin/env bash
# switch to GCC9 environment for the duration of the script
source scl_source enable devtoolset-9
gcc --version
I am using CentOS 7.9 and I encountered the same problem after following instructions here to install and run gcc 11. I tried launching different versions of gcc and found only devtoolset-9 works, which corresponds to the file devtoolset-9 in /etc/scl/conf/ folder. So I copied devtoolset-9 to devtoolset-11 in the same folder, and gcc 11 gets working.
I ran into the same issue that you are facing and this is how I got it fixed:
- Just want to be careful, you need to exist the Terminal and open a fresh one to start. This way, you are not under any devtoolset's bash.
- Go to /opt/rh folder, run command ls -la to see if you have any devtoolset-* folder there. Let's say you have devtoolset-8, proceed step 2.
- Go to /etc/scl/prefixes folder, if you don't see devtoolset-8 file, you can create a new one as devtoolset-8, and type 1 line: /opt/rh, then save and quit that file.
- Once you are done, you can call: scl enable devtoolset-8 -- bash w/o any error. Good luck
A work-around that I found is to set gnome-terminal to run
bash -c "$HOME/.bashrc; exec zsh"
as a custom command in place of the default shell, which is equivalent to opening a terminal emulator with
gnome-terminal -- bash -c "$HOME/.bashrc; exec zsh"
adapted from here.
It seems you could also use:
source /opt/rh/devtoolset-8/enable
Tested on RHEL7.6 for package rh-php73, it worked:
source /opt/rh/rh-php73/enable
Found the hint here
I wrote a blog post on this subject because it started to come up a lot. If you would like to read it, you can find it here: http://developerblog.redhat.com/2014/03/19/permanently-enable-a-software-collection/
tl;dr
you can source /opt/rh/devtoolset-1.1/enable in your .bashrc or, for somewhat better solution you can include:
source /opt/rh/devtoolset-1.1/enable
export X_SCLS="`scl enable devtoolset-1.1 'echo $X_SCLS'`"
But definitely check out the post for more info.
Check the URL
http://preilly.me/2013/05/28/redhat-developer-toolset-1-1/
for more information, for example how to set the CC, CPP, CXX environment variables. Or check
http://people.centos.org/tru/devtools-1.1/
for the devtool-1.1 repository for CentOS.